sqlite callback functions can deadlock in Python subinterpreters
还没有人认领这个 Issue。
评估
调研方向
从 Modules/_sqlite/connection.c 开始,重点查看第 824-851 行以及报告中提到的 callback 入口点;阅读 issue #59956,了解子解释器和 GIL 状态的相关背景。确定所有 callback 路径在子解释器中的行为,并验证 sqlite callback 不再发生死锁,包括 create_function、create_aggregate、create_window_function 和 set_authorizer。
由索引模型根据 Issue 内容生成。
描述
After quick search can't see that this problem as it affects sqlite is noted as existing issue so creating one so at least got one to point people at when they hit this problem when using mod_wsgi.
Main related issue around problems with simplified GIL state API and sub interpreters is:
Bug report
In the sqlite module there are various points these days where one can supply a callback function, or handler class, which a callback is made to at some point from sqlite C code, where the GIL is not already held. In order to acquire the GIL the simplified GIL state API functions are used. These functions though are well known to fail when used in the context of a Python sub interpreter as the TLS state related to them only works for the main interpreter context. The result is that use of sqlite in a Python sub interpreter can result in deadlocks when these callbacks are invoked. As mod_wsgi is a big user of Python sub interpreters, it can easily be affected by the problem.
Examples of some (possibly not all) of these callback function points in sqlite are:
- https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_function
- https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_aggregate
- https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_window_function
- https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.set_authorizer
Example of problematic code is found in https://github.com/python/cpython/blob/main/Modules/_sqlite/connection.c#L824-L851.
static void
func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
{
PyGILState_STATE threadstate = PyGILState_Ensure();
PyObject* args;
PyObject* py_retval = NULL;
int ok;
args = _pysqlite_build_py_params(context, argc, argv);
if (args) {
callback_context *ctx = (callback_context *)sqlite3_user_data(context);
assert(ctx != NULL);
py_retval = PyObject_CallObject(ctx->callable, args);
Py_DECREF(args);
}
ok = 0;
if (py_retval) {
ok = _pysqlite_set_result(context, py_retval) == 0;
Py_DECREF(py_retval);
}
if (!ok) {
set_sqlite_error(context, "user-defined function raised exception");
}
PyGILState_Release(threadstate);
}
The PyGILState_Ensure() function can deadlock when the current thread was operating against a Python sub interpreter and not the main interpreter context.
Basically any use of PyGILState_Ensure() in connection.c of sqlite module could be problematic.
Workaround for users of mod_wsgi is to force the use of the main Python interpreter context using:
WSGIApplicationGroup %{GLOBAL}
If mod_wsgi users are hosting multiple WSGI applications, they should ensure that they are using a separate daemon process group for each so that when forcing WSGI application to use the main Python interpreter context they don't conflict as that will then only occur in their respective processes.
Tagging @ericsnowcurrently as last person still working on related issues GIL state issues.
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
docs pending
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 72/100
-
build type-bug
难度 2/5 1-3 小时 新手友好度 76/100
-
stdlib topic-email type-feature
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
难度 2/5 1-3 小时 新手友好度 74/100
bancolombia/sentinel#23 ·
-
test md 未关闭CI
难度 2/5 1-3 小时 新手友好度 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
难度 2/5 1-3 小时 新手友好度 74/100
langchain-ai/deepagents#6450 ·
-
bug client
难度 2/5 1-3 小时 新手友好度 88/100