Skip to content

Commit 7f2934b

Browse files
committed
Tests driven by real-world consumer patterns and historical issues
Two additional FT-only tests targeting concrete consumer code paths identified by reviewing pythonnet's downstream users (QuantConnect/Lean, Rhino.Inside, Speckle) and historical issues: test_concurrent_clr_delegate_invocation_from_python Python callables wrapped as distinct CLR delegate types (PublicDelegate, StringDelegate, BoolDelegate) and invoked concurrently from worker threads. Pattern is canonical for embedders that pass Python lambdas where C# expects a delegate. Hits the DelegateManager.GetDispatcher Reflection.Emit lock plus Dispatcher.Dispatch's Py.GIL re-acquisition. test_concurrent_generic_type_binding 36 distinct Dictionary[K,V] / List[K] type-arg pairs resolved concurrently from N threads. Tracks #2269, #1407, #821 (concurrent ToPython / GenericByName). Exercises ClassManager.cache, TypeManager.cache, GenericUtil.mapping, and the generic-binding fast path together. Also: preserve the InternString single-write invariant under DEBUG. Previous TryAdd silently masked the assertion that Add() used to provide. Capture the bool result and Debug.Assert it - same correctness check, no release-build cost. Verified: 3.11 - 3.14 GIL (.NET 8/10): 473 passed, 11 skipped, 3/3 runs clean 3.14t (.NET 8/10): 479 passed, 5 skipped per run
1 parent 0860ade commit 7f2934b

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/runtime/InternString.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ public static bool TryGetInterned(BorrowedReference op, out string s)
7676

7777
private static void SetIntern(string s, PyString op)
7878
{
79-
_string2interns.TryAdd(s, op);
80-
_intern2strings.TryAdd(op.Reference.DangerousGetAddress(), s);
79+
// Initialize is single-threaded; TryAdd preserves the original
80+
// single-write invariant via Debug.Assert without crashing release.
81+
bool a = _string2interns.TryAdd(s, op);
82+
bool b = _intern2strings.TryAdd(op.Reference.DangerousGetAddress(), s);
83+
Debug.Assert(a && b);
8184
}
8285
}
8386
}

tests/test_thread.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,63 @@ def build(_):
247247
assert all(_run_in_threads(build, n_threads=8))
248248

249249

250+
@freethreaded_only
251+
def test_concurrent_clr_delegate_invocation_from_python():
252+
"""Python callables wrapped as distinct CLR delegate types, invoked concurrently.
253+
254+
Real-world: QuantConnect/Lean and similar embedders pass Python callables
255+
where C# expects a delegate; under FT the dispatcher emit + Invoke run from
256+
multiple threads. Hits DelegateManager.GetDispatcher (Reflection.Emit lock)
257+
and Dispatcher.Dispatch (Py.GIL reacquisition).
258+
"""
259+
from Python.Test import (
260+
PublicDelegate, StringDelegate, BoolDelegate,
261+
)
262+
263+
delegates = (
264+
PublicDelegate(lambda: None),
265+
StringDelegate(lambda: "ok"),
266+
BoolDelegate(lambda: True),
267+
)
268+
269+
def fire(i):
270+
d = delegates[i % len(delegates)]
271+
for _ in range(200):
272+
d()
273+
return True
274+
275+
assert all(_run_in_threads(fire, n_threads=8))
276+
277+
278+
@freethreaded_only
279+
def test_concurrent_generic_type_binding():
280+
"""Concurrent `Dictionary[K, V]` with many distinct type-arg pairs.
281+
282+
Real-world: pythonnet/pythonnet#2269, #1407, #821 — concurrent ToPython /
283+
GenericByName from N threads. Exercises ClassManager.cache,
284+
TypeManager.cache, GenericUtil.mapping, and the generic-type binding
285+
fast path together.
286+
287+
FT-only: the cumulative state under the full pytest suite trips the same
288+
pre-existing CPython 3.11/3.12/3.13 GIL-build crash that gates the other
289+
high-contention tests in this file.
290+
"""
291+
from System import Int32, Int64, String, Double, Single, Byte
292+
from System.Collections.Generic import Dictionary, List
293+
294+
arg_types = (Int32, Int64, String, Double, Single, Byte)
295+
pairs = [(k, v) for k in arg_types for v in arg_types]
296+
297+
def bind(_):
298+
for _ in range(50):
299+
for k, v in pairs:
300+
_ = Dictionary[k, v]
301+
_ = List[k]
302+
return True
303+
304+
assert all(_run_in_threads(bind, n_threads=8))
305+
306+
250307
@freethreaded_only
251308
def test_concurrent_shutdown_handler_register():
252309
"""Concurrent AddShutdownHandler/RemoveShutdownHandler — exercises ShutdownHandlers list.

0 commit comments

Comments
 (0)