Visitar URL original
Python 3.14 free-threaded support by greateggsgreg · Pull Request #2721 · pythonnet/pythonnet · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1d93b30
Thread-safety prep for free-threading builds
greateggsgreg May 9, 2026
e029137
Initialise pythonnet on free-threaded Python (#2720)
greateggsgreg May 9, 2026
fd56aa4
Make extension/CLR-object registries thread-safe
greateggsgreg May 9, 2026
b727a15
Atomic type creation in ReflectedClrType.GetOrCreate / TypeManager.Ge…
greateggsgreg May 9, 2026
debba0d
Add free-threaded thread-stress tests and 3.14t to CI matrix
greateggsgreg May 9, 2026
585916e
Atomic GCHandle ownership and finalizer-thread shutdown guards
greateggsgreg May 10, 2026
c0ddb29
Make additional internal registries thread-safe
greateggsgreg May 10, 2026
096e466
test_thread: join worker threads before returning
greateggsgreg May 10, 2026
5005f36
test_thread: cover ModuleObject thread-safe registries
greateggsgreg May 10, 2026
3ad586a
Wider thread-safety audit fixes for free-threaded Python
greateggsgreg May 10, 2026
6b1f3f4
Document lock acquisition sites and strong->weak GCHandle swap
greateggsgreg May 10, 2026
732d97e
Preserve InternString single-write invariant under DEBUG
greateggsgreg May 10, 2026
3d56611
test_thread: cover real-world consumer patterns
greateggsgreg May 10, 2026
7094b12
Auto-detect free-threaded libpython in venv home
greateggsgreg May 12, 2026
f49ed1b
Snapshot pypath, use ConcurrentDictionary for thunks and slot holders
greateggsgreg May 13, 2026
fa89ff0
Fix handling of python runtime suffixes m/t
greateggsgreg May 13, 2026
a373e79
Fix threadtest race
greateggsgreg May 13, 2026
43a4237
Fix double-free in chained ClassDerived Finalize
greateggsgreg May 13, 2026
b42377e
Enable Mono CI jobs on free-threaded Python 3.14
greateggsgreg May 13, 2026
526a297
Inline freethreaded_only as pytest.mark.skipif at call sites
greateggsgreg May 13, 2026
8485837
Fix InterruptTest assertion on free-threaded Python 3.14
greateggsgreg May 13, 2026
a459772
Add concurrent stress tests for PyBuffer.Dispose and CLR-cycle gc.col…
greateggsgreg May 15, 2026
3bf6d68
Trim concurrent overhead on hot paths from free-threading prep
greateggsgreg May 15, 2026
3433446
Pre-warm ctor binder in concurrent-gc test to avoid first-call race
greateggsgreg May 15, 2026
4a034f7
Make MethodBinder.GetMethods lazy init thread-safe under free-threading
greateggsgreg May 15, 2026
afd5799
Precompute method precedence to avoid quadratic GetParameters allocat…
greateggsgreg May 15, 2026
95e4811
Zero the slot in ClassDerived.tp_dealloc when tp_clear already ran to…
greateggsgreg May 15, 2026
a799c44
Keep ClassDerived wrapper alive across the NewObjectToPython slot dem…
greateggsgreg May 15, 2026
220b4b4
Document private helpers added during free-threading prep
greateggsgreg May 16, 2026
dfda807
Add debug echoes and a 6-minute step timeout to the Mono test job
greateggsgreg May 16, 2026
1130f6a
Drop the per-loop CLR GC.Collect from concurrent-gc test to avoid Mon…
greateggsgreg May 16, 2026
f75cbab
Add temporary Mono-step diagnostics on Linux/macOS to locate the x64-…
greateggsgreg May 16, 2026
64ea342
Revert temporary Mono-step diagnostics now that the underlying race i…
greateggsgreg May 16, 2026
fda8211
Add user-facing threading guide covering GIL, free-threading, and com…
greateggsgreg May 18, 2026
356d76f
Harden CollectBasicObject against .NET-GC timing differences
greateggsgreg May 18, 2026
f0ee4b2
Adjust the header offset on 32bit systems
filmor Aug 21, 2026
3445b08
Drop broken and unnecessary exclude
filmor Aug 22, 2026
8e6861e
Be strict about not loading on Python 3.13
filmor Aug 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test_thread: cover real-world consumer patterns
Two FT-only tests for code paths identified by reviewing pythonnet's
downstream consumers (QuantConnect/Lean, Rhino.Inside, Speckle) and
the historical issue tracker:

test_concurrent_clr_delegate_invocation_from_python
  Python callables wrapped as distinct CLR delegate types
  (PublicDelegate, StringDelegate, BoolDelegate) and invoked
  concurrently from worker threads.  Canonical embedder pattern
  for callbacks/event handlers; hits DelegateManager.GetDispatcher
  (the Reflection.Emit lock added in 92072bd) and the GIL
  re-acquisition path in Dispatcher.Dispatch.

test_concurrent_generic_type_binding
  36 distinct Dictionary[K,V] / List[K] type-arg pairs resolved
  concurrently from N threads.  Targets the open-issue family
  #2269 (ClassManager hash collision crash),
  #1407 (ClassManager perf regression with MaybeType keys), and
  #821 (generic resolution race).  Exercises ClassManager.cache,
  TypeManager.cache, GenericUtil.mapping, and the generic-binding
  fast path simultaneously.

Both are @freethreaded_only because the cumulative pytest state
under GIL builds trips the same pre-existing CPython 3.11/3.12/3.13
GC crash that gates the other high-contention tests in this file.
  • Loading branch information
greateggsgreg authored and filmor committed Aug 18, 2026
commit 3d5661116c48c06d2cdb568bb4840eeb6cf8c595
57 changes: 57 additions & 0 deletions tests/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,63 @@ def build(_):
assert all(_run_in_threads(build, n_threads=8))


@freethreaded_only
def test_concurrent_clr_delegate_invocation_from_python():
"""Python callables wrapped as distinct CLR delegate types, invoked concurrently.

Real-world: QuantConnect/Lean and similar embedders pass Python callables
where C# expects a delegate; under FT the dispatcher emit + Invoke run from
multiple threads. Hits DelegateManager.GetDispatcher (Reflection.Emit lock)
and Dispatcher.Dispatch (Py.GIL reacquisition).
"""
from Python.Test import (
PublicDelegate, StringDelegate, BoolDelegate,
)

delegates = (
PublicDelegate(lambda: None),
StringDelegate(lambda: "ok"),
BoolDelegate(lambda: True),
)

def fire(i):
d = delegates[i % len(delegates)]
for _ in range(200):
d()
return True

assert all(_run_in_threads(fire, n_threads=8))


@freethreaded_only
def test_concurrent_generic_type_binding():
"""Concurrent `Dictionary[K, V]` with many distinct type-arg pairs.

Real-world: pythonnet/pythonnet#2269, #1407, #821 — concurrent ToPython /
GenericByName from N threads. Exercises ClassManager.cache,
TypeManager.cache, GenericUtil.mapping, and the generic-type binding
fast path together.

FT-only: the cumulative state under the full pytest suite trips the same
pre-existing CPython 3.11/3.12/3.13 GIL-build crash that gates the other
high-contention tests in this file.
"""
from System import Int32, Int64, String, Double, Single, Byte
from System.Collections.Generic import Dictionary, List

arg_types = (Int32, Int64, String, Double, Single, Byte)
pairs = [(k, v) for k in arg_types for v in arg_types]

def bind(_):
for _ in range(50):
for k, v in pairs:
_ = Dictionary[k, v]
_ = List[k]
return True

assert all(_run_in_threads(bind, n_threads=8))


@freethreaded_only
def test_concurrent_shutdown_handler_register():
"""Concurrent AddShutdownHandler/RemoveShutdownHandler — exercises ShutdownHandlers list.
Expand Down