From 541ebfa62ed966d74e1b7e93eb0d62e536d50e7d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 05:54:59 -0300 Subject: test/daemon.py: a Term stops its reader before it closes the pty (a master closed under a blocked read stays open until the read returns, so the terminal never hung up and its stub waited; its fd's number, taken again, was read by the old thread); the stub's last words waited for rather than read at once; a client's next keys wait until its Esc has taken it out of insert mode (written behind the Esc, they came in one read with it as Alt on the first): the three that failed one run in ten or more under load Co-Authored-By: Claude Opus 5.5 --- test/daemon.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/test/daemon.py b/test/daemon.py index 10896194..8b0a20bd 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -26,6 +26,7 @@ private HOME and runtime directory: """ import fcntl import os +import select import shutil import signal import socket @@ -63,12 +64,18 @@ class Term: fcntl.ioctl(self.slave, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0)) self.out = bytearray() self.lock = threading.Lock() + self.closing = False self.reader = threading.Thread(target=self.drain, daemon=True) self.reader.start() def drain(self): - while True: + # Never blocked in a read when `close` comes: a master closed under a + # read stays open until it returns, so the terminal would not hang + # up, and its fd's number, taken again, would be read here. + while not self.closing: try: + if not select.select([self.master], [], [], 0.05)[0]: + continue got = os.read(self.master, 65536) except OSError: return @@ -93,9 +100,10 @@ class Term: return bytes(self.out) def close(self): + self.closing = True + self.reader.join(timeout=2) os.close(self.slave) os.close(self.master) - self.reader.join(timeout=2) def run(client, serial, line): @@ -284,6 +292,14 @@ def checks(path): return [] +def normal_again(term): + """The client's cursor a block again after its beam: the Esc it was sent + ended insert mode. A key written before that can reach it in one read + with the Esc, which is then Alt on that key.""" + t = term.text() + return t.rfind(b'\x1b[0 q') > t.rfind(b'\x1b[6 q') + + def agreed(path, after): """A check after the first `after` agreed; one that differed fails.""" lines = checks(path) @@ -335,6 +351,7 @@ def clients(binary): os.write(tb.master, b'jjofrom-B\x1b') wait_for('both clients\' keys in the session\'s text', lambda: b'from-A' in nine.read(f'/pane/{pane}/body') and b'from-B' in nine.read(f'/pane/{pane}/body')) + wait_for('both clients out of insert mode', lambda: normal_again(ta) and normal_again(tb)) wait_for('client A agrees once quiet', lambda: agreed(root / 'a.check', 0)) wait_for('client B agrees once quiet', lambda: agreed(root / 'b.check', 0)) # A terminal attached while a client's view is live (its input the @@ -363,6 +380,7 @@ def clients(binary): seen = len(checks(root / 'd.check')) os.write(td.master, b'Oafter-rejoin-D\x1b') wait_for('client D\'s keys after its rejoin', lambda: b'after-rejoin-D' in nine.read(f'/pane/{pane}/body')) + wait_for('client D out of insert mode', lambda: normal_again(td)) wait_for('client D agrees after its rejoin', lambda: agreed(root / 'd.check', seen)) os.write(td.master, b' sD') wait_for('client D let go', lambda: d.poll() is not None, timeout=10) @@ -372,6 +390,7 @@ def clients(binary): seen = len(checks(root / 'a.check')) os.write(ta.master, b'jjjAagain-A\x1b') wait_for('client A\'s keys after B went', lambda: b'again-A' in nine.read(f'/pane/{pane}/body')) + wait_for('client A out of insert mode', lambda: normal_again(ta)) wait_for('client A agrees again', lambda: agreed(root / 'a.check', seen)) assert sess.poll() is None # Detach typed in it: it alone goes. @@ -466,8 +485,10 @@ def main(binary, cycles): for cycle in range(cycles): client.write(f'/pane/{shell}/exec', b'Detach\n') assert stub.wait(timeout=10) == 0, stub.returncode + # The stub's last words are in its terminal before it ends, but + # this side's reader may not have them yet. + wait_for('the stub says it detached', lambda: b'detached from \'work\'' in term.text()) said = term.text() - assert b'detached from \'work\'' in said, said[-400:] assert b'\x1b[?1049l' in said, 'the terminal is put back' term.close() assert alive(session) and alive(shell_pid) @@ -503,7 +524,7 @@ def main(binary, cycles): # Its view is the live one (its terminal spoke last). client.write(f'/pane/{shell}/exec', b'Detach\n') assert stub2.wait(timeout=10) == 0, stub2.returncode - assert b'detached from \'work\'' in term2.text(), term2.text()[-400:] + wait_for('the second stub says it detached', lambda: b'detached from \'work\'' in term2.text()) term2.close() else: term2.close() @@ -653,6 +674,7 @@ def gui(binary, cycles): term = Term() tty_attach = term.spawn([binary.replace('pardes-gui', 'pardes'), '--attach=win'], env) assert tty_attach.wait(timeout=10) == 1 + wait_for('the refusal in the terminal', lambda: b'is a window' in term.text()) said = term.text() term.close() assert b'is a window' in said, said -- cgit v1.3