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
Add concurrent stress tests for PyBuffer.Dispose and CLR-cycle gc.col…
…lect
  • Loading branch information
greateggsgreg authored and filmor committed Aug 18, 2026
commit a4597725f3e13c73ff12930a7aef922b6276e9af
37 changes: 37 additions & 0 deletions src/embed_tests/TestPyBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using NUnit.Framework;
using Python.Runtime;
using Python.Runtime.Codecs;
Expand Down Expand Up @@ -129,6 +130,42 @@ public void MultidimensionalNumPyArray()
});
}

[Test]
public void ConcurrentDispose()
{
// Two threads racing on Dispose() must not double-release the view —
// Interlocked.Exchange on disposedValue gates PyBuffer_Release.
// Smoke test: no crash, exception, or buffer-protocol violation.
using var _ = Py.GIL();
using var arr = ByteArrayFromAsciiString("hello world! !$%&/()=?");

const int iterations = 200;
for (int i = 0; i < iterations; i++)
{
PyBuffer buf = arr.GetBuffer();

IntPtr ts = PythonEngine.BeginAllowThreads();
using var barrier = new Barrier(2);
Exception captured = null;
Action race = () =>
{
try
{
barrier.SignalAndWait();
using (Py.GIL()) buf.Dispose();
}
catch (Exception ex) { Interlocked.CompareExchange(ref captured, ex, null); }
};
var t1 = new Thread(() => race());
var t2 = new Thread(() => race());
t1.Start(); t2.Start();
t1.Join(); t2.Join();
PythonEngine.EndAllowThreads(ts);

if (captured != null) throw captured;
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void MakeBufAndLeak(PyObject bufProvider)
{
Expand Down
27 changes: 27 additions & 0 deletions tests/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,30 @@ def churn(i):
return True

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


@pytest.mark.skipif(_gil_enabled(), reason="Only meaningful on free-threaded Python (Py_GIL_DISABLED).")
def test_concurrent_gc_collect_on_clr_cycles():
"""Concurrent gc.collect on cyclic CLR-derived objects — exercises
ClassBase.ClearVisited + ManagedType.TryFreeGCHandle atomic slot.

Each worker builds short cycles holding a Python subclass of System.Object,
then calls gc.collect() while other workers do the same. Hits the
tp_clear/tp_dealloc race path on the per-object GCHandle slot.
"""
import gc
import System

class Cycle(System.Object):
__namespace__ = "test_concurrent_gc_collect_on_clr_cycles"

def churn(_):
for _ in range(100):
a, b = Cycle(), Cycle()
a.peer = b
b.peer = a
del a, b
gc.collect()
return True

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