Skip to content
Source
Sign inSign up
pingdotgg/t3code

Shutdown (and startup) runtime reconcile cancels every running run in the shared database, including runs owned by another server process #21153

Closediokoioko(GitHub profile, opens in a new tab) opened this issue 1 comment

Versions

  • Server: 0.0.46-nightly.20261014.3105 (headless t3 serve, Linux x64, managed by a systemd user service)
  • Desktop app: 0.0.46-nightly.20261008.2833 (Linux AppImage), connected to that host through an SSH environment
  • Both use the same base dir (~/.t3) and therefore the same statev2.sqlite.

Summary

When a T3 Code server shuts down gracefully, ProviderRuntimeRecoveryService.reconcile("shutdown") selects every thread with a run in preparing, starting, running or waiting across the whole database, with no filter on which server, node, provider session or process owns the run. It then marks those runs cancelled with "Cancelled because the server shut down before the provider work completed." Startup recovery (reconcile("startup")) uses the same selection.

So if two server processes ever point at one base dir, stopping either one cancels the other one's live work. The provider processes behind those runs keep running, so the work is also orphaned.

Steps to reproduce

  1. Start server A on a base dir: t3 serve --port 37775 --base-dir ~/.t3.
  2. Start a few agent runs on A and leave them running.
  3. Start server B on the same base dir from a build without the server-owner.sqlite lock (for example the server bundled with desktop nightly 20261008.2833): t3 serve --port 3776 --base-dir ~/.t3. In our case the desktop's SSH environment did this by itself (see "How we hit it").
  4. Stop server B gracefully (SIGTERM).

We have not run this as a controlled experiment. The steps follow from the code path below plus the production event records described under "How we hit it".

Expected

Stopping B affects only runs B started. A's runs keep running and stay running in the database.

Actual

B's shutdown reconcile marks A's running runs cancelled with the detail "Cancelled because the server shut down before the provider work completed." and command ids command:runtime-reconcile:shutdown:<thread>:<ts>. A's provider processes keep running while their runs show as cancelled. B's startup reconcile does the same on start, with "...the server restarted...".

How we hit it

On 17 Oct 2026 our headless host was heavily loaded (event loop stalls of 3 to 18 s). The desktop app's reuse-or-launch script for its SSH environment probes GET / on the existing server with a 2 s budget; when that failed it launched its own bundled t3 serve against the same ~/.t3, waited for readiness, then killed it, and retried every 1 to 3 minutes (INC lines 86 to 95). Our database event records show 67 runs marked cancelled by those second servers' reconcile commands in three waves: 35 by runtime-reconcile:startup and 32 by runtime-reconcile:shutdown (INC lines 47 to 53). The main server never restarted (same PID throughout, NRestarts=0), and the provider processes it supervised stayed alive (INC lines 62 to 66).

The code path is verified statically (below). Matching each historical cancellation to a specific second server comes from our incident logs and event records; it was not independently re-derived (RV line 17).

Root cause

All offsets are byte offsets into the embedded JavaScript of the 20261014.3105 server binary.

  1. ProviderRuntimeRecoveryService.reconcile, offset 164610515. The same function serves startup and shutdown, and it iterates every recovery thread id with no current-server filter:

    const reconcile = (trigger) => gen$1(function* () {
      ...
      const threadIds = yield* projections.getRecoveryThreadIds("runtime")...
      for (const threadId of threadIds) {
        const projection = yield* projections.getRuntimeRecoveryProjection(threadId)...
  2. ProjectionStore.getRecoveryThreadIds, offset 154797238, case "runtime" (at +2980). The run selection has no owner, node, session or server predicate. The other UNION arms can only widen the set:

    SELECT thread_id FROM orchestration_v2_projection_runs
    WHERE status IN ('preparing', 'starting', 'running', 'waiting')
    UNION
    SELECT thread_id FROM orchestration_v2_projection_runs
    WHERE status = 'queued'
      AND CASE WHEN json_valid(payload_json)
        THEN json_extract(payload_json, '$.queueHeld') IS NOT 1
        ELSE 1 END
    UNION ...

    The pending_provider_threads CTE in the same query carries an owner_node_id column, but the run selection does not use it (EV item 2; RV line 7). getRuntimeRecoveryProjection (offset 154816245) filters run rows by thread id and status, not by server ownership, so loading the projection does not add the missing boundary (RV line 8).

  3. nonterminalRuns, offset 164592079:

    return run.status === "preparing" || run.status === "starting" || run.status === "running" || run.status === "waiting";
  4. reconcileProjection, offset 164596223. It collects those runs and cancels them:

    for (const run of nonterminalRuns(projection)) {
      if (run.status === "waiting") {
        if ((yield* outbox.listByCommandId(CommandId.make(`command:effect:checkpoint.capture:${run.id}`))...)
          .some((effect) => effect.request.type === "checkpoint.capture" && ... (effect.status === "pending" || effect.status === "running"))) continue;
      }
      runs.push(run);
    }
    ...
    const detail = `Cancelled because the server ${trigger === "startup" ? "restarted" : "shut down"} before the provider work completed.`;
    const commandId = CommandId.make(`command:runtime-reconcile:${trigger}:${projection....

    The only node ids in this function concern pending message requests, not which server owns a run (EV item 3). Per RV line 10 the runs are emitted as run.updated with status cancelled through eventSink.commitCommand, and the continuation setting only adds a continuation effect on startup; it does not exempt anything from shutdown cancellation.

  5. Shutdown finalizer, around offset 165064551:

    yield* providerRuntimeRecovery.prepareForShutdown.pipe(ensuring$2(providerSessions.shutdown));
    const reconciliation = yield* providerRuntimeRecovery.reconcile("shutdown");
    yield* logInfo("V2 orchestration shutdown reconciliation completed", reconciliation);

    SIGTERM is routed to fiber interruption (signal handler around offset 150855195, RV line 13), so a normal stop reaches this path. Only an unclean kill skips it.

Precise scope (RV line 15): waiting runs with a pending or running checkpoint.capture effect are skipped, and ordinary queued runs are held rather than all cancelled. The accurate statement is: running runs anywhere in the shared database are eligible for cancellation with no server-ownership filter.

Relation to the server ownership lock

Build 3105 already has an exclusive server-owner.sqlite lock: layerMakeServer calls acquireServerOwnership(config.serverRuntimeStatePath) first (offset 166316401). We have not tested it, but it should make a second 3105 server on the same base dir fail with ServerAlreadyRunningError. It does not protect against a build that predates the lock: the 2833 server binary contains no acquireServerOwnership at all, and the desktop app launches its own bundled runtime over SSH. Separately, t3 serve is registered without rejectRunningServer, unlike the default command and t3 start (offset 166326756). Because reconcile assumes sole ownership of the database, any gap in the lock turns into mass cancellation.

Suggested fix

Either of these would have prevented our incident. Both together would be best.

  1. Scope reconcile to the stopping server's own work. Record the owning server instance (or provider session id) on each run when it starts, and have reconcile("shutdown") cancel only runs owned by the sessions this process is shutting down. On startup, cancel only runs whose owner is provably gone (owner lock free, or owner PID dead), not every non-terminal run.
  2. Refuse to start a second server on a base dir. Keep the 3105 owner lock, make sure it is taken before any migration or recovery on every server command including serve, and have the desktop's SSH launch path never start a server (especially an older runtime) while server-runtime.json names a live PID. An older build cannot honour a newer lock, so (1) is still needed as defence in depth.

Related, can be split out: the desktop's 2 s reuse probe is short enough that a slow but healthy server gets a duplicate launched next to it, and an older runtime then runs migrations on a newer database ("Database migration history diverges ... unknown to this build").

Workaround we use

  • A single-server lock on the host: exactly one long-lived server (the systemd service) serves the base dir.
  • A shim installed as ~/.t3/runtime/versions/<v>/t3 in front of every runtime (the real binary renamed to t3.real). It passes every command through, but refuses serve / start on that base dir when another server already owns it. The managed service execs t3.real directly, so it is unaffected. This stops the desktop's SSH launch path from starting a second server.
  • We do not stop the long-lived server gracefully while any other server might share the database.
  1. Note

    Grok responding on behalf of Julius.

    Thanks for the very thorough write-up, @iokoioko. This is the same failure as #17003: a second backend on the same T3 home runs startup/shutdown reconcile against runs it doesn't own.

    That was fixed by #16102 (merged Oct 14), which makes every server take an exclusive owner lock on the database before it starts, so a duplicate server can't come up next to a live one in the first place. As you spotted, your 3105 server already has the lock. The incident came from desktop nightly 20261008.2833, which predates that fix and launched its own older bundled runtime over SSH. Updating the desktop app to a nightly from Oct 14 or later should close that gap.

    The SSH launch path starting a competing runtime next to a slow but healthy server (the short reuse probe) is tracked in #5749, so please add your timeline there. Scoping reconcile per owner as defense in depth would be a good fit for an Ideas discussion.

    Closing this as a duplicate of #17003.

  2. juliusmarminge added labelsduplicate

  3. juliusmarminge closed this

Sign in to join this conversation.