[PR #5799] [MERGED] fix(storage): reclaim stale writer leases whose writer had no machine ID #6939

Closed
opened 2026-10-23 04:15:04 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5799
Author: @Romuromylus
Created: 9/3/2026
Status: ✅ Merged
Merged: 9/3/2026
Merged by: @Romuromylus

Base: staging ← Head: fix/writer-lease-null-host-id


📝 Commits (5)

  • 6537fd5 fix(storage): reclaim stale writer leases whose writer had no machine ID
  • 8897862 fix(storage): require a matching PID namespace before PID proofs under the storage-based host proof
  • 909f500 docs(storage): note why a reused PID-namespace inode cannot describe a live writer
  • a19059b Merge branch 'staging' into fix/writer-lease-null-host-id
  • 134d006 test(storage): guard the Linux-only writer-lock assertions for Android too

📊 Changes

4 files changed (+261 additions, -10 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 docs/TROUBLESHOOTING.md (+1 -1)
📝 packages/server/src/db/file-backed-store.ts (+102 -7)
📝 scripts/regressions/storage-writer-lock.regression.ts (+157 -2)

📄 Description

Linked issue

Closes #5744

Why this change

A file-storage writer lease whose record carries hostId: null could never be reclaimed. writerLeaseBelongsToCurrentHost compares the recorded machine ID with ours, and null matches no host, so none of the staleness proofs the reclaim path already trusts (earlier boot, exited PID, reused PID) were ever consulted for such a lease. Startup then failed with Another Marinara Engine process (PID …) may be using … on every boot until someone removed .writer-lease by hand, exactly as reported.

This is not only a legacy-record problem. hostId comes from /etc/machine-id (or the platform UUID / registry GUID), and the Docker image has no machine-id file, so every Docker and Podman deployment writes hostId: null on every boot (verified on my own production container). Within one host boot the socket liveness probe from #5389 still recovers a replaced container, but after a host reboot with an unclean container shutdown (power loss, OOM kill, docker kill) the recorded scope and boot IDs no longer match, the same-host gate stays closed, and the container crash-loops forever. Native Linux hosts without /etc/machine-id hit the same wall on any crash.

What changed

  • packages/server/src/db/file-backed-store.ts: new writerLeaseStorageIsMachineLocal(rootDir). On Linux and Android it statfses the storage directory and accepts it as machine-local only when the filesystem magic is on an explicit allow-list (ext2/3/4, xfs, btrfs, f2fs, zfs, bcachefs, overlayfs, tmpfs, ramfs, erofs, squashfs, ntfs, exfat, vfat, jfs). Network and cluster filesystems, FUSE mounts, unknown types, and every other platform return false, which keeps today's manual-recovery behavior.
  • acquireWriterLease now derives a hostProof (host-id | termux-home | local-storage). A lease with hostId: null on machine-local storage counts as same-host, because a writer that left a lease on storage only this machine can mount necessarily ran here. The existing staleness checks (boot, liveness, pid, pid-reused) are unchanged and still gate the actual reclaim: a live unidentified writer keeps its lease, and a lease naming a different machine ID never qualifies for the storage-based proof. The reclaim warning logs hostProof next to staleReason.
  • PID checks are only meaningful inside one PID namespace, and sibling containers on the same host cannot see each other's processes. The lease now records the writer's PID namespace (/proc/self/ns/pid, Linux and Android) as an optional pidNamespace field, and under the storage-based proof the pid / pid-reused checks run only when that field matches ours. A lease without it (older build, or a writer in another container) relies on the namespace-safe boot and liveness proofs alone, so a rolling restart on a custom image that lacks the liveness socket can never reclaim a still-live sibling's lease. The machine-ID and Termux proofs keep their existing PID path.
  • FileNativeStoreTestHooks.writerLeaseStorageIsMachineLocal and writerLeasePidNamespace override the filesystem check and the recorded namespace for regressions.
  • scripts/regressions/storage-writer-lock.regression.ts: seven new cases (exited unidentified writer in our namespace on local storage is reclaimed; unidentified writer from an earlier boot is reclaimed; live unidentified writer stays locked; exited PID from another namespace stays locked; lease without a recorded namespace stays locked; unidentified writer on shareable storage stays locked; foreign machine ID on local storage stays locked) plus real statfs and /proc/self/ns/pid assertions on Linux. The existing Termux outside-HOME case now pins the storage proof to "shareable" so it still proves the HOME rule on its own.
  • CHANGELOG.md entry under Unreleased / Fixed; docs/TROUBLESHOOTING.md gains one sentence about the automatic recovery.

Out of scope, on purpose: leases with a non-null hostId from another machine (still manual, by design), Windows and macOS (they always have a stable machine ID and keep the strict path), and Docker bind mounts on virtiofs/9p such as Docker Desktop host folders (those classify as shareable and keep the manual path).

One limit worth knowing, called out in the code: the filesystem check proves the filesystem type, not the block device underneath, so a non-cluster filesystem on a multi-attached block device (shared SAN LUN, multi-attach cloud volume) mounted read-write by two hosts at once is invisible to it. That setup corrupts ext4/xfs on its own and is not something the lease can protect.

The regression review also asked whether a sandboxed container runtime that fabricates a per-sandbox boot ID (gVisor, Kata, Firecracker) could let the boot-mismatch proof pre-empt the liveness probe during an overlapping rolling restart. It cannot reach the storage proof in the first place: Kata exposes volumes through virtiofs and Firecracker-class runtimes attach per-microVM block devices or NFS, neither of which is on the allow-list, and gVisor answers statfs on its gofer mounts with its own 9P magic rather than the host's. Those runtimes keep the manual-recovery path exactly as before.

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

Manual verification notes

  • Reproduced on my production Docker deploy (data volume on ext4; every lease it writes has hostId: null): stopped the service cleanly, planted a v4 lease with hostId: null, PID 12, and scope/boot IDs from a fake previous boot, then started it. On staging (204f57fd6) the container crash-looped with StorageWriterLeaseError: Another Marinara Engine process (PID 12, host deadbeefcafe) may be using /app/data/storage on every restart until I removed .writer-lease by hand.
  • Same procedure on this branch: the container booted, logged [file-storage] Reclaimed the writer lease after detecting that the previous owner belonged to an earlier boot. with staleReason: "boot", hostProof: "local-storage", and wrote a fresh lease. A clean stop/start afterwards still releases and re-acquires normally.
  • Negative control on the same deploy: during the zero-downtime rollout the new container started while the old one still held its (also hostId: null) lease, and it correctly refused with the "may be using" error until the old writer shut down, then acquired on retry. A live unidentified writer is still protected.
  • pnpm check passes locally. node ./scripts/run-regressions.mjs --filter scripts/regressions/storage-writer-lock.regression.ts passes on Windows (hook-driven cases plus the "storage proof is limited to Linux and Android" assertion); the Linux statfs assertion runs in CI.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) 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): follow-up #5800
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Server-only change; no UI.

Summary by CodeRabbit

  • Bug Fixes

    • Prevented stale storage writer leases from blocking startup indefinitely after reboots or force-closed processes on local machine storage.
    • Improved lease recovery for containers and Linux systems without a stable machine identifier.
    • Continued requiring manual lease removal for network or shared storage, where automatic recovery cannot be safely verified.
  • Documentation

    • Updated troubleshooting guidance to explain automatic recovery and the manual fallback procedure.

🔄 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/5799 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 9/3/2026 **Status:** ✅ Merged **Merged:** 9/3/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `staging` ← **Head:** `fix/writer-lease-null-host-id` --- ### 📝 Commits (5) - [`6537fd5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6537fd5a8731783f5b60c26aba99e2d504e4b3bb) fix(storage): reclaim stale writer leases whose writer had no machine ID - [`8897862`](https://github.com/Pasta-Devs/Marinara-Engine/commit/889786216dcc116899297b43fa20ca9ca32f8a0b) fix(storage): require a matching PID namespace before PID proofs under the storage-based host proof - [`909f500`](https://github.com/Pasta-Devs/Marinara-Engine/commit/909f5006187ac065af514d15842cc7b9faf05182) docs(storage): note why a reused PID-namespace inode cannot describe a live writer - [`a19059b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a19059bfc83511e148739fd9b7f6e05f793d8e0e) Merge branch 'staging' into fix/writer-lease-null-host-id - [`134d006`](https://github.com/Pasta-Devs/Marinara-Engine/commit/134d0069bc8e9edf501b0acff0351c545e2aa01a) test(storage): guard the Linux-only writer-lock assertions for Android too ### 📊 Changes **4 files changed** (+261 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `docs/TROUBLESHOOTING.md` (+1 -1) 📝 `packages/server/src/db/file-backed-store.ts` (+102 -7) 📝 `scripts/regressions/storage-writer-lock.regression.ts` (+157 -2) </details> ### 📄 Description ## Linked issue Closes #5744 ## Why this change A file-storage writer lease whose record carries `hostId: null` could never be reclaimed. `writerLeaseBelongsToCurrentHost` compares the recorded machine ID with ours, and `null` matches no host, so none of the staleness proofs the reclaim path already trusts (earlier boot, exited PID, reused PID) were ever consulted for such a lease. Startup then failed with `Another Marinara Engine process (PID …) may be using …` on every boot until someone removed `.writer-lease` by hand, exactly as reported. This is not only a legacy-record problem. `hostId` comes from `/etc/machine-id` (or the platform UUID / registry GUID), and the Docker image has no machine-id file, so every Docker and Podman deployment writes `hostId: null` on every boot (verified on my own production container). Within one host boot the socket liveness probe from #5389 still recovers a replaced container, but after a host reboot with an unclean container shutdown (power loss, OOM kill, `docker kill`) the recorded scope and boot IDs no longer match, the same-host gate stays closed, and the container crash-loops forever. Native Linux hosts without `/etc/machine-id` hit the same wall on any crash. ## What changed - `packages/server/src/db/file-backed-store.ts`: new `writerLeaseStorageIsMachineLocal(rootDir)`. On Linux and Android it `statfs`es the storage directory and accepts it as machine-local only when the filesystem magic is on an explicit allow-list (ext2/3/4, xfs, btrfs, f2fs, zfs, bcachefs, overlayfs, tmpfs, ramfs, erofs, squashfs, ntfs, exfat, vfat, jfs). Network and cluster filesystems, FUSE mounts, unknown types, and every other platform return `false`, which keeps today's manual-recovery behavior. - `acquireWriterLease` now derives a `hostProof` (`host-id` | `termux-home` | `local-storage`). A lease with `hostId: null` on machine-local storage counts as same-host, because a writer that left a lease on storage only this machine can mount necessarily ran here. The existing staleness checks (`boot`, `liveness`, `pid`, `pid-reused`) are unchanged and still gate the actual reclaim: a live unidentified writer keeps its lease, and a lease naming a *different* machine ID never qualifies for the storage-based proof. The reclaim warning logs `hostProof` next to `staleReason`. - PID checks are only meaningful inside one PID namespace, and sibling containers on the same host cannot see each other's processes. The lease now records the writer's PID namespace (`/proc/self/ns/pid`, Linux and Android) as an optional `pidNamespace` field, and under the storage-based proof the `pid` / `pid-reused` checks run only when that field matches ours. A lease without it (older build, or a writer in another container) relies on the namespace-safe `boot` and `liveness` proofs alone, so a rolling restart on a custom image that lacks the liveness socket can never reclaim a still-live sibling's lease. The machine-ID and Termux proofs keep their existing PID path. - `FileNativeStoreTestHooks.writerLeaseStorageIsMachineLocal` and `writerLeasePidNamespace` override the filesystem check and the recorded namespace for regressions. - `scripts/regressions/storage-writer-lock.regression.ts`: seven new cases (exited unidentified writer in our namespace on local storage is reclaimed; unidentified writer from an earlier boot is reclaimed; live unidentified writer stays locked; exited PID from another namespace stays locked; lease without a recorded namespace stays locked; unidentified writer on shareable storage stays locked; foreign machine ID on local storage stays locked) plus real `statfs` and `/proc/self/ns/pid` assertions on Linux. The existing Termux outside-HOME case now pins the storage proof to "shareable" so it still proves the HOME rule on its own. - `CHANGELOG.md` entry under Unreleased / Fixed; `docs/TROUBLESHOOTING.md` gains one sentence about the automatic recovery. Out of scope, on purpose: leases with a non-null `hostId` from another machine (still manual, by design), Windows and macOS (they always have a stable machine ID and keep the strict path), and Docker bind mounts on virtiofs/9p such as Docker Desktop host folders (those classify as shareable and keep the manual path). One limit worth knowing, called out in the code: the filesystem check proves the filesystem type, not the block device underneath, so a non-cluster filesystem on a multi-attached block device (shared SAN LUN, multi-attach cloud volume) mounted read-write by two hosts at once is invisible to it. That setup corrupts ext4/xfs on its own and is not something the lease can protect. The regression review also asked whether a sandboxed container runtime that fabricates a per-sandbox boot ID (gVisor, Kata, Firecracker) could let the boot-mismatch proof pre-empt the liveness probe during an overlapping rolling restart. It cannot reach the storage proof in the first place: Kata exposes volumes through virtiofs and Firecracker-class runtimes attach per-microVM block devices or NFS, neither of which is on the allow-list, and gVisor answers `statfs` on its gofer mounts with its own 9P magic rather than the host's. Those runtimes keep the manual-recovery path exactly as before. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - Reproduced on my production Docker deploy (data volume on ext4; every lease it writes has `hostId: null`): stopped the service cleanly, planted a v4 lease with `hostId: null`, PID 12, and scope/boot IDs from a fake previous boot, then started it. On `staging` (204f57fd6) the container crash-looped with `StorageWriterLeaseError: Another Marinara Engine process (PID 12, host deadbeefcafe) may be using /app/data/storage` on every restart until I removed `.writer-lease` by hand. - Same procedure on this branch: the container booted, logged `[file-storage] Reclaimed the writer lease after detecting that the previous owner belonged to an earlier boot.` with `staleReason: "boot", hostProof: "local-storage"`, and wrote a fresh lease. A clean stop/start afterwards still releases and re-acquires normally. - Negative control on the same deploy: during the zero-downtime rollout the new container started while the old one still held its (also `hostId: null`) lease, and it correctly refused with the "may be using" error until the old writer shut down, then acquired on retry. A live unidentified writer is still protected. - `pnpm check` passes locally. `node ./scripts/run-regressions.mjs --filter scripts/regressions/storage-writer-lock.regression.ts` passes on Windows (hook-driven cases plus the "storage proof is limited to Linux and Android" assertion); the Linux `statfs` assertion runs in CI. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [x] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation): follow-up #5800 - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) Server-only change; no UI. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Prevented stale storage writer leases from blocking startup indefinitely after reboots or force-closed processes on local machine storage. * Improved lease recovery for containers and Linux systems without a stable machine identifier. * Continued requiring manual lease removal for network or shared storage, where automatic recovery cannot be safely verified. * **Documentation** * Updated troubleshooting guidance to explain automatic recovery and the manual fallback procedure. <!-- 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-23 04:15:04 +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#6939
No description provided.