@@ -1349,27 +1349,28 @@ def test_pidfd_closed_when_waitpid_raises(self):
13491349 # _do_wait() must close the pidfd even when waitpid()
13501350 # fails with something other than ChildProcessError, otherwise the
13511351 # pidfd is leaked
1352- watcher = unix_events ._PidfdChildWatcher ()
1353- pid = os .posix_spawn (sys .executable , [sys .executable , '-c' , '' ],
1354- os .environ )
1355- pidfd = os .pidfd_open (pid )
1356- try :
1357- async def coro ():
1358- with mock .patch .object (os , 'waitpid' ,
1359- side_effect = OSError ('unexpected' )):
1360- with self .assertRaises (OSError ):
1361- watcher ._do_wait (pid , pidfd , lambda * a : None , ())
1352+ self .loop .set_exception_handler (lambda loop , context : None )
13621353
1363- self .loop .run_until_complete (coro ())
1354+ async def coro ():
1355+ before = os_helper .fd_count ()
1356+ proc = await asyncio .create_subprocess_exec (
1357+ sys .executable , '-c' , 'import sys; sys.stdin.read()' ,
1358+ stdin = asyncio .subprocess .PIPE
1359+ )
13641360
1365- with self .assertRaises (OSError ):
1366- os .fstat (pidfd )
1367- finally :
1368- try :
1369- os .close (pidfd )
1370- except OSError :
1371- pass
1372- os .waitpid (pid , 0 )
1361+ with mock .patch .object (os , 'waitpid' ,
1362+ side_effect = OSError ('unexpected' )) as m :
1363+ proc .stdin .close ()
1364+ while not m .called :
1365+ await asyncio .sleep (0 )
1366+
1367+ os .waitpid (proc .pid , 0 )
1368+ proc ._transport ._process_exited (0 )
1369+ await proc .wait ()
1370+
1371+ self .assertEqual (os_helper .fd_count (), before )
1372+
1373+ self .loop .run_until_complete (coro ())
13731374
13741375
13751376if __name__ == '__main__' :
0 commit comments