Skip to content

fix(orcad): a snapshot restore past the exec timeout keeps its fence instead of racing a second restore - #25811

Merged
OrcaWin merged 7 commits into
OrcaWin/node-rt-phase3from
OrcaWin/node-rt-p3-restore-timeout
Oct 6, 2026
Merged

OrcaWin merged 7 commits into
OrcaWin/node-rt-phase3from
OrcaWin/node-rt-p3-restore-timeout

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​748 $\color{#cf222e}{\Huge{\mathbf{−}}}$​36 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​712
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​335 $\color{#cf222e}{\Huge{\mathbf{−}}}$​60 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​275

ELI5

Before Orca rolls back or recovers a managed server, it restores the server's saved profile data from a snapshot. That restore can take longer than 30 seconds. When it did, Orca gave up waiting and assumed it had failed. In fact it was still running on the host. Orca then started a second restore into the same scratch folder, and the server came back up on a mix of old and new files while Orca reported "restored and serving". Now a restore that outlives Orca's wait keeps the host locked until someone checks it. Only one restore can run on a host at a time, and the host itself puts a time limit on the work.

What Changed

Before: snapshot capture, restore and clear went through the generic SSH command helper, which has a 30-second timeout. On the usual SSH library, that timeout closes the channel. Once the server acknowledges the close, Orca records "the command definitely ended". But sshd does not kill a command that has no terminal attached, so the restore kept going. As a result:

  • Rollback saw a failed restore and immediately ran its rescue restore, then started the newer build and released the host's lock.
  • Recover saw a definite failure and marked the lock as abandoned, so the next Recover took it over at once and ran a second restore.

Both restores extract into <root>/.orcad-state-restore-stage, so each one's rm and mv acted on the other's half-extracted files.

After:

  • Client side: the three state operations (capture, restore, clear) go through a new helper, execOrcadStateMutation. It is never cancelled mid-command. It waits longer than the host's own deadline (15 min + 1 min). Any channel close, timeout, "busy" answer or "deadline" answer counts as unconfirmed. Unconfirmed means the activation lock stays fresh: no rescue restore, no launch, no abandoned lock, so the next Recover waits instead of restoring a second time. Only the command's own answer, or a finished non-zero exit, counts as a result.

  • Host side, macOS and Linux:

    • Each operation runs under timeout -s KILL 900 where the host has timeout. GNU timeout kills the whole process group, so tar and rm stop too.
    • Each operation takes a lock directory under ~/.orca-remote (orcad-state-mutation.lock) holding the running shell's process id. A second operation answers STATE_MUTATION_BUSY while that process is alive. A lock left behind by a process that has exited is taken over.
  • Host side, Windows: the host script takes the same lock, with no host-side deadline. Two details:

    • Each operation returns its answer token, and the wrapper writes that answer only after it has released the lock and stopped the heartbeat. The script's answer function exits the process as soon as stdout is written, so an operation that answered early (MISSING, EMPTY, FAILED) used to exit before cleanup and leave the lock behind.
    • The lock holder is identified by process id plus process creation time, read through the server's installed windows-process-tree.node, so a reused process id doesn't count as the holder.
  • The lock is owned by the operation's whole process group (macOS and Linux). Each operation runs in its own process group: setsid where available, or perl -e setpgrp on macOS, with timeout running inside that group so its KILL reaches tar and rm too. The lock records the group id, read from /proc/<pid>/stat (parsed after the last ), because the command name can contain spaces and parentheses) or, where there's no /proc, from ps -o pgid= -p. BusyBox ps (Alpine) has no -p, which is why /proc comes first. A lock with a group id is taken over only once no process in that group is still alive, so a killed shell whose rm keeps running still holds it. A lock with no group id (a host that couldn't start a group) is taken over only once its holder's process id is dead, and its heartbeat has also missed three beats. On those hosts only, a leftover child process remains possible. Windows operations run inside the script's own Node process and start no child processes, so nothing can outlive the holder.

  • Only proof of exit frees the lock, on every platform. Proof means:

    • the holder's process id is dead;
    • on Windows, the process id is alive but its creation time differs (the id was reused);
    • on macOS and Linux, no process in the recorded group is alive.

    A live holder whose identity can't be read stays "busy" however long it has been quiet. Elapsed time is never permission. The owner record is written exclusively (set -C on POSIX, wx on Windows), so a run that resumes after its lock was taken over backs off.

  • The one way out: a host that can't prove a holder's identity (Windows without the process-tree add-on, or POSIX without setsid, perl and /proc) keeps a live holder's lock "busy" until that process exits, or until an operator runs Recover.

  • Reused process ids on macOS and Linux: a reused group id can only make the lock read "busy" for longer, never let it be taken over too early.

  • The activation lock stays fresh while an operation runs. The lock counts as abandoned once its directory's timestamp is 20 minutes old, and these operations can now take up to 15 minutes each. So while one runs, the host refreshes the lock's timestamp every 60 seconds. On macOS and Linux a background loop stops within one beat of its shell exiting. On Windows a timer runs in the host script; its copy and delete steps are now asynchronous so the timer actually fires. A run that is still working can't have its lock taken over by Recover, Stop or another desktop's connect, while a run whose process died stops refreshing and can still be taken over. The refresh never creates a lock that has already gone.

  • Capture had the same flaw, and this fixes it too. A slow pre-activation capture used to read as failed, and Orca restarted the old server while the copy was still running. It now keeps the lock like a slow restore does.

Why

  • Host-side deadline and unconfirmed result together. A client that stops waiting has not stopped anything on the host. Only the host can bound the work. If the client times out anyway, the only safe reading is "unknown", which is what the lock-retention code already expects.
  • A host lock rather than only a per-run scratch folder. Two restores into separate folders would still both delete and replace the same live profile files, so they must not run at the same time. The lock is a directory holding a process id, rather than flock, because macOS and busybox hosts don't reliably have flock.
  • Mixed versions: an older client running alongside this one doesn't take the lock. The fix covers every restore this client starts. A slow restore also no longer releases the lock, so an older client can't take it over during the run.

Linked Issue

Fixes #

Visual Proof

N/A. This changes host-side behaviour; nothing in the interface changes.

Testing

  • orcad-state-mutation-timeout.test.ts: drives the real deploy, rollback and recovery code against the fake orcad host, with the capture or restore outliving the client's wait. The host keeps applying it, and the client sees ssh2's confirmed 30s timeout. All three tests fail on the base commit:

    • Rollback: no rescue restore, no launch, lock kept.
    • Recover: the lock is not marked abandoned, and the next Recover is pending with no second restore.
    • Capture: the old server is not restarted beside the running capture.
  • orcad-state-mutation-exec.test.ts: real execCommand against a fake SSH channel whose remote command answers late, with fake timers:

    • the generic helper reads a 31s restore as a confirmed failure;
    • the new helper keeps waiting past 30s and returns the restore's own answer;
    • giving up past the deadline is unconfirmed even when the close was confirmed;
    • the busy and deadline answers are unconfirmed;
    • a non-zero exit still uses the fallback.
  • orcad-state-mutation-fence-heartbeat.test.ts: real shell with a one-second refresh and the real lock-takeover command (3-second abandon threshold). The lock's timestamp is backdated while a long operation runs, the refresh puts it back, and takeover answers BUSY. After the operation's process is killed with SIGKILL, the backdated lock stays old and takeover succeeds. A finished operation stops refreshing, and a missing lock is never created. A Windows host-script test checks that an operation refreshes a held lock and never creates one.

  • Real shell, process group: a mutation's shell is killed while its child keeps running, and the next mutation answers "busy". Once the whole group is killed, it runs. A lock with no group id waits on a live process id or a recent heartbeat, and is taken over once both are gone. The group id is read correctly from a /proc stat line whose command name contains spaces and parentheses. On Linux, with a ps that rejects -p, the group is still recorded through /proc. These pass under both macOS sh and dash (Linux's usual /bin/sh), and fail on the previous wrapper.

  • Real shell (orcad-state-snapshot-shell.test.ts): a restore answers busy and leaves state alone while a live process holds the lock. It takes over a lock whose holder exited, then releases it.

  • Windows host script, run under this machine's Node with a stand-in windows-process-tree add-on:

    • the lock is gone after MISSING, EMPTY and FAILED (this fails on the previous script);
    • the same live process with the same creation time gets "busy";
    • a reused process id (different creation time) is taken over;
    • a holder that has exited is taken over;
    • an unidentifiable live holder gets "busy" even when its lock is old, and is taken over once it exits;
    • a clear held mid-removal (a suspended process) blocks a restore until it finishes, so the restored profiles survive. This is the scenario from the review, and it fails on the previous script.
  • Windows host script, under this machine's Node (orcad-windows-host-state-ops.test.ts): the same two lock cases for restore, capture and clear.

  • Existing crash-recovery, rollback, deploy, snapshot and shell-integration suites pass. tsc for the Node project, the changed-code quality gate, and an unchanged pnpm-lock.yaml were all verified locally.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • SSH: a lost or abandoned answer is now always "unverifiable", never "failed", for anything that changes profile state. This follows the SSH execution boundary rule.
  • Busybox: busybox timeout signals only the shell, not its whole process group. A tar still running after the deadline would then outlive the lock. The client still reports that run as unconfirmed, so the lock stays held until an operator recovers.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@OrcaWin
OrcaWin marked this pull request as draft October 6, 2026 07:35
@OrcaWin
OrcaWin marked this pull request as ready for review October 6, 2026 07:40
@OrcaWin
OrcaWin force-pushed the OrcaWin/node-rt-p3-restore-timeout branch from 15f4694 to 9995ef1 Compare October 6, 2026 07:40
@OrcaWin
OrcaWin marked this pull request as draft October 6, 2026 07:47
@OrcaWin
OrcaWin marked this pull request as ready for review October 6, 2026 07:50
@OrcaWin
OrcaWin marked this pull request as draft October 6, 2026 07:51
@OrcaWin
OrcaWin marked this pull request as ready for review October 6, 2026 07:55
@OrcaWin
OrcaWin marked this pull request as draft October 6, 2026 08:09
m4air added 6 commits October 6, 2026 01:11
…instead of racing a second restore

A capture, restore or clear ran under the generic 30s exec timeout. On ssh2 the timeout closes the
channel and reads as a confirmed failure, but sshd leaves a pty-less command running, so rollback
ran its rescue restore and recover orphaned the fence for a second one, both in the same stage.

- State mutations run through execOrcadStateMutation: no abort, a client wait past the host's
  deadline, and any closed channel or busy/deadline answer is unconfirmed, so the fence stays fresh.
- POSIX hosts wrap each one in `timeout -s KILL` (where present) and a pid-checked lock dir under
  ~/.orca-remote; the Windows host script takes the same lock.
The fence goes stale by its lock dir's mtime after 20 minutes, and a capture, restore or clear can
now run up to 15 under it, so a rollback's rescue capture plus restore could outlast the window and
let a recovery steal the fence from a live run. While a mutation runs, the host now touches the
fence every 60s (POSIX: a background beat that stops with its shell; Windows: an interval in the
host script, whose mutations are now async so the timer runs). A dead process stops refreshing, so
stale takeover still recovers it.
…'s fence

A wake writes .orca-wake-owner into the fence dir and lets its fence age toward takeover; the
heartbeat now skips a fence that holds that token, and only ever changes the dir's mtime.
… and names its holder by pid and start time

On Windows answer() exits in the stdout write callback, so an op that answered before its first
await (MISSING, EMPTY, FAILED) exited before the wrapper's finally and leaked the lock; a reused
pid then read as alive and every later capture, restore and clear answered busy. Ops now return
their token and the wrapper answers after releasing the lock. The holder is pid plus creation time
(the slot's process-tree addon); one that cannot be identified is stale once its lock misses five
heartbeats. POSIX gets the same heartbeat-age check for a reused pid.
…s group

The lock named only the shell's pid, so a shell killed while its rm or tar ran let the next
mutation take the lock and race that child. Each mutation now runs in its own process group
(setsid, or perl setpgrp on macOS), with timeout inside it so a deadline KILL reaches the children
too. The lock records the group, and is taken over only once no member is alive; a host that can
start no group records none, and its lock is never taken over. Windows ops run in-process, with no
children to outlive the holder.
…d never hold a groupless lock forever

BusyBox ps has no -p, so Alpine hosts recorded no group and their lock read busy forever after a
timeout kill, reboot or OOM. The group now comes from /proc/<pid>/stat (read after the comm field's
last paren), with ps -o pgid= -p as the fallback. A lock that still names no group is taken over
once its pid is dead and its heartbeat has missed three beats.
@OrcaWin
OrcaWin marked this pull request as ready for review October 6, 2026 08:11
@OrcaWin
OrcaWin force-pushed the OrcaWin/node-rt-p3-restore-timeout branch from 0145efa to 85d364c Compare October 6, 2026 08:11
@OrcaWin
OrcaWin marked this pull request as draft October 6, 2026 08:11
A Windows holder whose creation time could not be read was taken over after five quiet minutes
though its pid was alive, so a suspended clear could resume and delete freshly restored profiles.
Both platforms now free the lock only on proof of exit: a dead pid, a different creation time, or
(POSIX) a group with no live member. A live holder of unknown identity stays busy until it exits.
The owner record is written exclusively, so a run that resumes after a takeover backs off.
@OrcaWin
OrcaWin marked this pull request as ready for review October 6, 2026 08:15
@OrcaWin
OrcaWin merged commit bdd2843 into OrcaWin/node-rt-phase3 Oct 6, 2026
46 of 58 checks passed
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