@@ -412,37 +412,36 @@ def test_wait_even_if_pipe_is_open(self):
412412 # despite the returncode being known.
413413
414414 async def run ():
415- # Just setup a pipe to pass to the grandchild for reading to ensure it dies.
416- # Inheritable is to allow it to be passed on windows
417- r , w = os .pipe ()
418- os .set_inheritable (r , True )
419-
420- code = textwrap .dedent (f"""\
415+ # The grandchild inherits the child's stdin and stdout pipes and
416+ # keeps both open after the child is killed. It writes "ready"
417+ # so we know it has started, and exits once its stdin hits EOF.
418+ code = textwrap .dedent ("""\
421419 import subprocess, sys
422- subprocess.run([sys.executable, "-c", "import sys;sys.stdin.read()"])
420+ subprocess.run([sys.executable, "-c",
421+ "import sys; sys.stdout.write('ready');"
422+ " sys.stdout.flush(); sys.stdin.read()"])
423423 """ )
424424
425425 proc = await asyncio .create_subprocess_exec (
426426 sys .executable , "-c" , code ,
427- # This will be inherited by granchild and should not prevent
428- # *this* process from firing .wait().
427+ stdin = subprocess .PIPE ,
429428 stdout = subprocess .PIPE ,
430- stdin = r ,
431- pass_fds = (r ,) if sys .platform != "win32" else (),
432- close_fds = False if sys .platform == "win32" else True ,
433429 )
434- os .close (r )
435-
436430 try :
437- # Ensure we start waiting before the process is killed.
438431 wait_proc = asyncio .create_task (proc .wait ())
439- await asyncio .sleep (0 )
432+ # Wait until the grandchild holds the inherited pipes; this
433+ # also lets the wait() task register its waiter.
434+ await proc .stdout .readexactly (5 )
440435 proc .kill ()
441- await asyncio .wait_for (wait_proc , timeout = support .SHORT_TIMEOUT )
436+ returncode = await asyncio .wait_for (
437+ wait_proc , timeout = support .SHORT_TIMEOUT )
438+ if sys .platform == 'win32' :
439+ self .assertIsInstance (returncode , int )
440+ else :
441+ self .assertEqual (- signal .SIGKILL , returncode )
442442 finally :
443- os .close (w ) # Allows the grandchild to exit
444- if proc .stdout is not None :
445- await proc .stdout .read ()
443+ proc .stdin .close () # let the grandchild exit
444+ await proc .stdout .read ()
446445
447446 self .loop .run_until_complete (run ())
448447
0 commit comments