Skip to content

fix(server): stop everything on Ctrl+C or a closed terminal - #585

Merged
ArgaFairuz merged 1 commit into
mainfrom
fix/clean-shutdown
Nov 18, 2026
Merged

ArgaFairuz merged 1 commit into
mainfrom
fix/clean-shutdown

Conversation

@ArgaFairuz

Copy link
Copy Markdown
Collaborator

Summary

Make Ctrl+C and closing a terminal stop Clopen completely before the prompt returns, with the ports free.

Why

Under bun run, one Ctrl+C reached the supervisor twice and was treated as a forced stop; bun run then killed the supervisor and orphaned the children with their ports. SIGHUP was ignored, and shutdown never ended terminal sessions or tunnels.

Changes

  • Exact absorption of the runner's duplicate SIGINT; first press graceful, second forces
  • A stop waits for the child and every process it started (collected first, across process groups)
  • SIGHUP stops when started from a terminal, ignored without one (nohup, services)
  • Supervised children stop if their supervisor disappears (Vite through a small guard)
  • Shutdown ends terminal sessions and awaits tunnels; a requested stop exits 0
  • PtyKit 0.1.1, TunnelKit 0.0.16

Notes

Requires PtyKit 0.1.1 and TunnelKit 0.0.16 to be published, then bun install to update bun.lock.

Ctrl+C on clopen or bun run dev returned the prompt while processes
were still running, so starting again picked other ports; closing a
terminal tab left what ran in it (a dev server) running for good.

Under bun run, one Ctrl+C reaches the dev runner twice, 1 ms apart: from
the terminal and forwarded by the runner. The supervisor took the copy
as a second press and SIGKILLed every child, and bun run, on its own
second SIGINT, killed the supervisor and returned the prompt with the
children orphaned in their own process groups. The supervisor also
ignored SIGHUP, and the server never stopped its terminal sessions or
tunnels on shutdown.

The duplicate is now absorbed exactly (the parent is a bun runner in the
same process group), the first press stops gracefully and a second one
forces. A stop is over only once the child and every process it started
- collected first, since some sit in other groups - have exited; only a
hang past the timeout is killed. SIGHUP stops everything when started
from a terminal and is ignored without one (nohup, services). A
supervised child stops itself if its supervisor disappears; Vite runs
under a small guard for that. The server ends terminal sessions and
awaits its tunnels on shutdown, and a requested stop exits 0.

Needs PtyKit 0.1.1 (a closed tab ends its shell and everything started
from it) and TunnelKit 0.1.0 (stopping waits for cloudflared to exit).
@ArgaFairuz
ArgaFairuz merged commit 17cd90c into main Nov 18, 2026
1 check failed
@ArgaFairuz
ArgaFairuz deleted the fix/clean-shutdown branch November 18, 2026 03:49
ArgaFairuz added a commit that referenced this pull request Nov 18, 2026
Unit tests failed on CI for every push since #585, and a diff test failed
now and then before that.

The launcher test failed every time on CI and never locally. stopOnSignals
asked ps three times whether its parent was a bun runner before it
installed any signal handler. Until a handler exists a signal takes its
default action, so SIGHUP or SIGINT in that window killed the supervisor
outright and left the server running with nothing to restart it. A slower
runner made the window wider than the server's start-up, which is when
the test sends SIGHUP. The handlers are now installed first and ps runs
last; a signal that arrives meanwhile waits for its handler. With ps
slowed by 300 ms the test failed every run before the change and passes
after.

The diff invariant test diffed a fully reversed 20,000-line file, Myers'
worst case. That took 3 to 5 s against bun's 5 s timeout, so it passed or
failed with the runner's load. The case keeps its shape at 4,000 lines;
the invariant does not depend on size, and the diff runs in a worker in
the app.

The git identity test read the developer's own name when run from a shell
Clopen started: GIT_CONFIG_COUNT/KEY_n/VALUE_n outrank every config file.
The test now drops them.
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.

1 participant