Pitfalls: Terminal & PTY
Traps and invariants across PTY emulation, ANSI parsing, VTE escape processing, grid layout, and child process lifecycles.
PTY Spawning & Process Lifecycles
portable_pty$HOMEfallback behavior:CommandBuilder::as_command()resolves directory paths withcwd.filter(is_dir).unwrap_or(home). If$HOMEpoints to a deleted or non-existent path, spawns fail withNo such file or directory (os error 2)naming the binary (e.g./bin/bash) even when the binary exists and is executable.- Teardown via
Drop: Every Rust structure wrapping a child process (portable_pty::Child) or dedicated reader thread must implementDropto call.kill()and abort reader tasks. Without explicit teardown, closing panes in Godot leaves orphaned shell processes and background threads. - PTY child working directory: A child process spawned from the GDExtension without an explicit
cwdinherits the Godot project directory, not the user’s terminal launch directory. Always resolve and supply the active workspace directory explicitly. - Exit detection on Windows (ConPTY): ConPTY keeps the PTY pipe open after the child process exits. Relying solely on pipe EOF causes panes to report
running: trueindefinitely. Exit detection requires pollingtry_waitalongside pipe reads. - Non-blocking input writes: PTY input is backpressured through bounded channels (
STDIN_QUEUE_CAPtoWRITER_QUEUE_CHUNKS). Writing to the PTY must be non-blocking; blocking in the write path leaves raw-mode children unkillable when they cease reading stdin. - A pty write’s success is not delivery: a raw-mode Linux master write absorbs megabytes into the tty flip buffers before it blocks, and the line discipline discards what a non-reading child never consumes while
writestill returns the full count. So a test cannot infer “the input queue filled” from countedsend_textsuccesses - an 18 MiB burst was swallowed with no error and onlyREADYon the grid, while a fresh probe blocked after ~11 KiB; saturation depends on the flip-buffer state earlier output left behind (measured 2026-11-21). Test a queue cap at its ingress instead: send the burst without yielding, so nothing drains and the cap is what refuses.
Grid Synchronization, Rendering & Sizing
- SGR 7 inverse video color swap: ANSI SGR 7 swaps both foreground and background colors. Do the swap once at the grid presentation boundary (
CellInfo::paint_colors). Bash readline wraps bracketed paste echos in SGR 7; swapping only text color paints glyphs with their own background, rendering them invisible. - Selection lives in grid space: Text selection coordinates must be maintained in grid buffer lines (
alacritty_terminal::Term::selection), not in viewport screen coordinates. Output scrolling and window resizing rotate the grid; viewport-based selections desynchronize immediately. - Transient pane sizes collapse grids: During layout churn (splits, spawns), panels briefly report tiny pixel sizes (~38×40 px, or 4×2 cells). If passed to
resize_grid(), the grid collapses and rewraps destructively. Skip resizing when pixel dimensions fall below the minimum grid threshold (TerminalPane._grid_for_size). - Resize rate-limiting & debouncing: Firing SIGWINCH on every frame during window drag overwhelms child processes. Debounce
_on_resizecalls in GDScript and avoid redundant SIGWINCH signals if calculated rows and columns have not changed. - Raw-byte buffering for grid replay: Never buffer parsed plain text for terminal replay. The ANSI state machine requires raw byte streams with escape sequences preserved.
Terminal Protocols & Key Handling
- Terminal query replies (
Event::PtyWrite):alacritty_terminalemitsEvent::PtyWritein response to terminal status queries (such as DSR cursor position requestsESC[6n). gPTY must forward these reply bytes back to the child PTY. Dropping them breaks interactive TUIs (e.g., OMP, lazydocker) after window resizes. - Alt key combos: For Alt+letter combinations, the keymap expects the frontend layer to prepend
\x1b(ESC) in the_key_to_textfallback path. - Mouse mode reporting: Mouse reporting mode bits (
get_mouse_mode) are the source of truth for whether the child owns the pointer. The frontend encodes SGR (DECSET 1006) or legacy X10 mouse packets accordingly. Shift-clicking bypasses mouse reporting to allow host text selection. - Ctrl+V passthrough:
Ctrl+Vmust reach the child shell as literal^V(for readline quoted-insert and vim visual-block). Terminal paste must always be bound toCtrl+Shift+V.