Skip to content

fix(hub): mark PTY session status on exit, terminate and restart - #148

Open
dvcolomban wants to merge 1 commit into
devframes:mainfrom
dvcolomban:dvcol/fix-pty-exit-status
Open

fix(hub): mark PTY session status on exit, terminate and restart#148
dvcolomban wants to merge 1 commit into
devframes:mainfrom
dvcolomban:dvcol/fix-pty-exit-status

Conversation

@dvcolomban

@dvcolomban dvcolomban commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

What

startPtySession's proc.onExit only closed the output stream — it never touched the registered session's status, so a PTY session stayed 'running' forever after its process was gone.

This mirrors what startChildProcess already does, so the two spawn helpers now expose the same lifecycle to consumers:

  • on exit — mark 'stopped' on a clean or signalled exit, 'error' on an unsignalled non-zero exit (zigpty reports signal: 0 when no signal was involved), matching startChildProcess's typeof code === 'number' && code !== 0 classification.
  • on terminate() — mark 'stopped', so a deliberate kill doesn't read as a crash.
  • on restart() — mark 'running', matching startChildProcess's restart.
    This one is symmetry rather than an observable fix: the streamClosed early-return means status is already 'running' whenever the body runs, so markStatus de-dupes it.
    It removes the implicit dependency on streamClosed and status staying in lockstep — the same coupling that let the exit bug hide.

Status is mutated in place through a markStatus helper that emits terminal:session:updated, copied from startChildProcess — consistent with update()'s Object.assign semantics, and with consumers that read status off the live session object (e.g. plugins/terminals's manager maps 'stopped''exited' at list time).

What it fixes

Anything relying on status to learn that a PTY session ended never saw the transition — a dock watching terminal:session:updated, the hub's debounced devframe:terminals:updated broadcast, and the terminals plugin's aggregated view of foreign sessions, which all kept rendering a dead shell as running until something else forced a refresh.

Tests

Five cases in host-terminals.test.ts, gated on hasNative like the surrounding PTY tests (0 skipped locally, so all of them genuinely ran):

  • clean exit → 'stopped' + an emitted update,
  • non-zero exit → 'error', terminate()'stopped' rather than 'error',
  • restart() → back to 'running', and restart() after exit staying 'stopped' (pinning the streamClosed no-op).

pnpm lint && pnpm test && pnpm typecheck && pnpm build all pass.

Note for maintainers

In both helpers the exit handler is registered before session is assigned — pty = spawnPty() precedes the session literal, as cp = createChildProcess() does in startChildProcess — so a synchronous exit delivery would throw ReferenceError: Cannot access 'session' before initialization from inside the callback.

Unreachable today (both zigpty and child_process defer exit to the event loop), and pre-existing rather than introduced here, so I kept the existing shape rather than widen the diff.

If you'd like it closed off, constructing the session literal before the spawn is behaviour-neutral in both methods (the closures capture the pty / cp variables, not their values), happy to add that here or in a follow-up.

startPtySession's proc.onExit only closed the stream, leaving status
frozen at 'running' forever — unlike startChildProcess, which already
marks 'stopped'/'error'. Downstream consumers relying on status to know
a PTY session ended (e.g. a dock watching terminal:session:updated) never
see the transition.
Copilot AI review requested due to automatic review settings July 30, 2026 10:14
@netlify

netlify Bot commented Jul 30, 2026

Copy link
Copy Markdown

Deploy Preview for devfra ready!

Name Link
🔨 Latest commit dd6ada5
🔍 Latest deploy log https://app.netlify.com/projects/devfra/deploys/6a6b240bf29b170008220bd1
😎 Deploy Preview https://deploy-preview-148--devfra.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Aligns PTY session lifecycle reporting in @devframes/hub with the existing child-process session behavior by ensuring PTY sessions update their status and emit session-update events when the underlying process exits, is terminated, or is restarted.

Changes:

  • Added an in-place markStatus() helper in startPtySession() to keep session.status synchronized with PTY process lifecycle and emit terminal:session:updated.
  • Updated the PTY onExit handler to mark sessions as 'stopped' on clean/signalled exit and 'error' on unsignalled non-zero exit.
  • Added a dedicated PTY status lifecycle test suite covering exit, non-zero exit, terminate, restart, and restart-after-exit behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/hub/src/node/host-terminals.ts Updates PTY session exit/terminate/restart handling to mutate status and emit terminal:session:updated consistently with child-process sessions.
packages/hub/src/node/tests/host-terminals.test.ts Adds regression tests to validate PTY status transitions and update emission across lifecycle events.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants