Skip to content

Linux: terminal-agent shutdown leaves active PTY children orphaned #3147

Closed

Description

@Y4NN777

What happened

On Linux, a browse terminal-agent restart can leave its active PTY child running after the agent exits. Repeated watchdog restarts can therefore accumulate orphaned processes and exhaust host memory.

This is a process leak, not an in-process heap leak.

Root cause

browse/src/terminal-agent.ts handled SIGTERM and SIGINT by removing state files and immediately calling process.exit(0).

That shutdown path did not enumerate or dispose active PtySession instances. The primary session index is a WeakMap, which cannot be enumerated, while sessionsById intentionally excludes legacy grants. Even if a PTY received SIGINT, the immediate process exit prevented the existing delayed SIGKILL fallback from running.

Reproduction

I reproduced this with Bun 1.4.3 using an integration test that:

  1. starts the real terminal-agent on loopback;
  2. grants and opens a real WebSocket PTY session;
  3. launches a child that ignores SIGINT and SIGTERM;
  4. sends SIGTERM to the terminal-agent;
  5. checks whether the recorded child PID is still alive.

Before the fix, the assertion fails because the child survives. After the fix, the child is reaped before the agent exits.

Proposed fix

Maintain a strong set of active sessions solely for shutdown ownership, dispose every active session during agent cleanup, and keep the signal handler alive long enough for the existing PTY shutdown grace period and SIGKILL fallback.

Validation on Bun 1.4.3:

  • 136 focused terminal-agent and PTY lifecycle tests pass
  • production TypeScript check passes
  • test TypeScript ratchet passes with no new diagnostics

The failure and fix were reproduced on Bun 1.4.3.

Related reports

This report is specifically about active PTY descendants surviving a Linux terminal-agent shutdown.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions