diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-22 14:01:03 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-22 14:01:03 -0300 |
| commit | 85e793ae92683808d14433cb7c99ccdab4c967dc (patch) | |
| tree | 65aad3f6c0bb94f27e462ab3e75a55dad6112dd4 /test/daemon.py | |
| parent | 8caed8e7d894fee836c95f2ca83fda9b22e1db84 (diff) | |
| download | pardes-85e793ae92683808d14433cb7c99ccdab4c967dc.tar.gz pardes-85e793ae92683808d14433cb7c99ccdab4c967dc.zip | |
daemon.py: a terminal read after its writer ended waits for its last bytes, kept_views waits for the session to take the goodbyes it races, and far_flood keeps its evidence (pardes-term-join-9, item 1 (b), (a), (c)): `Term.last` reads a terminal's text once nothing waits on its master (FIONREAD, read under the reader's lock, which now reads and keeps in one hold) and nothing more came for 0.1 s, and the four reads that followed an exit or an event seen elsewhere at once use it (clients' detach words, the failing row R-plain-2 at load 7.6; a killed stub's put-back; "let this client go" not said, a negative that passed early; the old build's replica told to upgrade too); kept_views dials again until a refusal says a view is kept (two raw replicas' byes reach the session on their own connections and the next join through the daemon, in no order the session could keep: D-plain-3 and R-plain-1 of round 8) and waits the seconds that refusal states, not a fixed 31 from before the goodbyes were taken; far_flood copies the far client's check and stats files, its terminal and every process's threads with their wait channels beside its root at any failure (R--far-6 of round 8, not reproduced in 13 runs since, went with its root)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test/daemon.py')
| -rw-r--r-- | test/daemon.py | 83 |
1 files changed, 68 insertions, 15 deletions
diff --git a/test/daemon.py b/test/daemon.py index c5c39be3..55cffaf4 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -102,13 +102,15 @@ class Term: try: if not select.select([self.master], [], [], 0.05)[0]: continue - got = os.read(self.master, 65536) + # Read and kept in one hold of the lock: `last` counts what + # waits on the master under it. + with self.lock: + got = os.read(self.master, 65536) + self.out += got except OSError: return if not got: return - with self.lock: - self.out += got # A request split across two reads is answered too. seen, self.tail = self.tail + got, got[-3:] if b'\x1b[5n' in seen: @@ -127,6 +129,23 @@ class Term: with self.lock: return bytes(self.out) + def last(self): + """Its text once what was written to it is read, for a read after + its writer ended (or wrote its last before an event seen + elsewhere): the reader may not have the last bytes yet (it reads + every 50 ms, later under load). Nothing waiting on the master and + nothing more read for 0.1 s.""" + quiet = None + def done(): + nonlocal quiet + with self.lock: + now = (struct.unpack('i', fcntl.ioctl(self.master, termios.FIONREAD, b'\0\0\0\0'))[0], len(self.out)) + if now[0] or quiet is None or quiet[0] != now: + quiet = (now, time.monotonic()) + return time.monotonic() - quiet[1] >= .1 + wait_for('the terminal\'s last bytes read', done) + return self.text() + def close(self): self.closing = True self.reader.join(timeout=2) @@ -523,7 +542,7 @@ def clients(binary): # Detach typed in it: it alone goes, and says so (exit 0). os.write(ta.master, b' sD') wait_for('client A let go', lambda: a.poll() is not None, timeout=10) - assert a.returncode == 0 and b"detached from 'files', which is still running" in ta.text(), ta.text()[-300:] + assert a.returncode == 0 and b"detached from 'files', which is still running" in ta.last(), ta.last()[-300:] # A third client, a shell: its keys wait for the session (a pty # write), its output comes back in units, the same on both sides. nine.write(f'/pane/{pane}/exec', b'Term\n') @@ -996,7 +1015,7 @@ def kept_views(binary): nobody types in: a `new --detach` session at its four views (a client attached, idle; raw replicas), two of them leaving to join again and never coming back; a join at once is refused and told for how long, one - 31 s later, nothing typed in between, is welcomed.""" + a second past that, nothing typed in between, is welcomed.""" scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' scratch.mkdir(parents=True, exist_ok=True) root = Path(tempfile.mkdtemp(prefix='rk-', dir=scratch)) @@ -1023,15 +1042,29 @@ def kept_views(binary): for r in rs[:2]: r.send(0x02, b'\x01') r.close() - left = time.monotonic() - said = Replica(sock).hello('kv', build) - assert said[0] == 'refused' and re.search(r'as many views as it takes \(one is kept (\d+) s more for a client joining again\)', said[1]), said - print(f'kept views: {len(rs)} replicas and a client at the cap; two left to join again; a join at once refused: "{said[1]}"') - time.sleep(max(0, left + 31 - time.monotonic())) + + # The goodbyes reach the session on their own connections and the + # next join through the daemon, in no order between them: a join + # judged before the session took them is refused with no view kept + # yet. Dialled again until a refusal says one is. + dials = 0 + + def refused(): + nonlocal dials + dials += 1 + r = Replica(sock) + said = r.hello('kv', build) + r.close() + assert said[0] == 'refused' and 'as many views as it takes' in said[1], said + kept = re.search(r'as many views as it takes \(one is kept (\d+) s more for a client joining again\)', said[1]) + return kept and (said, int(kept.group(1)), time.monotonic()) + said, more, at = wait_for('a join refused, a view kept', refused) + print(f'kept views: {len(rs)} replicas and a client at the cap; two left to join again; a join at once refused: "{said[1]}" ({dials} dials)') + time.sleep(max(0, at + more + 1 - time.monotonic())) said = Replica(sock).hello('kv', build) - assert said[0] == 'welcome', ('a view kept 31 s in an idle session, still kept', said) + assert said[0] == 'welcome', ('a view kept past what its refusal said in an idle session, still kept', more, said) assert a.poll() is None - print('kept views: a join 31 s after, nothing typed in between, took a kept view') + print(f'kept views: a join {more + 1} s after, nothing typed in between, took a kept view') a.terminate() a.wait(timeout=20) assert subprocess.run([binary, 'kill', 'kv'], env=env, capture_output=True, timeout=30).returncode == 0 @@ -2211,6 +2244,25 @@ def far_flood(binary): sess.wait(timeout=60) nine.socket.close() wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=10) + except BaseException: + # Its evidence kept, the root going: the far client's checks and + # stats, its terminal, and where each process's threads wait. + kept = Path(tempfile.mkdtemp(prefix='fl-kept-', dir=scratch)) + for f in ('f.check', 'f.stats'): + if (root / f).exists(): + shutil.copy(root / f, kept) + for i, term in enumerate(terms): + (kept / f'term-{i}.out').write_bytes(term.text()) + with open(kept / 'threads', 'w') as out: + for pid in started: + out.write(f'{pid} {cmdline(pid)[:3]} alive={alive(pid)}\n') + for task in sorted(Path(f'/proc/{pid}/task').glob('*')) if procfs and alive(pid) else []: + try: + out.write(f' {task.name} {(task / "comm").read_text().strip()} {(task / "wchan").read_text()}\n') + except OSError: + pass + print(f'far flood: kept {kept}', file=sys.stderr) + raise finally: for term in terms: term.close() @@ -2861,7 +2913,7 @@ def main(binary, cycles): os.kill(stub, signal.SIGKILL) wait_for('the stub gone', lambda: not alive(stub)) wait_for('the session detached from its killed stub', lambda: sized(client)) - assert b'\x1b[?1049l' in term.text()[drawn:], 'the terminal is put back' + assert b'\x1b[?1049l' in term.last()[drawn:], 'the terminal is put back' assert alive(session) shell.kill() shell.wait(timeout=10) @@ -2889,7 +2941,7 @@ def main(binary, cycles): # the daemon go. exit_session(client, pane) assert stub.wait(timeout=10) == 0, stub.returncode - assert b'let this client go' not in term.text(), term.text()[-300:] + assert b'let this client go' not in term.last(), term.last()[-300:] client.socket.close() term.close() wait_for('the adopted shell hung up', lambda: not alive(shell_pid)) @@ -3273,7 +3325,8 @@ def upgrades(old, new): replica.close() stale.wait(timeout=30) # (Across a change of the replica protocol, it says that instead.) - assert b'upgrade this client too' in term3.text() or b'run the same pardes at both ends' in term3.text(), term3.text()[-400:] + said = term3.last() + assert b'upgrade this client too' in said or b'run the same pardes at both ends' in said, said[-400:] term3.close() print('upgrade: a replica client of the old build was let go, dialled again and was told to upgrade too') # The old image's prompt files swept, the new one's made. |
