pty · docs

embeddable PTY sessions for Elixir hosts

erlexec drives one OS child per session, out of process; a session owns its PTY and outlives every client. Closing a tab, refreshing or dropping the network only detaches; reattaching by id replays the scrollback and rejoins the same live process, and any number of clients can share one terminal. The library serves nothing itself: the host owns the endpoint, the auth and the page.

Map

Every public call goes through the facade and takes a session as an id or a pid. The API, the integration recipes and the security model are in the README.

Pty.start/2 spawns a child through erlexec into a Pty.Session, which fans each output chunk out to every subscriber and onto a bounded scrollback. The session ends only when the child exits (its status) or a reap stops it (129); either way terminate/2 sends every subscriber one {:pty_exit, code}. A reap hangs up the child's process group: SIGHUP at once, a 2 s grace, then SIGTERM, then SIGKILL 1 s later.
The session lifecycle: start and attach, fan-out and scrollback, the two ends, and the hangup ladder a reap runs. Source: Claude Design canvas pty figures, board session-lifecycle.
pathholds
lib/pty.exthe facade — every public call, id or pid
lib/pty/session.exthe GenServer owning one PTY: spawn, fan-out, scrollback
lib/pty/client.exthe transport contract and the shared frame decoder
lib/pty/socket.exthe WebSock handler bridging one socket to a session
lib/pty/plug.exthe upgrade gate: id validation, Origin check, hand-off
priv/static/pty.jsbrowser glue; the host hands xterm.js in
test/pty/ExUnit over real cat/sh PTYs — no mocks
test/js/bun test drives pty.js, unmodified, against hand-written fakes of the WebSocket, the timers and xterm.js

Invariants

What every change must keep true.

Close codes

The client sends JSON text frames ({"t":"i","d":…} input, {"t":"b","d":…} raw bytes, one per code point 0–255, {"t":"r","cols":C,"rows":R} resize); the server sends raw output as binary frames, the scrollback first. Three private close codes are final, and pty.js stops on exactly those.

codemeaningpty.js
4000the session ended — its child exited or it was reapedstop · onExit
4404a reattach found no session, or the one found ended as it attachedstop · onExit
4422an invalid id, reattaching or not, or a refused spawn — :command or :cwd not found, or an option rejected, such as a bad :cols or a :max_lag that is no positive integer; permanentstop · onExit
1011any other start failure — pty cannot tell a passing failure (resources) from a fault (a malformed option); the host's logs show whichreconnect
1013a subscriber fell more than :max_lag messages behind and was taken off the feedreconnect · reattach, resync from scrollback
1000never the session's: Bandit sends it on its own shutdownreconnect
othertransport loss, or an upgrade Pty.Plug refused (400 bad id, 403 origin) — backed off, since the backoff resets only on attachreconnect, 1 s → 15 s
A table of close codes. 4000 session ended, 4404 no session for a reattach, 4422 spawn refused: pty.js stops and fires onExit. 1011 any other start failure, 1000 Bandit's own shutdown, and any other close or a refused upgrade: pty.js reconnects. Below, the client loop: connect, the first frame (the scrollback) resets backoff to 1 s and marks the client attached so later connects add ?reattach, then on close either stop or retry with backoff doubling from 1 s to 15 s.
WebSocket close codes and the client loop in pty.js. Source: Claude Design canvas pty figures, board close-codes.

Hazards

Traps a change can walk into.

Next

What the next shift does first. shift brief reads this list, and a landing that changes it edits it here.

  1. bun test --coverage reports 100 % of pty.js but enforces nothing, and bun measures lines and functions, not branches — mount()'s degrade paths are held by their test, not the metric: add bunfig.toml with [test] coverageThreshold = { functions = 1.0, lines = 1.0 } — the shift sandbox refuses writing bun config.
  2. list/0 drops a session busy past the 1 s info timeout — unreproduced: a yes flood answers in 0 ms; find a stalled erlexec call first.
  3. Sweep leftovers, confirmed and unfixed (s-20261019T0620Z): a restarted Registry leaves live sessions unregistered; scrollback replay can start mid-escape; the resize skip compares against the last requested size, not the PTY's; a lone UTF-16 surrogate drops a whole input frame; subscriber messages carry no session id.
  4. Redraw the close-codes and session-lifecycle boards of the pty figures canvas for 1013 and {:pty_lagged} (mise run figures) — the PNGs and their alt text predate backpressure.
  5. A lagged Pty.Socket drains up to :max_lag queued chunks before it closes 1013, and a client slower than the child's output resyncs (RESET + ≤ 256 KB replay) about once a second while the burst lasts, since the first frame resets the backoff; a flush of queued {:pty_data} on lag and a backoff that survives a 1013 are the candidates.
  6. A child that floods without end through SIGHUP and ignores SIGTERM is never SIGKILLed (probe: trap '' TERM; trap 'while :; do echo x; done' HUP, ~480 k chunks/s, alive 10 s on, before and after backpressure) — exec-port's kill timer starves behind the output, so the reap never returns; confirm in erlexec and bound it (a session-side SIGKILL past the ladder).

pty · jgeschwendt/pty · the README is the API reference.