sabctools icon indicating copy to clipboard operation
sabctools copied to clipboard

sabctools and No GIL? "'sabctools.sabctools', which has not declared that it can run safely without the GIL. "

Open sanderjo opened this issue 1 year ago • 4 comments

I'm playing with python 3.13 No GIL / free-threading, and I get this:

root@87c5b0d55fce:/sabnzbd# python3.13-nogil                     
Python 3.13.1 experimental free-threading build (main, Dec  4 2024, 08:54:14) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sabctools
<frozen importlib._bootstrap>:488: RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module 'sabctools.sabctools', which has not declared that it can run safely without the GIL. To override this behavior and keep the GIL disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.
>>> 

So is sabctools safe with no GIL?

FWIW

Not an answer to my question, but indeed you can overwrite it

root@87c5b0d55fce:/sabnzbd# python3.13-nogil -Xgil=0             
Python 3.13.1 experimental free-threading build (main, Dec  4 2024, 08:54:14) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sabctools
>>> sabctools.__version__
'8.2.5'
>>> 

sanderjo avatar Dec 18 '24 09:12 sanderjo

This might be related: https://docs.python.org/3.13/howto/free-threading-extensions.html#module-initialization

sanderjo avatar Dec 18 '24 10:12 sanderjo

I did have a brief look at it when 3.13 was released, but SAB needs to wait for compatability from https://github.com/python-cffi/cffi/issues/126 and there may be others so I didn't pursue it further.

As you have found it's only 3 lines to mark the module as nogil compatibile, but as for the question of whether it's safe I'm not sure. There is some guidance in https://docs.python.org/3/howto/free-threading-extensions.html and as far as I can see most method are isolated with no global state modifications. Likely need to look at the unlocked ssl parts and see if that needs to merge any upstream changes, but it's also possible SAB won't need it in nogil.

#ifdef Py_GIL_DISABLED
    PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
#endif 

The real fun is in SAB itself, there could be all sorts of bugs were locking will be required but it currently relies on GIL behaviour.

mnightingale avatar Dec 18 '24 10:12 mnightingale

The real fun is in SAB itself, there could be all sorts of bugs were locking will be required but it currently relies on GIL behaviour.

I hacked together a working SABnzbd no-GIL : https://github.com/sanderjo/sabnzbd-nogil-docker . "Working" as in: it starts up, I can download, and no problems so far. (Not working: SABnzbd listening to HTTPS)

image

sanderjo avatar Dec 18 '24 15:12 sanderjo

Hi! I'm working on ecosystem compatibility for free-threaded Python. I found this by looking at issues linking to the cffi issue.

We're working on support for cffi over at http://github.com/quansight-labs/cffi

Also take a look at the guide we've been writing: https://py-free-threading.github.io

ngoldbaum avatar Mar 12 '25 20:03 ngoldbaum