Repository navigation
fix(nodejs): write state files atomically and tolerate corruption - #109
Open
tangquanwei wants to merge 1 commit into
Open
tangquanwei wants to merge 1 commit into
tangquanwei wants to merge 1 commit into
Conversation
`~/.wechatbot/cursor.json` could end up zero-filled, and `FileStorage.get()`
rethrew the resulting `JSON.parse` error. That rejected `client.start()`
before the poll loop began, surfacing as:
Poll error: Unexpected token ... is not valid JSON
Two independent fixes:
- Writes are now atomic (temp file + fsync + rename) and serialized per key,
so a crash or overlapping write cannot leave a truncated/zero-filled file.
- Reads tolerate a UTF-8 BOM, and a corrupt file is quarantined and treated as
missing instead of throwing, so one bad state file can no longer prevent the
bot from starting.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A single damaged state file could permanently kill the bot.
FileStorage.get()did a bareJSON.parse()and rethrew on failure, soWeChatBot.start()rejected insidepoller.loadCursor()before the poll loop started:The state file can be damaged because
set()used a plainwriteFile: non-atomic, nofsync, and callers don't await it, so overlapping writes to the same path can interleave.Fix (one file + tests)
nodejs/src/storage/file.ts:fsync→rename, plus writes serialized per key. Readers see either the old or the new complete file, never a partial one.delete()/clear()wait for in-flight writes.*.corrupt-<ts>) and returned as missing instead of throwing.This removes both halves of the bug: the file is no longer produced corrupt, and even an externally damaged file can no longer take down startup.
Tests
nodejs/tests/file-storage.test.ts— atomicity, concurrent writes, BOM, NUL/corrupt quarantine, delete-vs-write race.87 → 88tests passing (npx vitest run),tsc --noEmitclean.Notes