Registry Mirror
Skip to content

Conversation

@backnotprop

@backnotprop backnotprop commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Fixes the recurring "T3 daemon.json race" in the "OpenCode 2 installed package" job, plus the 5 s timeout of the compiled-installer test beside it.

Root cause: product

When a T3 worker exits (idle exit, /stop, SIGTERM/SIGINT), cleanup stopped the HTTP server first and removed daemon.json and the running lock last:

cleanup = () => { clearInterval(timer); reviews.dispose(); bridge.dispose(); server.stop(true); void client.close(); rmSync(daemon.json); rmSync(lock); };

Between server.stop(true) and the two rmSync calls, /health fails while daemon.json still names a live pid. In that window:

  • daemonStatus reports running: false over a registry that is still there (what the test caught at line 172).
  • A command arriving then is worse off: ensureDaemon finds the registry unhealthy and spawns a worker, whose serveDaemon finds the lock held by a pid that is alive but no longer answers and refuses with "A T3 companion is still starting or unresponsive". The command then fails with "T3 companion failed to start".

The idle-exit code even says "Unregister first, so a command arriving now starts a fresh worker", but the code did not.

Seen locally with a probe in the test: at the moment status read "not running", the worker's pid was alive, its log already said "No open reviews for 4s; the T3 companion … exits", and daemon.json was gone 500 ms later.

Fix

packages/t3-code/daemon.ts: cleanup now unregisters before anything stops answering:

  1. remove the running lock (first, so this worker can never remove a lock a new worker has just taken),
  2. remove daemon.json, only while it still holds this worker's token,
  3. then stop the timer, the reviews, the bridge, the server and the T3 client.

So once a worker stops answering, its registry is already gone, and a command arriving during the exit starts a fresh worker instead of failing.

Tests

  • connected-session.test.ts: the same "not running implies no daemon.json" check is added after the explicit /stop too (the earlier step), so both exit paths are guarded.
  • install-hook.test.ts: "the compiled installer helper registers an executable hook…" starts the compiled binary five times; it timed out at 6.4 s against bun's 5 s default on a loaded runner (run 38618833740, attempt 1). It now has an explicit 30 s limit.

Proof

connected-session.test.ts against the compiled binary (bun build … --compile with the release flags), each run in its own temp HOME and PLANNOTATOR_DATA_DIR, PLANNOTATOR_BROWSER=/usr/bin/true, PLANNOTATOR_INBOX_CONNECT=0. "Before" is a binary compiled from origin/main, "after" one from this branch; both use this branch's test. macOS, 18 cores.

before after
30 runs, one at a time, no extra load 8 failed (7 at the new post-stop check, 1 at the original line-172 check) 30 passed
60 runs, 4 at a time, plus 12 busy loops 10 failed at the post-stop check (plus 6 failures from two rounds where the whole machine stalled, see below) 60 passed

Without the new post-stop assertion, the original line-172 check failed 1 in 7 unloaded and 1 in 48 under load on main.

Not caused by this race: in one loaded "before" round three parallel runs stalled for about 16 minutes (the machine slept), and in another round three runs waited past eventually's 15 s for the review decision. Neither repeated in the 60 loaded "after" runs.

bun test packages/t3-code with T3_TEST_EXECUTABLE set passes, and tsc --noEmit for the package is clean.

An exiting T3 worker stopped its server first and removed daemon.json and
its lock last. In between, /health failed while the registry still named a
live pid: status read "not running" over a file that was still there (the
connected-session flake at line 172), and a command arriving then started a
worker that found the lock held by a silent live pid and refused to start.

cleanup now removes the lock, then daemon.json (only while it is still this
worker's), and only then stops the timer, reviews, bridge, server and client.
The connected-session test checks the same contract after an explicit stop,
and the compiled-installer test gets an explicit 30 s limit (it timed out at
6.4 s against bun's 5 s default in CI).
@backnotprop
backnotprop merged commit 1625e6b into main Oct 15, 2026
34 of 35 checks passed
@backnotprop
backnotprop deleted the fix/t3-daemon-json-race branch October 15, 2026 00:12
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