Session Lifecycle¶
How a relay-tty session is born, lives, and dies.
Creation¶
- You run
relay bash(or any command) - The CLI spawns a detached
pty-hostprocess - pty-host calls
forkpty()to create a PTY and fork the shell - pty-host creates a Unix socket at
~/.relay-tty/sockets/<id>.sock - pty-host writes session metadata to
~/.relay-tty/sessions/<id>.json - The CLI attaches via the Unix socket (or prints the URL if
--detach)
Running¶
While the session is active:
- pty-host reads from the PTY and writes to its 10MB ring buffer
- Any connected client (CLI or browser) receives data in real-time via the Unix socket
- Input from any client is written to the PTY
- Metrics (throughput, buffer position) are broadcast periodically
Persistence¶
Sessions survive:
- Browser disconnects — close the tab, reopen, output is replayed
- CLI detach — press Ctrl+], session keeps running
- Server restarts — pty-host is independent; the server rediscovers it on restart
- Network drops — the tunnel reconnects automatically
Sessions do NOT survive:
- Machine reboot — pty-host processes are killed (no persistent daemon)
relay stop— explicitly kills the pty-host process- Shell exit — when the shell process exits (e.g., typing
exit), pty-host cleans up
Reconnection¶
When a client reconnects:
- Browser opens a WebSocket to the server
- Server opens a new Unix socket connection to pty-host
- Browser sends
RESUME(lastByteOffset) - pty-host sends delta data (or full replay if offset is stale)
- pty-host sends
SYNC(currentOffset) - Terminal is restored — no visible delay
Cleanup¶
When a session ends:
- The shell process exits
- pty-host detects the PTY closure
- pty-host removes its Unix socket file
- Session metadata file is cleaned up
- Connected clients receive a close frame