Skip to content

fix(orcad): free a lock whose PID another user now owns, and explain a possibly reused PID - #29548

Merged
nwparker merged 3 commits into
mainfrom
fix/orcad-lock-prior-boot-reclaim
Oct 16, 2026
Merged

nwparker merged 3 commits into
mainfrom
fix/orcad-lock-prior-boot-reclaim

Conversation

@nwparker

@nwparker nwparker commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​132 $\color{#cf222e}{\Huge{\mathbf{−}}}$​12 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​120
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​98 $\color{#cf222e}{\Huge{\mathbf{−}}}$​10 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​88

ELI5

orcad (Orca's background server) keeps a lock file so two copies never share one data folder. The lock remembers the process ID (PID) of the orcad holding it. If that orcad crashes and the machine restarts, another program can end up with the same PID. Lock files written by older orcads don't record enough for Orca to tell the two apart on Linux, so Orca kept treating that unrelated program as orcad. orcad would not start, and the user wasn't told why. Now, if the PID belongs to a different user account, Orca knows it can't be orcad and takes the lock back. When Orca still can't tell, the error names the program it found and the exact file that is safe to delete.

What Changed

Before: on a Linux host (SSH or local headless), an older orcad dies uncleanly (out of memory, SIGKILL, power loss) and leaves orcad.lock behind. After a reboot, an unrelated long-running process gets the same PID. When Orca is updated:

  • The new orcad sees a live PID whose start time doesn't match.
  • Since fix(orcad): identify instances by kernel start ticks, not drifting wall-clock start time #28125, a start-time mismatch on Linux counts as "unproven" rather than "a different process".
  • The new orcad refuses with orcad_instance_lock_held, naming that unrelated process as "Another orcad".
  • Every SSH deploy readiness wait fails until that process exits or someone finds and deletes orcad.lock by hand.
  • Managed stop reports unverifiable instead of exited for the same instance.

Before #28125, the mismatch let the new orcad take the lock.

After:

  • Proof by owner. A process's uid is the user account it runs as. orcad never changes its uid, so on that "unproven" path a PID now owned by a different uid than the lock's identity can't be the lock's holder. That uid is read from /proc/<pid>. Root and the kernel's overflow uid don't count, because the kernel can show a real holder as either of those. When the proof holds, the verdict becomes differs:

    • lock acquisition reclaims the lock;
    • managed stop answers exited.

    Managed stop takes the identity from the lock only while that lock still names the instance.

  • A useful error. When the verdict stays unproven and the record has no boot id, orcad_instance_lock_held (same code) now says:

    • which program is running under that PID (/proc/<pid>/comm);
    • that it may be an unrelated program that reused the PID;
    • the exact orcad.lock path to delete once the user is sure orcad isn't running.

Records written since #28125 carry kernel start ticks and a boot id. They never reach this path, so nothing changes for them. macOS and Windows are unaffected: they return before the new check.

Why

The regression comes from #28125. It was right to stop trusting Linux wall-clock start times, because they're derived from /proc/stat btime, which drifts. But for records written before it, nothing could prove a mismatch anymore.

Alternatives I rejected:

The uid check is narrower: a root-owned or same-user process that reuses the PID still needs the manual delete. But it never gives a false differs, and for the rest the error now tells the user exactly what to do.

Linked Issue

None. Regression from #28125, found in post-merge review.

Visual Proof

N/A: this is lock and process-identity logic on the execution host, with no UI change.

Testing

  • orcad-process-start-time.test.ts checks the verdict for a Linux wall-clock mismatch on a record without ticks:

    • another non-root uid gives differs;
    • root, the overflow uid, the same uid, or an unreadable owner gives unproven;
    • an identity that isn't a uid is never proven.
    • On a real Linux /proc, the owner uid and command-name readers are checked against this process.
  • orcad-instance-lock.test.ts: the old test "keeps an older Linux record whose wall-clock start time disagrees" now runs on every OS and states its intent: drift alone proves nothing, so the lock stays held. It covers:

    • same uid, root, and unreadable owner: held, with an error naming the process and the exact lock path;
    • another non-root uid: reclaimed;
    • an unreadable process name: the error says "an unknown program";
    • a record that has a boot id: the plain refusal is kept.
  • orcad-managed-stop-identity.test.ts: for an older lock, another uid gives exited with a receipt, and root or the same uid gives unverifiable. A lock that no longer names the instance gives no owner proof (unverifiable).

  • Ran pnpm tc, the orcad lock/stop/start-time suites, desktop-profile-instance-lock.test.ts, oxlint, and check:code-quality:changed, all on macOS. The real /proc tests run on Linux CI.

  • An adversarial review agent found no blocking issue. I applied its findings: the overflow uid, the error wording, and the extra tests.

  • 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 copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • SSH: the check runs on the execution host, where orcad and its lock live.
  • Older and newer peers: no wire change. The error code is the same; only the message text changed, and nothing parses it.
  • Security: an attacker can't fake a different owner for a live orcad's PID, and /proc/<pid>/comm is restricted to printable characters before it reaches the message.

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)

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 16 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b3f1d03c-c6da-4e0c-964b-722c74cdc4a4
📥 Commits

Reviewing files that changed from the base of the PR and between de4422cc55d79383c637cee76a412c82b66cff1b and 8684266.

📒 Files selected for processing: 6
  • src/main/orcad/orcad-instance-lock.test.ts
  • src/main/orcad/orcad-instance-lock.ts
  • src/main/orcad/orcad-managed-stop-completion.ts
  • src/main/orcad/orcad-managed-stop-identity.test.ts
  • src/main/orcad/orcad-process-start-time.test.ts
  • src/main/orcad/orcad-process-start-time.ts

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 78de36fe-ada1-4279-8b61-8abdf01fccd9
📥 Commits

Reviewing files that changed from the base of the PR and between 23dd218 and de4422cc55d79383c637cee76a412c82b66cff1b.

📒 Files selected for processing: 2
  • src/main/orcad/orcad-instance-lock.test.ts
  • src/main/orcad/orcad-instance-lock.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

📝 Walkthrough

Linux process identity comparison now uses owner UID to assess PID reuse when older records lack process incarnation data. Lock acquisition reports possible PID reuse when identity remains unproven and the record has no boot ID. Managed-stop completion carries recorded identity into its checks and uses owner-UID evidence in those checks.

Priority: ⬇️ Low

Merge Risk: 🔵 Low · up to de442

Linux orcad lock handling for older records may still miss some cases where a PID belongs to a different user. The impact is narrow, so the change is mergeable with owner awareness of this open concern.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 12 functions across 6 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title is concise, specific, and accurately describes the main change: reclaiming locks when another user owns the PID and explaining possible PID reuse.
Description check Passed The description is detailed and covers the required explanation, changes, rationale, testing, visual proof, cross-platform impact, security, and checklist. It does not provide the required linked issu…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is detailed and covers the required explanation, changes, rationale, testing, visual proof, cross-platform impact, security, and checklist. It does not provide the required linked issue and states that no issue exists, but the remaining content is complete and directly related to the pull request.

✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: adce3168-c5b0-4838-8a59-94f905b9b657
📥 Commits

Reviewing files that changed from the base of the PR and between cd10ab7 and 23dd218.

📒 Files selected for processing: 6
  • src/main/orcad/orcad-instance-lock.test.ts
  • src/main/orcad/orcad-instance-lock.ts
  • src/main/orcad/orcad-managed-stop-completion.ts
  • src/main/orcad/orcad-managed-stop-identity.test.ts
  • src/main/orcad/orcad-process-start-time.test.ts
  • src/main/orcad/orcad-process-start-time.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

if ((readers.platform ?? process.platform) !== 'linux') {
return 'differs'
}
return ownerProvesAnotherProcess(pid, recorded, readers) ? 'differs' : 'unproven'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

grep -n 'unknown\|matches\|differs\|unproven\|ownerProvesAnotherProcess' src/main/orcad/orcad-process-start-time.ts
sed -n '120,160p' src/main/orcad/orcad-process-start-time.ts

Repository: stablyai/orca

Length of output: 1907


🏁 Script executed:

set -eu
printf '%s\n' '--- identity helper and exported function ---'
nl -ba src/main/orcad/orcad-process-start-time.ts | sed -n '1,165p'
printf '%s\n' '--- direct consumers and related tests ---'
rg -n -F --glob '*.ts' -e 'compareOrcadProcessIdentity' -e "'differs'" -e "'unproven'" -e "'unknown'" src test tests 2>/dev/null || true
printf '%s\n' '--- lock/stop files mentioning identity verdicts ---'
rg -n -F --glob '*.ts' -e 'OrcadProcessIdentityVerdict' -e 'compareOrcadProcessIdentity' -e 'ownerProvesAnotherProcess' src 2>/dev/null || true

Repository: stablyai/orca

Length of output: 45665


🏁 Script executed:

set -eu
printf '%s\n' '--- lock consumer ---'
nl -ba src/main/orcad/orcad-instance-lock.ts | sed -n '1,190p'
printf '%s\n' '--- managed-stop consumer ---'
nl -ba src/main/orcad/orcad-managed-stop-completion.ts | sed -n '1,125p'
printf '%s\n' '--- identity tests ---'
nl -ba src/main/orcad/orcad-process-start-time.test.ts | sed -n '45,180p'

Repository: stablyai/orca

Length of output: 20999


🏁 Script executed:

set -eu
nl -ba src/main/orcad/orcad-instance-lock.ts | sed -n '183,310p'

Repository: stablyai/orca

Length of output: 6481


Use owner evidence when a Linux record has no start time.

When either start time is null, compareOrcadProcessIdentity returns unknown before ownerProvesAnotherProcess runs. Lock acquisition then refuses the live lock, and managed stop does not report the PID as exited. A different eligible UID therefore remains unproven while the replacement process is alive.

This is not permanent after the replacement process exits. Keep the matches path unchanged. A start time within tolerance is already accepted as a match; adding the owner check there would conflict with that result.

Suggested fix
   const actual = (readers.startedAtMs ?? readOrcadProcessStartedAtMs)(pid)
   if (actual === null || recorded.startedAtMs === null) {
+    if ((readers.platform ?? process.platform) === 'linux') {
+      return ownerProvesAnotherProcess(pid, recorded, readers) ? 'differs' : 'unknown'
+    }
     return 'unknown'
   }

@nwparker
nwparker force-pushed the fix/orcad-lock-prior-boot-reclaim branch from 23dd218 to de4422c Compare October 16, 2026 08:54
…, and explain a possibly reused PID

Records written before #28125 carry no start ticks or boot id, so on Linux a
wall-clock mismatch is unproven and a reused PID kept the lock held forever.
A PID owned by another non-root uid cannot be the holder (orcad never changes
uid), so that now proves reuse in both lock reclaim and managed stop. When the
verdict stays unproven, the error names the live process and the orcad.lock
path that is safe to remove once no orcad is running.
…e platform hook also drives data-root privacy
@nwparker
nwparker force-pushed the fix/orcad-lock-prior-boot-reclaim branch from de4422c to 8684266 Compare October 16, 2026 09:11
@nwparker
nwparker merged commit da14046 into main Oct 16, 2026
53 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