sqlite callback functions can deadlock in Python subinterpreters #101767
Labels
3.11
only security fixes
3.12
only security fixes
3.13
bugs and security fixes
topic-sqlite3
topic-subinterpreters
type-bug
An unexpected behavior, bug, or error
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:
Example of problematic code is found in https://github.com/python/cpython/blob/main/Modules/_sqlite/connection.c#L824-L851.
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()
inconnection.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:
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.
The text was updated successfully, but these errors were encountered: