fix(host-kit): publish the reclaim guard atomically

PC
7h ago
+54-41

Summary

Fixes #5423. createReclaimMutex created daemon.reclaim.lock with openSync(path, 'wx') and wrote the holder record with a separate writeSync. A daemon killed between the two calls left an empty guard. readGuardHolder cannot attribute an empty file, so findAbandonedGuard never reaps it and every later start fails with Daemon registration busy.

The guard is now published like the lock owner file already is: publishFileSync writes the holder to a temporary sibling and linkSyncs it into place (link-exclusive), and EEXIST still means "held". A crash leaves at most a temporary sibling, which never blocks acquisition.

The test fixtures that hooked fs.openSync on the guard path now hook fs.linkSync.

Before / after

Process killed after taking the guard but before writing its holder:

before: daemon.reclaim.lock exists, 0 bytes -> every later start: "Daemon registration busy."
after:  no daemon.reclaim.lock (only a temp sibling) -> next start acquires the lock

Validation

  • Tested at 36b2352. The guard test now also checks that the guard is not visible when its holder record is written. It fails on main and passes with the fix.
  • vitest run packages/host-kit/src/internal/process-lock passes (58 tests). oxlint, oxfmt --check and tsc --noEmit are clean.
  • Not run on Windows. An empty guard left by an older version still needs manual removal.

This change was prepared with AI assistance.


Prove it works
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.