]> granicus.if.org Git - python/commitdiff
Issue #21247: Fix a race condition in test_send_signal() of asyncio
authorVictor Stinner <victor.stinner@gmail.com>
Thu, 17 Jul 2014 21:49:11 +0000 (23:49 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Thu, 17 Jul 2014 21:49:11 +0000 (23:49 +0200)
Add a basic synchronization mechanism to wait until the child process is ready
before sending it a signal.

Lib/test/test_asyncio/test_subprocess.py

index d050458e7fff5eac4d800e1d3acfe20661295812..5425d9bfd5ebdfd7368900d40e45b1a3ee400678 100644 (file)
@@ -108,11 +108,22 @@ class SubprocessMixin:
 
     @unittest.skipIf(sys.platform == 'win32', "Don't have SIGHUP")
     def test_send_signal(self):
-        args = PROGRAM_BLOCKED
-        create = asyncio.create_subprocess_exec(*args, loop=self.loop)
+        code = 'import time; print("sleeping", flush=True); time.sleep(3600)'
+        args = [sys.executable, '-c', code]
+        create = asyncio.create_subprocess_exec(*args, loop=self.loop, stdout=subprocess.PIPE)
         proc = self.loop.run_until_complete(create)
-        proc.send_signal(signal.SIGHUP)
-        returncode = self.loop.run_until_complete(proc.wait())
+
+        @asyncio.coroutine
+        def send_signal(proc):
+            # basic synchronization to wait until the program is sleeping
+            line = yield from proc.stdout.readline()
+            self.assertEqual(line, b'sleeping\n')
+
+            proc.send_signal(signal.SIGHUP)
+            returncode = (yield from proc.wait())
+            return returncode
+
+        returncode = self.loop.run_until_complete(send_signal(proc))
         self.assertEqual(-signal.SIGHUP, returncode)
 
     def prepare_broken_pipe_test(self):