@@ -499,6 +499,7 @@ def test_pipeline_writer_relay_lends_only_while_a_producer_waits(pauses) -> None
499499 first_read = len (payload ) - capacity - 16384 if pauses else len (payload )
500500 received = bytearray ()
501501 lent = threading .Event ()
502+ paused = threading .Event ()
502503 resume = threading .Event ()
503504 lends = []
504505
@@ -513,11 +514,18 @@ def _lend_terminal():
513514
514515 def drain () -> None :
515516 # Like a pager waiting for a key, drain nothing until lent the terminal. Then read a page, or all of it, and wait for
516- # the next key.
517- lent .wait (10 )
517+ # the next key. Neither wait has a timeout, which would let a slow test read ahead unnoticed: the test's cleanup
518+ # releases both.
519+ lent .wait ()
518520 while len (received ) < first_read :
519- received .extend (os .read (read_fd , min (65536 , first_read - len (received ))))
520- resume .wait (10 )
521+ chunk = os .read (read_fd , min (65536 , first_read - len (received )))
522+ if not chunk :
523+ # End of file arrives only once the test's cleanup closes the writer. Reaching it here means the output fell
524+ # short, so stop instead of spinning on empty reads.
525+ return
526+ received .extend (chunk )
527+ paused .set ()
528+ resume .wait ()
521529 while chunk := os .read (read_fd , 65536 ):
522530 received .extend (chunk )
523531
@@ -539,11 +547,15 @@ def drain() -> None:
539547 time .sleep (0.01 )
540548 assert not lends
541549 if pauses :
542- assert len (received ) == first_read
550+ # The child can exit before the consumer has read all of its first page, so give it time. Its reads stop at that
551+ # page until resumed, so the rest is still in the relay.
552+ assert paused .wait (5 ), f"the consumer read { len (received )} of its first { first_read } bytes in time"
543553 # cmd2's own writes follow what the child wrote, which is still pending.
544554 threading .Timer (0.3 , resume .set ).start ()
545555 assert writer .write (b"end" ) == 3
546556 finally :
557+ # Release the consumer wherever it waits, so a test that fails before a lend does not hold up the join.
558+ lent .set ()
547559 resume .set ()
548560 writer .close ()
549561 consumer .join (10 )
0 commit comments