Skip to content

fix(ssh): reclaim this desktop's own exited lock on Windows hosts too - #26087

Merged
OrcaWin merged 5 commits into
OrcaWin/node-rt-phase3from
OrcaWin/windows-own-lock-reclaim
Oct 7, 2026
Merged

OrcaWin merged 5 commits into
OrcaWin/node-rt-phase3from
OrcaWin/windows-own-lock-reclaim

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​627 $\color{#cf222e}{\Huge{\mathbf{−}}}$​23 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​604
Prod 14 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​408 $\color{#cf222e}{\Huge{\mathbf{−}}}$​94 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​314

ELI5

If you quit Orca partway through updating the server on a Windows machine you reach over SSH, Orca leaves a "someone is working here" lock behind. When you reopen Orca, it now checks whether the lock was left by its own earlier run that has definitely exited, and if so clears it right away. Before this change that check only ran on Linux and macOS hosts, so Windows hosts made you wait about 20 minutes.

What Changed

Before: after a quit mid-update, the relaunch cleared its own leftover lock in about 13 seconds on a Linux host. On a Windows host it waited for the 20-minute stale window, both for the activation fence and for the version folder's install lock.

After: Windows hosts get the same early reclaim, under the same rules.

Mechanism. The desktop already records {token, pid, hostname, bootedAt} for each lock it takes. That pid belongs to the desktop process, and the exit check (process.kill(pid, 0) returning ESRCH, same machine, same boot) runs on the desktop. So nothing about it depends on the remote OS. What did depend on the remote OS was the host-side half: a POSIX shell command checks that the lock still carries the token, that it has been quiet for 3 minutes, and that no state mutation is live (kill -0 on the mutation's process group). Windows had no equivalent, so the Windows path was skipped.

This PR splits the reclaim into a read-only proof and a conditional steal.

1. Proof (read-only).

  • The desktop works out which of its recorded tokens belong to processes proven to have exited. If there are none, it sends nothing to the host.
  • On Windows, a new host-script op, fence-exited-owner, runs on the pinned node.exe. It reads the lock's owner token and checks that the lock has been quiet for three heartbeats. For the fence, it also checks that no state-mutation lock exists at all.
  • If that Windows command line would be longer than cmd.exe accepts, there is no proof, and the 20-minute window applies.
  • On POSIX, the existing shell check does the same.
  • Neither writes anything.

2. Steal (the only writer).

  • The proven token goes to the existing install-lock steal command. That command now also takes a lock whose owner file still names that token and has been quiet past 180 seconds, in addition to locks that are stale (20 minutes) or left over from a previous boot.
  • The condition is evaluated twice, both times on the very lock instance the steal then moves aside: once before taking the steal claim, and again inside it alongside the existing identity recheck (mtime plus inode on POSIX, mtime plus creation ticks on Windows).
  • A lock that was replaced, refreshed or re-owned in between fails that recheck and answers BUSY. Nothing is ever backdated.
  • An EXITED_OWNER_OK answer tells the caller to forget the token it reclaimed.

3. Fence only: the steal and state mutations never overlap.

  • Inside its claim, the steal takes the state-mutation lock and refuses if any mutation lock already exists. It holds the lock until the takeover is done.
  • It owns the lock exactly the way a mutation does, so only proof that the steal exited can free it:
    • On POSIX it reuses the mutation's own owner writer (posixStateMutationPidRecord / posixStateMutationGroupRecord), which writes a noclobber pid record.
    • On Windows it moves a complete {"pid": N} record into place as owner.json, so the file never exists empty. While that pid is alive, the host script reads the holder as unverifiable and treats the lock as busy.
    • Either way it removes the lock only while the lock still names it.
  • While holding it, the steal rereads the owner and only then re-samples the fence's identity, so a heartbeat that landed after the first sample fails the takeover.
  • A mutation now rechecks its fence token right after it takes the mutation lock, in both serializedStateMutationCommand and the Windows withStateMutationLock. If the token changed, the mutation stops with the fence-lost marker before doing any work.
  • So a mutation that already holds the lock makes the steal answer BUSY, and one that takes the lock after the steal finds the fence gone. The version install keeps no mutation guard.
  • Older clients. This closes these schedules only for mutations issued by clients that include this change. A mutation started by an older client, or by an older content-addressed host script that is still on the host, has no post-lock fence recheck. It can still be admitted late under a replaced fence; that gap predates this PR.
  • A dead mutation lock also refuses the early path. Taking it over safely would race a mutation's own takeover of it, so that rare case waits out the 20-minute window.

Callers. acquireInstallLock takes this through a new exitedOwner option, which replaces #25991's beforeStaleCheck hook. The version-folder install, the fence refusal's clear path, and Recover's stale-takeover lock all use it.

Both POSIX and Windows use this path. On POSIX it replaces the older backdate-then-steal pattern, which had the same race.

Why

  • Why it is safe. A lock is freed only on proof of exit, at both levels. The desktop proves its own earlier process exited. The host proves no mutation process from that run is still alive. "Unverifiable" never frees a lock: an unreadable creation time, a host that doesn't answer, or a host script that can't be staged all fall back to the 20-minute window.
  • Why the steal decides. An earlier revision of this PR backdated the lock by path after checking it. Review found that a live successor which replaced the lock in that gap could be aged and then stolen, and that an interrupted or failed restore would leave the successor aged for good. Tying the reclaim to the steal's own claim and identity recheck means nothing writes to a lock the steal does not already hold. A second owner read before a write would not have closed that gap.
  • Why hold the mutation lock (review round 2). If the mutation guard ran only in the proof, a mutation admitted after the proof, or one whose first heartbeat landed after the steal sampled the fence's age, could keep running under a replaced fence. Holding the one lock every mutation must take, and rechecking the fence on both sides of it, makes the two strictly exclusive.
  • Why the host script. It reuses the pinned node.exe and the staging that already exists, with one call per check and no new PowerShell process. That matters for the Windows EDR posture.
  • Why the host script reads the owner itself. It saves a second remote process, and it works for the install lock, whose caller has no remote home path for the record reader.

Linked Issue

N/A (follow-up to #25941 and #25991)

Visual Proof

N/A. No UI change; the relaunch simply stops waiting.

Testing

  • Steal command (ssh-relay-install-lock-exited-owner.test.ts, real /bin/sh; the same cases run under PowerShell 5.1 on Windows CI). It:

    • takes a quiet lock that still names the exited token;
    • keeps a lock now owned by someone else, however quiet;
    • keeps a lock that is not yet quiet;
    • lets exactly one of several concurrent callers win;
    • in the fence scope, refuses while a mutation lock exists, and leaves no mutation lock behind after taking the fence.
  • Successor race (regression for the review's race fixtures): a live successor that replaces the lock right after the proof is never aged or taken, and its token is kept. Covered for the install lock (orcad-exited-own-install-lock.test.ts) and the fence (orcad-exited-own-fence.test.ts).

  • Late-mutation races (review round 2 fixtures, real /bin/sh, orcad-exited-own-fence-mutation-race.test.ts):

    • a mutation admitted after the proof and paused before its first heartbeat makes the steal answer BUSY;
    • a mutation that takes its lock only after the takeover stops with the fence-lost marker and runs nothing;
    • a mutation started inside the steal claim either holds the lock first, so the steal answers BUSY and the mutation runs under the old fence, or meets the steal's lock and runs nothing.
  • Stalled steal: a steal paused just before it moves the fence still holds the mutation lock against a mutation, even with the lock backdated past every ownerless and quiet threshold in place of a 2-minute wait. This test fails against the previous empty-directory version.

  • Windows mutation recheck: a Windows mutation that takes its lock after its fence was replaced stops before touching state and frees the lock.

  • Windows proof length: a proof command that would not fit cmd.exe falls back to the stale window without running anything.

  • Proof never writes: the lock's mtime is unchanged after the proof, on both POSIX and the Windows host script. This covers the interrupted-restore and failed-restore fixtures, because no write or restore exists any more.

  • Windows host script (orcad-windows-host-fence-ops.test.ts): quiet and token checks, and any mutation lock (live, ownerless, or with an exited holder) keeps the fence.

  • Windows client (orcad-exited-own-lock-windows.test.ts): only exited tokens are offered, and an unoffered or KEPT answer is ignored.

  • Command-line limit: the Windows steal command with an exited owner stays under cmd.exe's limit including the fence's mutation-lock path.

  • pnpm test src/main/ssh/ (318 files, 4034 tests), pnpm tc, oxlint and pnpm run check:code-quality:changed pass.

  • No manual run on a Windows SSH host yet.

  • 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

  • Backwards compatibility. The host script is content-addressed, so this client stages its own copy, and older clients keep theirs.
  • SSH. All liveness verdicts stay live / unverifiable / exited. Only exited frees a lock.

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)

m4air added 5 commits October 6, 2026 22:50
The relaunch after a quit mid-update now frees the activation fence and the
version-dir install lock on a Windows SSH host the same way it does on POSIX,
instead of waiting out the 20-minute stale window. The host script ages the
lock only when its token belongs to a desktop process proven exited, it has
been quiet for three heartbeats, and (for the fence) no state mutation is
live, where a mutation holder counts as gone only by pid plus creation time.
…tion

Review found the reclaim backdated the lock by path after checking it, so a
live successor that replaced the lock in between could be aged and then
stolen, and an interrupted or failed restore left it aged for good.

The exited-holder check is now read-only. The steal command itself accepts
the proven token and, inside its steal claim and identity recheck, also takes
a lock whose owner file still names that token and that has been quiet for
three heartbeats. Nothing is written to a lock before the steal owns it.
POSIX uses the same path.
…can start

Review round 2 found the fence's live-mutation guard ran only in the read-only
proof, so a mutation admitted after the proof, or one whose first heartbeat
landed after the steal sampled the fence's age, kept running under a fence
the steal had replaced.

For the fence, the steal now takes the state-mutation lock inside its claim
(mkdir on POSIX, the exclusive owner.json on Windows) and holds it until the
takeover is done; it refuses when any mutation lock exists. Holding it, it
rereads the owner and only then re-samples the fence identity. A mutation now
rechecks its fence token right after it takes the mutation lock and stops with
the fence-lost marker if it changed. The Windows proof also falls back to the
stale window when its command line would not fit cmd.exe.
Review round 3 found the POSIX steal held the state-mutation lock as an empty
directory, which a mutation reclaims after a minute without any liveness
check; a steal stalled that long lost its exclusion and could replace the
fence under a running mutation.

The steal now writes its pid (and group, under the same rule) with the
mutation's own noclobber owner writer, so only proof of its exit frees the
lock, and it removes the lock only while the lock still names it. On
Windows the owner record is moved into place whole, so it never exists
empty, and is removed only while it still names the steal's pid.
… graph

The mutation-lock owner writers moved into a leaf module, so the relay's
install-lock commands no longer import orcad-state-snapshot and, through it,
the Windows host script, orcad-instance-lock and the daemon process query.
Those modules evaluate imports at load time that existing suites mock
partially. No behavior change.
@OrcaWin
OrcaWin marked this pull request as ready for review October 7, 2026 08:27
@OrcaWin
OrcaWin merged commit b71dbe4 into OrcaWin/node-rt-phase3 Oct 7, 2026
47 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