Visitar URL original
Python 3.14 free-threaded support by greateggsgreg · Pull Request #2721 · pythonnet/pythonnet · GitHub
Skip to content
Merged
Show file tree
Hide file tree
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
Snapshot pypath, use ConcurrentDictionary for thunks and slot holders
  • Loading branch information
greateggsgreg authored and filmor committed Aug 18, 2026
commit f49ed1bd32833d557bb41efb0c170a81f3c1d6a3
22 changes: 13 additions & 9 deletions src/runtime/AssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ internal class AssemblyManager

// modified from event handlers below, potentially triggered from different .NET threads
private static readonly ConcurrentQueue<Assembly> assemblies = new();
internal static readonly List<string> pypath = new (capacity: 16);
// Snapshot; UpdatePath swaps under lock, readers iterate the captured ref.
internal static volatile IReadOnlyList<string> pypath = Array.Empty<string>();
private static readonly object _pypathLock = new();
private AssemblyManager()
{
}
Expand All @@ -49,7 +51,7 @@ private AssemblyManager()
/// </summary>
internal static void Initialize()
{
pypath.Clear();
pypath = Array.Empty<string>();

AppDomain domain = AppDomain.CurrentDomain;

Expand Down Expand Up @@ -154,19 +156,20 @@ internal static void UpdatePath()
{
BorrowedReference list = Runtime.PySys_GetObject("path");
var count = Runtime.PyList_Size(list);
if (count != pypath.Count)
if (count == pypath.Count) return;

lock (_pypathLock)
{
pypath.Clear();
if (count == pypath.Count) return;
var fresh = new List<string>(checked((int)count));
probed.Clear();
for (var i = 0; i < count; i++)
{
BorrowedReference item = Runtime.PyList_GetItem(list, i);
string? path = Runtime.GetManagedString(item);
if (path != null)
{
pypath.Add(path);
}
if (path != null) fresh.Add(path);
}
pypath = fresh;
}
}

Expand Down Expand Up @@ -196,7 +199,8 @@ public static string FindAssembly(string name)

static IEnumerable<string> FindAssemblyCandidates(string name)
{
foreach (string head in pypath)
var paths = pypath;
foreach (string head in paths)
{
string path;
if (head == null || head.Length == 0)
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/Finalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ internal void ThrottledCollect()
if (!started) throw new InvalidOperationException($"{nameof(PythonEngine)} is not initialized");

if (!Enable || Interlocked.Increment(ref _throttled) < Threshold) return;
// Stale pointers on the queue would crash Py_DecRef during teardown.
// Defends against externally-driven Py_Finalize (e.g. host's atexit):
// queue pointers may already be freed, so skip the drain.
if (Runtime._Py_IsFinalizing() == true) return;
Interlocked.Exchange(ref _throttled, 0);
this.Collect();
Expand All @@ -138,7 +139,7 @@ internal void AddFinalizedObject(ref IntPtr obj, int run
return;
}

// Skip on FT: stale ob_ref_local read from the finalizer thread can crash.
// Skip on FT: the split refcount can race here and trip the assert spuriously.
if (!Native.ABI.IsFreeThreaded)
{
Debug.Assert(Runtime.Refcount(new BorrowedReference(obj)) > 0);
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ internal static Type GetPrototype(MethodInfo method)
}


internal static Dictionary<IntPtr, Delegate> allocatedThunks = new();
// Concurrent: documents the multi-writer contract previously enforced
// by callers happening to hold TypeManager._cacheCreateLock.
internal static ConcurrentDictionary<IntPtr, Delegate> allocatedThunks = new();

internal static ThunkInfo GetThunk(MethodInfo method)
{
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ internal static unsafe void XIncref(BorrowedReference op)
internal static unsafe void XDecref(StolenReference op)
{
#if DEBUG
// Racy on FT: stale ob_ref_local read may crash.
// Skip on FT: the split refcount can race here and trip the assert spuriously.
Debug.Assert(op == null || Native.ABI.IsFreeThreaded || Refcount(new BorrowedReference(op.Pointer)) > 0);
Debug.Assert(_isInitialized || Py_IsInitialized() != 0 || _Py_IsFinalizing() != false);
#endif
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ internal class TypeManager
internal static readonly ConcurrentDictionary<MaybeType, PyType> cache = new();
internal static readonly object _cacheCreateLock = new();

static readonly Dictionary<PyType, SlotsHolder> _slotsHolders = new(PythonReferenceComparer.Instance);
// Concurrent: documents the multi-writer contract previously enforced
// by callers happening to hold _cacheCreateLock.
static readonly ConcurrentDictionary<PyType, SlotsHolder> _slotsHolders = new(PythonReferenceComparer.Instance);

// Slots which must be set
private static readonly string[] _requiredSlots = new string[]
Expand Down Expand Up @@ -949,7 +951,7 @@ internal static SlotsHolder CreateSlotsHolder(PyType type)
{
type = new PyType(type);
var holder = new SlotsHolder(type);
_slotsHolders.Add(type, holder);
_slotsHolders[type] = holder;
return holder;
}
}
Expand Down