From 4ffe5c93b3087f245f5b57a822ab0ecf41cc085c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 10:48:56 -0300 Subject: sessions register with the daemon: a terminal's session forked off its stub, Detach and --attach in place for terminals and windows, pty masters kept, Adopt pardes in a terminal reaches (or starts) the daemon, opens /dev/tty and forks: the child is the session, in a session of its own (setsid), drawing on that terminal (tty.Term.fd, a raw vaxis Tty made from the fd); the parent stays as its stub, the job the shell waits on. Not the daemon spawning it from a passed fd, env and argv: the user's terminals each run in a 9ns user and mount namespace, which a session spawned by a per-user daemon would not share, and a fork costs one small process's page tables where the spawn cost an IPC and an exec. GUI windows and --detach sessions just register. tty: Detach (and a terminal's end: EOF, EIO, a hangup the stub passes on, taken on the signal thread) puts the terminal back, drops it and the stub's stdio, tells the stub (which ends, exit 0), and leaves the session running with nothing drawn (Host.watched), its messages on the clock and `size` its own. pardes --attach[=name] hands its terminal through the daemon; the session enters it as the first one, sized, queried and drawn whole, and that process is its stub until it leaves. A hangup never ends a kept session; TERM and INT do, telling the daemon so the shells go with it (one async-signal-safe send). gui: Detach closes the window (its swapchain or renderer) and keeps the device, atlas and pipelines; the loop sleeps on SDL's queue; pardes-gui --attach[=name] opens a window again. Closing a window still quits. --detach is a session started detached: the tty host with no terminal yet, the GUI with a hidden window closed at once. forkShell sends the daemon a dup of every master and the hosts tell it what they close, so a session killed outright leaves its shells running; the new Adopt word takes them in as terminal panes (the Tty word's pane, its spawn taking the pty, a 1x1 size then the pane's to make it redraw). The Attach word, which switched a session's own terminal to a detached session through the frames, goes, with its effect and request in the core. PARDES_DAEMON=off (the e2e harness's) runs a session as before. test/daemon.py: 50 detach/attach cycles at a level fd count with no stale socket, kill -9 and Adopt, and nothing left; --gui the same for a window. test/fs.py: a forked session's socket is found by its own pid, and a hangup leaves a kept session running. Co-Authored-By: Claude Opus 5.5 --- test/fs.py | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'test/fs.py') diff --git a/test/fs.py b/test/fs.py index 98767a41..74c958ce 100644 --- a/test/fs.py +++ b/test/fs.py @@ -47,6 +47,7 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False preexec = lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0) if launch is not None: args = [binary, *launch] + existing = set(root.glob('pardes-9p-*.sock')) child = subprocess.Popen(args, cwd=root, env=env, stdin=slave, stdout=slave if tty else subprocess.DEVNULL, stderr=subprocess.PIPE, start_new_session=True, @@ -55,7 +56,9 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False # Borrowed handles for input-driving probes, valid inside this context. terminal.update(pid=child.pid, input_fd=master) if launch is not None and socket_name is None: - address = root / f'pardes-9p-{child.pid}.sock' + # Named by the session's pid: a terminal's session is forked + # off the process launched, which stays as its stub. + address = None if slave is not None: os.close(slave) slave = None @@ -68,7 +71,11 @@ def session(binary, root, name, *options, socket_name=None, file=None, tty=False reader = threading.Thread(target=drain, daemon=True) reader.start() deadline = time.monotonic() + 10 - while not address.exists(): + while address is None or not address.exists(): + if address is None: + address = next(iter(set(root.glob('pardes-9p-*.sock')) - existing), None) + if address is not None: + continue if child.poll() is not None: raise AssertionError((args, child.returncode, child.stderr.read().decode())) if time.monotonic() > deadline: @@ -984,19 +991,24 @@ def python_repl_runs_one_line(binary): def signal_cleans_up(binary): - """A detached session told to stop by a signal unlinks its sockets.""" + """A detached session told to stop by a signal unlinks its sockets. A + hangup is a terminal's end, not a session's: the daemon keeps it.""" for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP]: with tempfile.TemporaryDirectory(prefix='pardes-sig-') as directory: root = Path(directory) seen = {} with session(binary, root, 'sig', terminal=seen) as (client, address): - detached = root / 'pardes-detached-sig.sock' - assert address.exists() and detached.exists() + assert address.exists() + if sig == signal.SIGHUP: + os.kill(seen['pid'], sig) + time.sleep(.3) + assert address.exists() and client.read('/index'), 'a hangup ended a kept session' + sig = signal.SIGTERM os.kill(seen['pid'], sig) deadline = time.monotonic() + 5 - while (address.exists() or detached.exists()) and time.monotonic() < deadline: + while address.exists() and time.monotonic() < deadline: time.sleep(.01) - assert not address.exists() and not detached.exists(), (sig, list(root.iterdir())) + assert not address.exists(), (sig, list(root.iterdir())) def odd_paths(binary): @@ -1885,6 +1897,7 @@ def test(binary, quic=False): cooked = termios.tcgetattr(slave) env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), TERM='xterm-256color', PARDES_NOTIME='1') + existing = set(root.glob('pardes-9p-*.sock')) killed = subprocess.Popen([binary, '--tty', 'x.txt'], cwd=root, env=env, stdin=slave, stdout=slave, stderr=subprocess.DEVNULL, start_new_session=True, @@ -1897,12 +1910,21 @@ def test(binary, quic=False): if select.select([master], [], [], .1)[0]: seen += os.read(master, 65536) time.sleep(.3) - own_socket = root / f'pardes-9p-{killed.pid}.sock' - assert own_socket.exists() + # The session's own, named by its pid: forked off the + # launched process, which stays as its stub. + own_socket, = set(root.glob('pardes-9p-*.sock')) - existing assert not dead_socket.exists() assert live_socket.exists() killed.send_signal(sig) assert killed.wait(timeout=5) == -sig + if sig == signal.SIGHUP: + # A hangup is the terminal's end: the session carries on + # detached (the daemon keeps it) until told to end. + assert own_socket.exists(), own_socket + os.kill(int(own_socket.name[len('pardes-9p-'):-len('.sock')]), signal.SIGTERM) + deadline = time.monotonic() + 5 + while own_socket.exists() and time.monotonic() < deadline: + time.sleep(.01) assert not own_socket.exists(), own_socket while select.select([master], [], [], .2)[0]: try: -- cgit v1.3