Skip to content

refactor(self-update): use active markers instead of lifetime locks#205

Merged
BlackHole1 merged 1 commit into
mainfrom
pref-start
May 13, 2026
Merged

refactor(self-update): use active markers instead of lifetime locks#205
BlackHole1 merged 1 commit into
mainfrom
pref-start

Conversation

@BlackHole1
Copy link
Copy Markdown
Member

The previous process-lifetime version lock prevented concurrent CLI invocations on the same version from coexisting, because each process held an exclusive lock on the version lock file for its entire run.

Split the lock into two concerns: install locks guard mutation of a version directory, while per-process active markers advertise which versions are currently in use. Multiple processes can now run the same version simultaneously, and a self-update that targets a version held by another process now returns a busy outcome before clobbering its on-disk binary.

The previous process-lifetime version lock prevented concurrent CLI
invocations on the same version from coexisting, because each process
held an exclusive lock on the version lock file for its entire run.

Split the lock into two concerns: install locks guard mutation of a
version directory, while per-process active markers advertise which
versions are currently in use. Multiple processes can now run the
same version simultaneously, and a self-update that targets a version
held by another process now returns a busy outcome before clobbering
its on-disk binary.

Signed-off-by: Kevin Cui <bh@bugs.cc>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 13, 2026

Review Change Stack

Summary by CodeRabbit

Release Notes

  • Refactor
    • Strengthened self-update system with improved detection and prevention of concurrent update attempts.
    • Enhanced version protection mechanisms during updates for better reliability.
    • Refined lock cleanup to more effectively remove stale entries and prevent update conflicts.
    • Processes attempting simultaneous updates now receive feedback about active ownership.

Walkthrough

This PR refactors the self-update version locking mechanism from a single process-lifetime lock model to a split filesystem layout with three lock types: active version markers (per-process under active/<version>/), install version locks (under install/), and legacy locks. The implementation adds install-lock reference counting to handle repeated acquisitions by the same process, stale-lock cleanup across all types, ownership discovery and liveness checks, and integration into the CLI bootstrap and core update flow. Busy outcomes now propagate from install-lock acquisition and active-owner checks in version materialization.

Possibly related PRs

  • oomol-lab/oo-cli#132: Both PRs modify materializeTargetVersion and busy/owner coordination in the self-update core flow.
  • oomol-lab/oo-cli#77: The main PR refactors current-version coordination via active markers and updates run-cli.ts initialization/cleanup, directly overlapping with that PR's managed install/update flow wiring.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title follows the required format and clearly describes the main refactoring change of replacing lifetime locks with active markers.
Description check ✅ Passed The description is directly related to the changeset, explaining the motivation and benefits of replacing process-lifetime locks with active markers and install locks.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch pref-start

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

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/application/self-update/lock.ts (1)

627-633: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Handle non-directory lock paths gracefully in readDirectoryEntries.

If a lock path expected to be a directory is actually a file, readdir throws and aborts stale-cleanup/active-owner discovery instead of skipping malformed entries. This can block self-update flows.

Suggested fix
 async function readDirectoryEntries(path: string): Promise<string[]> {
     try {
         return await readdir(path);
     }
     catch (error) {
-        if (isPathMissingError(error)) {
+        if (isPathMissingError(error) || isDirectoryReadError(error)) {
             return [];
         }

         throw error;
     }
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/application/self-update/lock.ts` around lines 627 - 633,
readDirectoryEntries currently treats only missing-path errors via
isPathMissingError, but if the lock path is a file (readdir throws ENOTDIR or
equivalent) it aborts cleanup; update readDirectoryEntries to also treat "not a
directory" errors as non-fatal by detecting the error code/name (e.g., ENOTDIR
or error indicating the path is not a directory) and return an empty array (skip
the malformed entry) instead of rethrowing; keep rethrowing for unexpected
errors and reference the existing readDirectoryEntries function and
isPathMissingError predicate when implementing the extra check.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/application/self-update/lock.ts`:
- Around line 627-633: readDirectoryEntries currently treats only missing-path
errors via isPathMissingError, but if the lock path is a file (readdir throws
ENOTDIR or equivalent) it aborts cleanup; update readDirectoryEntries to also
treat "not a directory" errors as non-fatal by detecting the error code/name
(e.g., ENOTDIR or error indicating the path is not a directory) and return an
empty array (skip the malformed entry) instead of rethrowing; keep rethrowing
for unexpected errors and reference the existing readDirectoryEntries function
and isPathMissingError predicate when implementing the extra check.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f12ea5b8-b1ad-46e4-be4b-3e28fa99aecd

📥 Commits

Reviewing files that changed from the base of the PR and between 7b1da7f and 763541b.

📒 Files selected for processing (7)
  • src/application/bootstrap/run-cli.ts
  • src/application/self-update/core.test.ts
  • src/application/self-update/core.ts
  • src/application/self-update/lock.test.ts
  • src/application/self-update/lock.ts
  • src/application/self-update/paths.test.ts
  • src/application/self-update/paths.ts

@BlackHole1 BlackHole1 merged commit bfdf0e2 into main May 13, 2026
6 checks passed
@BlackHole1 BlackHole1 deleted the pref-start branch May 13, 2026 06:35
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