]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
To fix the random failed test cases of test___xxsubinterpreters in multiprocess....
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 29 Jul 2021 17:28:22 +0000 (10:28 -0700)
committerGitHub <noreply@github.com>
Thu, 29 Jul 2021 17:28:22 +0000 (19:28 +0200)
(cherry picked from commit 9101b39e67c2437e88c0ad6b57aafd48ab08d431)

Co-authored-by: Hai Shi <shihai1992@gmail.com>
Lib/test/test__xxsubinterpreters.py

index 7aec021fb19a5e6707c129a5cb9c6aad8a0ce441..cbfa881818bc6ab483966b37da36e4e4019e4520 100644 (file)
@@ -38,6 +38,20 @@ def _run_output(interp, request, shared=None):
         return rpipe.read()
 
 
+def _wait_for_interp_to_run(interp, timeout=None):
+    # bpo-37224: Running this test file in multiprocesses will fail randomly.
+    # The failure reason is that the thread can't acquire the cpu to
+    # run subinterpreter eariler than the main thread in multiprocess.
+    if timeout is None:
+        timeout = support.SHORT_TIMEOUT
+    start_time = time.monotonic()
+    deadline = start_time + timeout
+    while not interpreters.is_running(interp):
+        if time.monotonic() > deadline:
+            raise RuntimeError('interp is not running')
+        time.sleep(0.010)
+
+
 @contextlib.contextmanager
 def _running(interp):
     r, w = os.pipe()
@@ -50,6 +64,7 @@ def _running(interp):
 
     t = threading.Thread(target=run)
     t.start()
+    _wait_for_interp_to_run(interp)
 
     yield