From 2d53b8a61ecec05aa7d8d206f6fae424a5d50a8b Mon Sep 17 00:00:00 2001 From: Kaushik Samadder Date: Sat, 15 Aug 2026 18:06:04 +0530 Subject: [PATCH] fix(site-memory): lock site memory writes across processes writeChains only guards updateText/updateJson within one process, so two webcmd processes writing to the same site interleave their read-modify-write and the loser's write is silently dropped (exit 0, no error). Add a cross-process lock (open(path, 'wx') on a sibling *.lock file, retry with backoff, break locks left by a dead owner or older than 10s) and use it to guard the read as well as the write in updateText/updateJson. writeSiteFile is left on the in-process lock alone since it overwrites a full body rather than modifying one. Lock files are hidden from site memory readers the same way temp write files already are. Reproduced the issue's 20-concurrent-process repro against a real build (was 17-19/20 surviving, now 20/20 for both notes.md and endpoints.json). Added cross-process tests that spawn real child processes; verified they fail on the old code and pass on the new one. --- .../__fixtures__/concurrent-writer.mts | 20 ++ src/site-memory/file-lock.test.ts | 97 ++++++++++ src/site-memory/file-lock.ts | 178 ++++++++++++++++++ src/site-memory/local-store.test.ts | 62 +++++- src/site-memory/local-store.ts | 25 ++- 5 files changed, 375 insertions(+), 7 deletions(-) create mode 100644 src/site-memory/__fixtures__/concurrent-writer.mts create mode 100644 src/site-memory/file-lock.test.ts create mode 100644 src/site-memory/file-lock.ts diff --git a/src/site-memory/__fixtures__/concurrent-writer.mts b/src/site-memory/__fixtures__/concurrent-writer.mts new file mode 100644 index 00000000..6a5d0639 --- /dev/null +++ b/src/site-memory/__fixtures__/concurrent-writer.mts @@ -0,0 +1,20 @@ +/** + * Child process for the cross-process site memory tests. Run through tsx: + * + * node concurrent-writer.mts note|endpoint