[PR #7804] [MERGED] fix(storage): reclaim a dead writer lease on Windows when the machine id probe times out #7932

Closed
opened 2026-10-24 04:08:56 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7804
Author: @SpicyMarinara
Created: 10/19/2026
Status: ✅ Merged
Merged: 10/19/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/7801-windows-writer-lease


📝 Commits (1)

  • 5bcfa35 fix(storage): reclaim a dead writer lease on Windows when the machine id probe times out

📊 Changes

2 files changed (+138 additions, -28 deletions)

View changed files

📝 packages/server/src/db/file-backed-store.ts (+72 -28)
📝 scripts/regressions/storage-writer-lock.regression.ts (+66 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7801

Why this change

  • On some Windows machines the first reg.exe launch in a Node process takes about 2 s. The MachineGuid probe had a 1 s budget and ran only once at module load, so the server sometimes started with no host id.
  • Without a host id, the server could not match a crashed writer's lease that recorded one. It never reached the dead-PID check and refused to start, naming a PID that no longer existed, until the user deleted data/storage/.writer-lease by hand. The reporter measured this in detail; it is the evidence #6674 asked for.
  • The #5799 fix only covered the opposite case (the old lease has hostId: null).

What changed

  • readStableMachineId() (win32 only): timeout raised from 1 s to 2 s, in line with the boot-id probe, with one retry when the probe hits ETIMEDOUT. The reporter measured the second reg.exe call in the same process at about 40 ms. macOS and Linux probes are unchanged.
  • acquireWriterLease(): the existing #5744/#5799 "local-storage" same-host proof now also applies to the mirror case. This process has no host id, the lease records one, and the storage is machine-local. The existing PID checks then decide, with their PID-namespace requirement unchanged. A live PID keeps its lease, and shared or network storage stays locked. A known host id that differs from the lease's still never uses the storage proof.
  • New writerLeaseBootIdsMatch(): on win32, two LastBootUpTime boot ids up to 5 minutes apart count as the same boot. The reporter saw the same boot recorded about 6 s apart. Before this change, that jitter could make the boot check reclaim the lease of a writer that was still alive. Now a near match skips only the boot proof, and the PID checks decide. This makes reclaims stricter and never looser. The on-disk format is unchanged, and storage-format.json / STORAGE_VERSION are not bumped.
  • New writerLeaseHostId test hook (null simulates a failed probe), used by the regression.

Limitation (follow-up): writerLeaseStorageIsMachineLocal() returns false on Windows, since only Linux/Android expose a filesystem magic. So on Windows the mirror proof is not reached, and the fix for the reported case is the probe retry. Two cases remain manual-recovery on Windows, as before this PR:

  • Both probe attempts time out.
  • An existing hostId: null lease was written by a process whose probe failed (the reporter's current lease is one). A later start with a working probe still cannot use the storage proof there.

A Windows storage-locality proof is the follow-up. It would need a UNC/mapped-drive check, plus a PID-proof rule for Windows, which has no PID namespace.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Commands run by the agent on macOS:

  • pnpm build:shared && node ./scripts/run-regressions.mjs --filter storage-writer-lock: passed. New cases:
    • Lease with a hostId, an exited PID, this process's host id null and machine-local storage: reclaimed.
    • The same lease with a live PID: still refused.
    • The same lease on shared storage: still refused.
    • writerLeaseBootIdsMatch with the reporter's two jittered boot ids on win32 gives the same boot; hours apart, on linux, or with non-timestamp ids it does not.
  • With only the mirror condition reverted, storage-writer-lock fails, so the new case pins the fix.
  • node ./scripts/run-regressions.mjs --filter <name> for backup-folder-lease, robustness-boot-performance, automatic-backup-retention, lorebook-scan-compaction and launcher/update: each passed.
  • pnpm check: passed.

Manual verification notes

  • Manually verify on a Windows machine where the first reg.exe call is slow that a fresh server start writes a non-null hostId in data/storage/.writer-lease/owner.json.
  • Manually verify on Windows that ending the server with Task Manager "End task" and starting it again reclaims the lease automatically, with the "Reclaimed the writer lease" warning in the log, without deleting .writer-lease by hand.
  • Manually verify on Windows that starting a second server against the same data folder while the first is still running is still refused with StorageWriterLeaseError.
  • Manually verify that Windows startup time does not visibly regress on a normal machine (the probe should still finish in milliseconds there).

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

No user-facing docs change: the existing troubleshooting text about removing .writer-lease still applies to the remaining manual cases.

Summary by CodeRabbit

  • Bug Fixes
    • Improved recovery of stale writer leases on Windows by allowing small differences in boot timestamps.
    • Machine ID checks now retry once after a timeout. When a machine ID is unavailable, lease recovery accounts for whether storage is machine-local or shared.
  • Tests
    • Added regression coverage for boot ID matching, machine ID availability, and writer lease behavior.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/7804 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/19/2026 **Status:** ✅ Merged **Merged:** 10/19/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/7801-windows-writer-lease` --- ### 📝 Commits (1) - [`5bcfa35`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5bcfa354b5886d85f017679ac5a82b3fdf3b91b4) fix(storage): reclaim a dead writer lease on Windows when the machine id probe times out ### 📊 Changes **2 files changed** (+138 additions, -28 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/db/file-backed-store.ts` (+72 -28) 📝 `scripts/regressions/storage-writer-lock.regression.ts` (+66 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. ## Linked issue Closes #7801 ## Why this change - On some Windows machines the first `reg.exe` launch in a Node process takes about 2 s. The MachineGuid probe had a 1 s budget and ran only once at module load, so the server sometimes started with no host id. - Without a host id, the server could not match a crashed writer's lease that recorded one. It never reached the dead-PID check and refused to start, naming a PID that no longer existed, until the user deleted `data/storage/.writer-lease` by hand. The reporter measured this in detail; it is the evidence #6674 asked for. - The #5799 fix only covered the opposite case (the old lease has `hostId: null`). ## What changed - `readStableMachineId()` (win32 only): timeout raised from 1 s to 2 s, in line with the boot-id probe, with one retry when the probe hits `ETIMEDOUT`. The reporter measured the second `reg.exe` call in the same process at about 40 ms. macOS and Linux probes are unchanged. - `acquireWriterLease()`: the existing #5744/#5799 "local-storage" same-host proof now also applies to the mirror case. This process has no host id, the lease records one, and the storage is machine-local. The existing PID checks then decide, with their PID-namespace requirement unchanged. A live PID keeps its lease, and shared or network storage stays locked. A known host id that differs from the lease's still never uses the storage proof. - New `writerLeaseBootIdsMatch()`: on win32, two `LastBootUpTime` boot ids up to 5 minutes apart count as the same boot. The reporter saw the same boot recorded about 6 s apart. Before this change, that jitter could make the boot check reclaim the lease of a writer that was still alive. Now a near match skips only the boot proof, and the PID checks decide. This makes reclaims stricter and never looser. The on-disk format is unchanged, and `storage-format.json` / `STORAGE_VERSION` are not bumped. - New `writerLeaseHostId` test hook (null simulates a failed probe), used by the regression. **Limitation (follow-up):** `writerLeaseStorageIsMachineLocal()` returns `false` on Windows, since only Linux/Android expose a filesystem magic. So on Windows the mirror proof is not reached, and the fix for the reported case is the probe retry. Two cases remain manual-recovery on Windows, as before this PR: - Both probe attempts time out. - An existing `hostId: null` lease was written by a process whose probe failed (the reporter's current lease is one). A later start with a working probe still cannot use the storage proof there. A Windows storage-locality proof is the follow-up. It would need a UNC/mapped-drive check, plus a PID-proof rule for Windows, which has no PID namespace. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` Commands run by the agent on macOS: - `pnpm build:shared && node ./scripts/run-regressions.mjs --filter storage-writer-lock`: passed. New cases: - Lease with a hostId, an exited PID, this process's host id null and machine-local storage: reclaimed. - The same lease with a live PID: still refused. - The same lease on shared storage: still refused. - `writerLeaseBootIdsMatch` with the reporter's two jittered boot ids on win32 gives the same boot; hours apart, on linux, or with non-timestamp ids it does not. - With only the mirror condition reverted, `storage-writer-lock` fails, so the new case pins the fix. - `node ./scripts/run-regressions.mjs --filter <name>` for `backup-folder-lease`, `robustness-boot-performance`, `automatic-backup-retention`, `lorebook-scan-compaction` and `launcher/update`: each passed. - `pnpm check`: passed. ### Manual verification notes - Manually verify on a Windows machine where the first `reg.exe` call is slow that a fresh server start writes a non-null `hostId` in `data/storage/.writer-lease/owner.json`. - Manually verify on Windows that ending the server with Task Manager "End task" and starting it again reclaims the lease automatically, with the "Reclaimed the writer lease" warning in the log, without deleting `.writer-lease` by hand. - Manually verify on Windows that starting a second server against the same data folder while the first is still running is still refused with `StorageWriterLeaseError`. - Manually verify that Windows startup time does not visibly regress on a normal machine (the probe should still finish in milliseconds there). ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) No user-facing docs change: the existing troubleshooting text about removing `.writer-lease` still applies to the remaining manual cases. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved recovery of stale writer leases on Windows by allowing small differences in boot timestamps. * Machine ID checks now retry once after a timeout. When a machine ID is unavailable, lease recovery accounts for whether storage is machine-local or shared. * **Tests** * Added regression coverage for boot ID matching, machine ID availability, and writer lease behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:08:56 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#7932
No description provided.