Repository navigation
Builds on no-GIL interpreters, but not fully supported yet #613
Description
Activity
no plan until cython support no-gil build.
Note that while Cython support for free-threaded Python isn't fully complete yet, it works well enough in
masternow (3.1.0-dev, not in 3.0.x). A nightly wheel for Cython can be installed as documented on https://py-free-threading.github.io/ci/. It's used by projects with a large amount of Cython code like SciPy and scikit-learn for their free-threaded nightly wheels.This issue should automatically go away, see https://py-free-threading.github.io/debugging/#cython-compilation-errors-unknown-type-name-__pyx_vectorcallfunc
The sdist for 1.1.0rc1 on pypi contains a couple files generated by cython which makes the install fail even if you have a version of cython that will work. Is it possible to make cython a build-time dependency and regenerate the output as needed?
See #539.
no-GIL Python is experimental. Do not ask support for it.
Just wait until stable Cython release, or manually build instead of pip install.
That is what you should do it by yourself when using experimental software.Reacted by Ratul HasanHi.
You can use this script to compile for nogil python, remember to install cython from git.#!/usr/bin/env python import os import sys from setuptools import Extension, setup from Cython.Build import cythonize from Cython.Compiler.Version import version as cython_version from packaging.version import Version PYPY = hasattr(sys, "pypy_version_info") libraries = [] macros = [] ext_modules = [] if sys.platform == "win32": libraries.append("ws2_32") macros = [("__LITTLE_ENDIAN__", "1")] if not PYPY and not os.environ.get("MSGPACK_PUREPYTHON"): ext_modules.append( Extension( "msgpack._cmsgpack", sources=["msgpack/_cmsgpack.pyx"], libraries=libraries, include_dirs=["."], define_macros=macros, ) ) del libraries, macros compiler_directives = { "cdivision": True, "embedsignature": True, "boundscheck": False, "wraparound": False, } if Version(cython_version) >= Version("3.1.0a0"): compiler_directives["freethreading_compatible"] = True setup( ext_modules=cythonize(ext_modules, compiler_directives = compiler_directives), packages=["msgpack"], )I've just compile it for 3.13t.
Reacted by Ratul Hasan, Mike Borsetti, everloom, Vikas, Brian Takita and Riyad ParvezHere's a patch on top of current
mainto get things to build: https://gist.github.com/ngoldbaum/ea251bb5abb10c1b46fc6bceca8ac606Reacted by Brian Takita, Inada Naoki and Riyad ParvezWith the recent update to Cython 3.1.1, msgpack-python builds with 3.13t and passes all existing tests. However, there don't seem to be any existing tests for multithreading.
I added a multithreading test for packing to this fork, and get flakey results with segmentation fault errors:
test/test_multithreading.py::test_multithread_packing Fatal Python error: Segmentation faultThe standard TODOs for adding free-threading support are:
- Audit Python bindings and declare them free-threading compatible (xref https://py-free-threading.github.io/porting/#updating-extension-modules).
- Run the test suite with pytest-run-parallel to find potential issues, and fix them.
- Run the test suite under ThreadSanitizer. If possible, depends on how many dependencies there are and if they run under TSan.
- Add cp313t-* to CI to build free-threading wheels.
For more details, please see the
suggested plan of attack in the py-free-threading guide.Let me know if help is wanted to build out multithreading tests, like the one above, as well as to make the underlying code thread safe. Any suggestions here will be very useful.
It is not relating to free threading. I don't support using one Packer from multiple threads, regardless free threding or with GIL.
I don't support using one Packer from multiple threads, regardless free threding or with GIL.
Fair enough. I don't see that documented anywhere - maybe it should be?
If you're interested in avoiding the possibility of crashing the interpreter if someone happens to try to do something unsupported like this, you can probably turn it into a runtime error.
One approach is to use an atomic integer flag that hangs off of the Packer object to indicate that it is currently in use. If someone tries to simultaneously use a Packer in two threads, the second thread sees the "in use" flag set to 1 and raises a runtime exception. You can do something similar with a
threading.Lockif you uselock.acquire(blocking=False)and raise an exception if acquiring the lock fails.@methane Would you be open to reviewing a PR that adds support for free-threading now that there's a stable Cython 3.1 release and PEP 779 has been accepted?
I've done a more thorough review of the code and it looks like there's no global state. The only real problem is when sharing
Packer/Unpackerobjects. Since you've signified that that isn't supported (and I've checked that that is the case in other language bindings formsgpackas well), I can open a PR documenting that. Optionally, we can raise an error, when detecting that an object is shared across threads.Additionally, I'm preparing a PR to add CI and build free-threaded wheels. Is that something you would be okay with?
Wait for two weeks.
When Python 3.14RC1 is released, I start development for msgpack v1.2 in main branch.
It will use new Cython and cibuildwheel.Reacted by Lysandros Nikolaou and Clémence Lesné@methane Python 3.14rc1 has been released. If you start working on free-threading support and need any help from my side, please let me know!
Reacted by Clémence LesnéI have a (draft) PR #641 that supports this, and includes a test for multi-threading.
Interesting, it only fails for no-GIL interpreters with the pure Python fallback
@methane Is there any way we can support you in implementing support for free-threading? The open PR is in a relatively good state, though it'll definitely need another set of eyes on it.
Reacted by Mike Borsettimsgpack 1.1.2 has free-threaded wheels: https://pypi.org/project/msgpack/#files
I think this can be closed now.
That wheels are not no-GIL ready. It works on free threaded Python, but it enables GIL.
I decided to release one version to support Python 3.14 before supporting no-GIL.@methane Maybe it's worth it to add a note on the docs? If so, I can help by creating a first draft. Before doing that, however, I'd like to understand better what you think in terms of what guarantees the library makes. I saw that on #654 you added
@cython.critical_sectionto the public APIs forPackerandUnpackerobjects. That to me means that objects can be shared, but users are responsible for ordering when using these in a multithreaded context. Is that correct?@clin1234 can you change the title of this issue to reflect that builds are fine but msgpack doesn't yet fully support the free-threaded build?
- changed the title
[-]Build fails on 3.14 no-GIL[/-][+]Builds on no-GIL interpreters, but not fully supported yet[/+]on Jan 23, 2026 - added a commit that references this issue
on May 6, 2026 lxml (a popular Cython project) now has a free-threaded release. msgpack is next on the list for me to wait on.
With
msgpack==1.1.2I still getThe global interpreter lock (GIL) has been enabled to load module 'msgpack._cmsgpack', which has not declared that it can run safely without the GIL..With #682 being merged, I believe the no-GIL interpreters are now fully supported
I've tested this downstream in dask CI and it looks good!