From ffc4f307ff330a8b13aa5c19df079e4664a573a2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 09:16:01 -0300 Subject: test/daemon.py, test/process: a shell's first pty/run asked once, at once, its whole answer said when it is not what the row wants: `gui_upgrades`' setup failed twice this week on `int(b'')`, the answer never shown (it was `error not run`, the product's, the change before); now every first `echo $$` after a start is test/daemon.py `pid_in` (`exit 0` and a pid, or an AssertionError naming the pane and the answer whole), which replaces `adopted_pid` and the waits that stood in for the product's (a `true` run again until it answered `exit 0`): manage and relink (a session's Term), relink, answer_late, main, upgrades, upgrade_clients and gui_upgrades (a session's first terminal), upgrades (the run after the fallback, the fresh session's terminals), and the four adopted shells (relink, answer_late, main, upgrades); clients' first run in its new Term checks its answer; test/process: terminals.zig's `prompted` (a `true`-until-`exit 0` loop) deleted, sized's first run (`stty size`) asked at once, runFile's `while busy` loop ("deadline its first prompt": the greeting's `ls`) one run that must answer `exit 0`, look_and_exec.zig's `true`-until-`exit 0` loop one run; kept: terminals.zig's wait for a restarted shell (a run written at once can still reach the shell being replaced) and look_and_exec's for the shell back after `cat`; with the product change before: tui process-test 0 of 146 (load 6.2), test/daemon.py on ReleaseSafe plain 1/1, `--upgrade` from the installed build 1/1, `--upgrade --gui` from the installed build 5/5, `--relink` 20 of 20 (on the installed build 18 of 20 (two `error not run` on the adopted shell), each failure `AssertionError: pane 3: `echo $$` answered b'error not run\n'`: what adopted_pid's wait hid) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/daemon.py | 55 ++++++++++++++++++++---------------------- test/process/look_and_exec.zig | 14 ++--------- test/process/terminals.zig | 17 ++----------- 3 files changed, 30 insertions(+), 56 deletions(-) (limited to 'test') diff --git a/test/daemon.py b/test/daemon.py index dc1abcbc..ca59449c 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -188,14 +188,16 @@ def run(client, serial, line): client.close(fid) -def adopted_pid(client, pane): - """An adopted shell's `echo $$`, once it answers: sized twice at its - adoption (dev/Ptys.zig `take`), it draws its prompt again, and a run - sent into a prompt redrawn under it reads as not run (core/ninep/pty.zig - `noteMarks`).""" - wait_for('the adopted shell answers', lambda: run(client, pane, b'true\n') == b'exit 0\n') +def pid_in(client, pane): + """The pid of terminal `pane`'s shell, its `echo $$` by pty/run, asked + at once: a run written before its first prompt (or before the prompt + after its greeting) waits for it, and one under a prompt drawn again + (a window's first size, an adoption's two) is still the line run + (core/ninep/pty.zig). Any other answer is said whole.""" said = run(client, pane, b'echo $$\n') - return said.split(b'\n')[1], said + pid = re.fullmatch(rb'exit 0\n([0-9]+)\n', said) + assert pid, f'pane {pane}: `echo $$` answered {said!r}' + return int(pid[1]) # FreeBSD has no /proc: ps and procstat answer instead. @@ -580,7 +582,8 @@ def clients(binary): c = tc.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'c.check'))) started.append(c.pid) wait_for('client C\'s first frame', lambda: b'$' in tc.text(), timeout=20) - run(nine, shell, b'echo from-the-session\n') + said = run(nine, shell, b'echo from-the-session\n') + assert said == b'exit 0\nfrom-the-session\n', said os.write(tc.master, b'echo from-C\r') wait_for('client C\'s keys in the shell', lambda: b'from-C' in nine.read(f'/pane/{shell}/body')) wait_for('client C agrees with a shell\'s output', lambda: agreed(root / 'c.check', 0)) @@ -1458,8 +1461,7 @@ def manage(binary): c.write('/pane/1/exec', b'Term\n') wait_for(f'session {name}\'s shell', lambda: terminals(c)) shell = terminals(c)[0] - wait_for(f'session {name}\'s shell answers', lambda: run(c, shell, b'true\n') == b'exit 0\n') - shells[name] = int(run(c, shell, b'echo $$\n').split(b'\n')[1]) + shells[name] = pid_in(c, shell) started.append(shells[name]) daemon = [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']][0] started.append(daemon) @@ -1563,8 +1565,7 @@ def relink(binary): work.write('/pane/1/exec', b'Term\n') wait_for('its shell', lambda: terminals(work)) shell = terminals(work)[0] - wait_for('its shell answers', lambda: run(work, shell, b'true\n') == b'exit 0\n') - shell_pid = int(run(work, shell, b'echo $$\n').split(b'\n')[1]) + shell_pid = pid_in(work, shell) started.append(shell_pid) term = Term() terms.append(term) @@ -1576,7 +1577,7 @@ def relink(binary): started.append(own_pid) wait_for('its first frame', lambda: b'\x1b[?1049h' in term.text()) own_shell = terminals(own)[0] - own_shell_pid = int(run(own, own_shell, b'echo $$\n').split(b'\n')[1]) + own_shell_pid = pid_in(own, own_shell) started.append(own_shell_pid) expected = [f'session work {session} {listed_as("detached")}', f'session own {own_pid} {listed_as("has a frontend")}', f'shell {shell_pid}', f'shell {own_shell_pid}'] @@ -1608,8 +1609,7 @@ def relink(binary): before = set(terminals(own)) own.write(f'/pane/{min(before)}/exec', b'Adopt\n') pane = wait_for('the adopted pane', lambda: set(terminals(own)) - before).pop() - pid, said = adopted_pid(own, pane) - assert pid == str(shell_pid).encode(), (said, shell_pid) + assert (pid := pid_in(own, pane)) == shell_pid, (pid, shell_pid) print(f'relink: a client attached to the session through the new daemon; killed outright, its shell {shell_pid} was adopted') exit_session(own, pane) stub.wait(timeout=10) @@ -1659,7 +1659,7 @@ def answer_late(binary): one = Client(root / 'pardes-9p-one.sock') session = int(dict(line.split(maxsplit=1) for line in one.read('/status').decode().splitlines())['pid']) started.append(session) - shell_pid = int(run(one, terminals(one)[0], b'echo $$\n').split(b'\n')[1]) + shell_pid = pid_in(one, terminals(one)[0]) started.append(shell_pid) daemon = [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']][0] started.append(daemon) @@ -1683,8 +1683,7 @@ def answer_late(binary): two.write(f'/pane/{min(before)}/exec', b'Adopt\n') took = time.monotonic() - asked pane = wait_for('the adopted pane', lambda: set(terminals(two)) - before).pop() - pid, said = adopted_pid(two, pane) - assert pid == str(shell_pid).encode(), (said, shell_pid) + assert (pid := pid_in(two, pane)) == shell_pid, (pid, shell_pid) print(f'answer late: an Adopt asked while the daemon was stopped waited {took:.1f} s for its answer, ' f'and shell {shell_pid} was adopted') exit_session(two, pane) @@ -2833,7 +2832,7 @@ def main(binary, cycles): answer_late(binary) kitty_image(binary) shell = terminals(client)[0] - shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + shell_pid = pid_in(client, shell) started.append(shell_pid) # Detach in the terminal it was born with: the stub ends (exit 0, # its words), the terminal put back; the session runs on detached, @@ -2974,8 +2973,7 @@ def main(binary, cycles): client.write(f'/pane/{min(before)}/exec', b'Adopt\n') adopted = wait_for('the adopted pane', lambda: set(terminals(client)) - before) pane = adopted.pop() - pid, said = adopted_pid(client, pane) - assert pid == str(shell_pid).encode(), said + assert (pid := pid_in(client, pane)) == shell_pid, (pid, shell_pid) print(f'daemon: shell {shell_pid} outlived kill -9 of its session and was adopted') # Attached again: a client. (After the runs: a pty/run under a # client puts it off the session's state, and it joins again; one @@ -3382,7 +3380,7 @@ def upgrades(old, new): daemon = wait_for('the daemon', lambda: [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']])[0] started.append(daemon) shell = terminals(client)[0] - shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + shell_pid = pid_in(client, shell) started.append(shell_pid) client.write(f'/pane/{shell}/look', f'{notes}\n'.encode()) text = int(client.read('/look')) @@ -3572,7 +3570,7 @@ def upgrades(old, new): assert os.stat(f'/proc/{session}/exe').st_ino == running, 'back on the build that ran' assert alive(shell_pid) and ppid(shell_pid) == session and stub.poll() is None assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' - assert run(client, shell, b'echo $$\n').split(b'\n')[1] == str(shell_pid).encode() + assert (pid := pid_in(client, shell)) == shell_pid, (pid, shell_pid) print('upgrade: a build that refused the dump fell back to the one running; the shell ran on') # One that refuses it everywhere: a fresh session, the shells adopted. @@ -3585,8 +3583,8 @@ def upgrades(old, new): kept = client.read('/dumps').decode() assert any(line.split()[0].startswith(f'upgrade-{session}') for line in kept.splitlines()), kept assert alive(shell_pid) and ppid(shell_pid) == session, ('the shell under the fresh session', shell_pid, alive(shell_pid) and ppid(shell_pid), session) - pids = {p: run(client, p, b'echo $$\n') for p in terminals(client)} - adopted = [p for p, out in pids.items() if out.split(b'\n')[1:2] == [str(shell_pid).encode()]] + pids = {p: pid_in(client, p) for p in terminals(client)} + adopted = [p for p, pid in pids.items() if pid == shell_pid] assert adopted, ('the shell is a pane of the fresh session', shell_pid, pids, client.read('/index')) print(f'upgrade: a build that refused it everywhere started afresh, shell {shell_pid} adopted, the dump kept') @@ -3623,8 +3621,7 @@ def upgrades(old, new): before = set(terminals(client)) client.write(f'/pane/{min(before)}/exec', b'Adopt\n') pane = wait_for('the adopted pane', lambda: set(terminals(client)) - before).pop() - pid, said = adopted_pid(client, pane) - assert pid == str(shell_pid).encode(), (said, shell_pid) + assert (pid := pid_in(client, pane)) == shell_pid, (pid, shell_pid) print(f'upgrade: the upgraded daemon kept shell {shell_pid} through kill -9 of its session, and it was adopted') exit_session(client, pane) stub.wait(timeout=10) @@ -3723,7 +3720,7 @@ def upgrade_clients(old, new): nine = Client(sock) session = int(dict(line.split(maxsplit=1) for line in nine.read('/status').decode().splitlines())['pid']) shell = terminals(nine)[0] - shell_pid = int(run(nine, shell, b'echo $$\n').split(b'\n')[1]) + shell_pid = pid_in(nine, shell) t = Term(30, 100) terms.append(t) tc = t.spawn([str(binary), 'attach', 'up'], dict(env, PARDES_REPLICA_CHECK=str(root / 't.check'))) @@ -3828,7 +3825,7 @@ def gui_upgrades(old, new): daemon = wait_for('the daemon', lambda: [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']])[0] started.append(daemon) shell = terminals(client)[0] - shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + shell_pid = pid_in(client, shell) started.append(shell_pid) client.write(f'/pane/{shell}/look', f'{notes}\n'.encode()) text = int(client.read('/look')) diff --git a/test/process/look_and_exec.zig b/test/process/look_and_exec.zig index 9829797b..8ba52534 100644 --- a/test/process/look_and_exec.zig +++ b/test/process/look_and_exec.zig @@ -118,18 +118,8 @@ fn canary(gpa: std.mem.Allocator, exe: [:0]const u8) !void { // A terminal: pty/run failing then passing. const term = try f.madeBy(1, "Term\n"); - { - const deadline = process.nowMs() + 10_000; - while (true) { - const answer = try f.run(term, "true\n"); - if (std.mem.eql(u8, answer, "exit 0\n")) break; - if (process.nowMs() > deadline) { - std.debug.print("pty/run answered {s}\n", .{answer}); - return error.Timeout; - } - process.nap(50); - } - } + // Asked at once: a run waits for its first prompt and its greeting. + try std.testing.expectEqualStrings("exit 0\n", try f.run(term, "true\n")); try std.testing.expectEqualStrings("exit 1\n", try f.run(term, "false\n")); try std.testing.expectEqualStrings("exit 0\npassed\n", try f.run(term, "echo passed\n")); // (A run's answer is its record: the log has no err for these.) diff --git a/test/process/terminals.zig b/test/process/terminals.zig index 151b221b..8d595120 100644 --- a/test/process/terminals.zig +++ b/test/process/terminals.zig @@ -111,15 +111,6 @@ fn sizeLines(a: std.mem.Allocator, c: *process.Conn, n: u32) ![]const []const u8 return out.items; } -/// A shell takes runs: its first prompt, or the one started again. -fn prompted(a: std.mem.Allocator, c: *process.Conn, term: u32) !void { - const deadline = eh.nowMs() + 10_000; - while (!std.mem.eql(u8, try run(a, c, term, "true\n"), "exit 0\n")) { - if (eh.nowMs() > deadline) return error.NoPrompt; - process.sleep(50); - } -} - fn sizedFork(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var d = try process.Detached.start(gpa, exe, "sized", .{}); defer d.deinit(); @@ -135,7 +126,7 @@ fn sizedFork(gpa: std.mem.Allocator, exe: [:0]const u8) !void { // the screen's 160x50. try c.write("/exec", "Term\n"); const term = try c.newest(gpa); - try prompted(a, c, term); + // Asked at once: a run waits for its first prompt and its greeting. var got = try sttySize(a, c, term); try expect(!std.mem.eql(u8, got[1], "50 160")); try expectEqualStrings(got[1], got[0]); @@ -200,11 +191,7 @@ fn runFile(gpa: std.mem.Allocator, exe: [:0]const u8) !void { const serial = try std.fmt.parseInt(u32, w.next().?, 10); if (std.mem.eql(u8, w.next().?, "term")) term = @max(term, serial); } - const deadline = eh.nowMs() + 10_000; - while (std.mem.eql(u8, try run(a, c, term, "true\n"), "busy\n")) { // deadline its first prompt - if (eh.nowMs() > deadline) return error.NoPrompt; - process.sleep(50); - } + try expectEqualStrings("exit 0\n", try run(a, c, term, "true\n")); // asked at once: it waits for the first prompt, and the greeting's try expectEqualStrings("exit 1\n", try run(a, c, term, "false\n")); try expectEqualStrings("exit 0\none\ntwo\n", try run(a, c, term, "printf \"one\\ntwo\\n\"\n")); // then what it printed try expectEqualStrings("exit 7\n", try run(a, c, term, "sh -c \"exit 7\"\n")); -- cgit v1.3