Skip to content

fix(run): a stop during a restart in place stops hex cleanly - #493

Open
AVGVSTVS96 wants to merge 1 commit into
mainfrom
fix/stop-during-startup
Open

AVGVSTVS96 wants to merge 1 commit into
mainfrom
fix/stop-during-startup

Conversation

@AVGVSTVS96

Copy link
Copy Markdown
Owner

A hex stop that comes while a restart in place is still starting killed hex outright: it exited 143 and left state/hub.pid behind. Found while fixing a test's timing (#492).

What happened

hex run runs the owner's login shell first and set its stop handler only after. A fresh start can't be stopped that early, since hex stop needs state/hub.pid, written later. A restart in place can: it starts bin/hex run again under the pid that file already names, so a stop during the login shell reached a hex with no handler.

It doesn't reach a hex under systemd, where hex stop goes through systemctl. It does where there is none.

The change

The handlers are set first thing in run, and the stop removes the pid file only when it names this hex, so a second hex stopped early doesn't delete the running one's:

leave() { [ -z "${hub:-}" ] || { kill -"$1" "$hub" 2>/dev/null; wait "$hub" || :; }; [ "$(cat "$pidfile" 2>/dev/null)" != $$ ] || rm -f "$pidfile"; exit 0; }
trap 'leave TERM' INT TERM
trap 'leave HUP' HUP

A stop that lands during the login shell now waits for that shell to end, up to its 10 second limit, before hex exits.

The test

test("a stop that comes while a restart in place runs the login shell again stops hex as any stop does", async () => {
  // … a login shell that takes 2 seconds once hex has restarted
  Bun.spawnSync([join(hex, "bin/hex"), "restart"]);
  await until(() => existsSync(join(home, "shells")));
  expect(await Bun.spawn([join(hex, "bin/hex"), "stop"]).exited).toBe(0);
  expect(await running.exited).toBe(0);
  expect(existsSync(join(home, "state/hub.pid"))).toBe(false);
});

Without the change it fails with 143 in 2 seconds. With it, it passes in 4.

Checked

  • bin/hex.run.test.ts (25) and bin/hex.restore.test.ts (15).
  • The whole check was not run on the server; this PR's check on GitHub is its first run.

Not checked

  • A machine whose /bin/sh is dash, or macOS. Here /bin/sh is bash.
  • How launchd treats an exit of 143.

@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hex Ready Ready Preview Nov 18, 2026 9:03pm UTC

Request Review

A restart in place starts bin/hex run over as the pid state/hub.pid names, and ran the login shell before setting its stop handler, so a hex stop that came then killed it with SIGTERM (143) and left state/hub.pid behind. The handler is now set first, and removes the pidfile only when it names this hex.

This branch was successfully deployed

1 active deployment
Preview — cdf87fa3 Deployed Nov 18, 2026 by vercel[bot]
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