Skip to content

Commit 44858ec

Browse files
committed
Fix flakey interactive test
The PTY drain loop in the Ctrl-C test had a race: after sending the keystroke, the parent only drained for 0.5s before calling os.waitpid. If the child was still writing to the PTY after that window closed, it would block on a full PTY buffer, deadlocking waitpid. Moving the drain into a background thread keeps the buffer clear while waitpid runs.
1 parent 453468d commit 44858ec

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

test/hdi.bats

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -838,23 +838,31 @@ else:
838838
local keys=$'\x03'
839839

840840
python3 -c "
841-
import pty, os, sys, time, select
841+
import pty, os, sys, time, select, threading
842842
843843
keys = sys.argv[1].encode()
844844
845845
pid, fd = pty.fork()
846846
if pid == 0:
847847
os.execvp(sys.argv[2], sys.argv[2:])
848848
else:
849+
# Drain PTY output continuously in a background thread to prevent
850+
# the child from blocking on a full PTY buffer (especially on macOS)
851+
def drain():
852+
try:
853+
while True:
854+
if not select.select([fd], [], [], 5.0)[0]:
855+
break
856+
if not os.read(fd, 4096):
857+
break
858+
except OSError:
859+
pass
860+
t = threading.Thread(target=drain, daemon=True)
861+
t.start()
862+
849863
time.sleep(0.5)
850864
os.write(fd, keys)
851-
time.sleep(0.5)
852-
try:
853-
while select.select([fd], [], [], 0.5)[0]:
854-
if not os.read(fd, 4096):
855-
break
856-
except OSError:
857-
pass
865+
858866
_, status = os.waitpid(pid, 0)
859867
sys.exit(os.waitstatus_to_exitcode(status))
860868
" "$keys" "$HDI" "$FIXTURES/node-express"

0 commit comments

Comments
 (0)