Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98fa332

Browse files
committed
Issue #21247: Fix a race condition in test_send_signal() of asyncio
Add a basic synchronization mechanism to wait until the child process is ready before sending it a signal.
1 parent fe5649c commit 98fa332

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

Lib/test/test_asyncio/test_subprocess.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,22 @@ def test_terminate(self):
108108

109109
@unittest.skipIf(sys.platform == 'win32', "Don't have SIGHUP")
110110
def test_send_signal(self):
111-
args = PROGRAM_BLOCKED
112-
create = asyncio.create_subprocess_exec(*args, loop=self.loop)
111+
code = 'import time; print("sleeping", flush=True); time.sleep(3600)'
112+
args = [sys.executable, '-c', code]
113+
create = asyncio.create_subprocess_exec(*args, loop=self.loop, stdout=subprocess.PIPE)
113114
proc = self.loop.run_until_complete(create)
114-
proc.send_signal(signal.SIGHUP)
115-
returncode = self.loop.run_until_complete(proc.wait())
115+
116+
@asyncio.coroutine
117+
def send_signal(proc):
118+
# basic synchronization to wait until the program is sleeping
119+
line = yield from proc.stdout.readline()
120+
self.assertEqual(line, b'sleeping\n')
121+
122+
proc.send_signal(signal.SIGHUP)
123+
returncode = (yield from proc.wait())
124+
return returncode
125+
126+
returncode = self.loop.run_until_complete(send_signal(proc))
116127
self.assertEqual(-signal.SIGHUP, returncode)
117128

118129
def prepare_broken_pipe_test(self):

0 commit comments

Comments
 (0)