|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using NUnit.Framework; |
| 4 | +using Python.Runtime; |
| 5 | + |
| 6 | +namespace Python.EmbeddingTest |
| 7 | +{ |
| 8 | + public class TestLazyPyMembers |
| 9 | + { |
| 10 | + public static class OverloadedMethods |
| 11 | + { |
| 12 | + public static string Call(string value) => "string"; |
| 13 | + public static string Call(object value) => "object"; |
| 14 | + } |
| 15 | + |
| 16 | + [OneTimeSetUp] |
| 17 | + public void SetUp() |
| 18 | + { |
| 19 | + PythonEngine.Initialize(); |
| 20 | + } |
| 21 | + |
| 22 | + [OneTimeTearDown] |
| 23 | + public void Dispose() |
| 24 | + { |
| 25 | + PythonEngine.Shutdown(); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Resolving PyDecimalType imports _pydecimal, which runs Python code that can hand the GIL |
| 30 | + /// to another thread. A thread that reaches PyDecimalType meanwhile must not block on it |
| 31 | + /// while holding the GIL, or the importing thread can never finish. |
| 32 | + /// </summary> |
| 33 | + [Test] |
| 34 | + public void ConcurrentFirstResolutionOfDecimalTypeDoesNotDeadlock() |
| 35 | + { |
| 36 | + using var scope = Py.CreateScope(); |
| 37 | + scope.Exec(@" |
| 38 | +import sys |
| 39 | +import threading |
| 40 | +from Python.EmbeddingTest import TestLazyPyMembers |
| 41 | +OverloadedMethods = TestLazyPyMembers.OverloadedMethods |
| 42 | +
|
| 43 | +importing = threading.Event() |
| 44 | +
|
| 45 | +class SlowDecimalImport: |
| 46 | + def find_spec(self, name, path, target=None): |
| 47 | + if name == '_pydecimal': |
| 48 | + importing.set() |
| 49 | + # keep the import open, with the GIL released, until the other thread reaches PyDecimalType |
| 50 | + threading.Event().wait(1) |
| 51 | + return None |
| 52 | +
|
| 53 | +finder = SlowDecimalImport() |
| 54 | +sys.modules.pop('_pydecimal', None) |
| 55 | +sys.meta_path.insert(0, finder) |
| 56 | +"); |
| 57 | + Assert.IsFalse(Runtime.Runtime.PyDecimalType.IsValueCreated); |
| 58 | + |
| 59 | + var results = new string[2]; |
| 60 | + var errors = new Exception[2]; |
| 61 | + Thread Start(int index, string code) => new Thread(() => |
| 62 | + { |
| 63 | + try |
| 64 | + { |
| 65 | + using (Py.GIL()) |
| 66 | + { |
| 67 | + results[index] = scope.Eval<string>(code); |
| 68 | + } |
| 69 | + } |
| 70 | + catch (Exception e) |
| 71 | + { |
| 72 | + errors[index] = e; |
| 73 | + } |
| 74 | + }); |
| 75 | + var first = Start(0, "OverloadedMethods.Call([])"); |
| 76 | + var second = Start(1, "importing.wait(10) and OverloadedMethods.Call([])"); |
| 77 | + |
| 78 | + // the fixture initializes the engine with the GIL held on this thread: |
| 79 | + // release it so the worker threads can acquire it |
| 80 | + var ts = PythonEngine.BeginAllowThreads(); |
| 81 | + try |
| 82 | + { |
| 83 | + second.Start(); |
| 84 | + first.Start(); |
| 85 | + Assert.IsTrue(first.Join(TimeSpan.FromSeconds(30)), "first thread deadlocked"); |
| 86 | + Assert.IsTrue(second.Join(TimeSpan.FromSeconds(30)), "second thread deadlocked"); |
| 87 | + } |
| 88 | + finally |
| 89 | + { |
| 90 | + PythonEngine.EndAllowThreads(ts); |
| 91 | + } |
| 92 | + scope.Exec("sys.meta_path.remove(finder)"); |
| 93 | + |
| 94 | + Assert.IsNull(errors[0]); |
| 95 | + Assert.IsNull(errors[1]); |
| 96 | + Assert.AreEqual("object", results[0]); |
| 97 | + Assert.AreEqual("object", results[1]); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments