@@ -3590,25 +3590,33 @@ def test_communicate_BrokenPipeError_stdin_close_with_timeout(self):
35903590 proc .communicate (timeout = 999 )
35913591 mock_proc_stdin .close .assert_called_once_with ()
35923592
3593- def test_communicate_timeout_stdin_spuriously_writable (self ):
3594- # gh-162016: poll() can report a pipe as writable when it has no
3595- # free space . communicate () must not then block in write() past
3596- # its timeout .
3593+ def _spuriously_writable_selector (self ):
3594+ # A selector that reports stdin as writable whenever it is
3595+ # registered . poll () on macOS and FreeBSD can do this for a full
3596+ # pipe when many pipes are in use system-wide (gh-162016) .
35973597 class AlwaysWritableSelector (subprocess ._PopenSelector ):
3598+ spurious_events = 0
3599+
35983600 def select (self , timeout = None ):
3599- ready = super ().select (0.01 )
3600- fds = {key .fd for key , _ in ready }
3601+ ready = super ().select (0 )
3602+ writable = {key .fd for key , events in ready
3603+ if events & selectors .EVENT_WRITE }
36013604 for key in self .get_map ().values ():
36023605 if (key .events & selectors .EVENT_WRITE
3603- and key .fd not in fds ):
3606+ and key .fd not in writable ):
36043607 ready .append ((key , selectors .EVENT_WRITE ))
3605- return ready
3608+ type (self ).spurious_events += 1
3609+ return ready or super ().select (timeout )
36063610
3607- # The child does not read stdin, so the pipe fills up. A blocking
3608- # write() would not return until the child exits.
3611+ return AlwaysWritableSelector
3612+
3613+ def test_communicate_timeout_stdin_spuriously_writable (self ):
3614+ # The child never reads stdin, so the pipe fills up. communicate()
3615+ # must neither block in write() past its timeout nor spin on a pipe
3616+ # that keeps being reported as writable.
3617+ selector = self ._spuriously_writable_selector ()
36093618 sleep = support .SHORT_TIMEOUT
3610- with mock .patch .object (subprocess , '_PopenSelector' ,
3611- AlwaysWritableSelector ), \
3619+ with mock .patch .object (subprocess , '_PopenSelector' , selector ), \
36123620 subprocess .Popen ([sys .executable , '-c' ,
36133621 f'import time; time.sleep({ sleep } )' ],
36143622 stdin = subprocess .PIPE ,
@@ -3617,12 +3625,39 @@ def select(self, timeout=None):
36173625 start = time .monotonic ()
36183626 with self .assertRaises (subprocess .TimeoutExpired ):
36193627 proc .communicate (b'x' * support .PIPE_MAX_SIZE ,
3620- timeout = 0.1 )
3628+ timeout = 0.5 )
36213629 self .assertLess (time .monotonic () - start , sleep )
3630+ # communicate() must wait between attempts to write to a
3631+ # full pipe. Retrying in a busy loop would produce many
3632+ # thousands of these events within the timeout.
3633+ self .assertLess (selector .spurious_events , 100 )
36223634 self .assertTrue (os .get_blocking (proc .stdin .fileno ()))
36233635 finally :
36243636 proc .kill ()
36253637
3638+ def test_communicate_stdin_spuriously_writable_last_pipe (self ):
3639+ # All input must still arrive when stdin is full, is reported as
3640+ # writable, and is the only pipe communicate() has left to service.
3641+ selector = self ._spuriously_writable_selector ()
3642+ data = bytes (range (256 )) * (support .PIPE_MAX_SIZE // 256 )
3643+ code = textwrap .dedent (f"""
3644+ import os, sys, time
3645+ os.close(1)
3646+ time.sleep(0.1) # Let the parent fill the stdin pipe.
3647+ data = bytes(range(256)) * { len (data ) // 256 }
3648+ sys.exit(sys.stdin.buffer.read() != data)
3649+ """ )
3650+ with mock .patch .object (subprocess , '_PopenSelector' , selector ), \
3651+ subprocess .Popen ([sys .executable , '-c' , code ],
3652+ stdin = subprocess .PIPE ,
3653+ stdout = subprocess .PIPE ) as proc :
3654+ try :
3655+ proc .communicate (data , timeout = support .SHORT_TIMEOUT )
3656+ self .assertEqual (proc .returncode , 0 )
3657+ self .assertGreater (selector .spurious_events , 0 )
3658+ finally :
3659+ proc .kill ()
3660+
36263661 @unittest .skipUnless (_testcapi is not None
36273662 and hasattr (_testcapi , 'W_STOPCODE' ),
36283663 'need _testcapi.W_STOPCODE' )
0 commit comments