fix: prevent data corruption in sync-leaderboard.js with atomic writes#211
Merged
jagdish-15 merged 1 commit intoJun 18, 2026
Merged
Conversation
Implement atomic file writes to prevent data loss when the sync process crashes mid-write (GitHub Actions runner preemption, OOM). - Add atomicWrite() helper: writes to .tmp file then atomic rename - Add startup cleanup for orphaned .tmp.* files from prior crashes - Replace all 8 unsafe fs.writeFileSync calls with atomicWrite() - updateUserHistory write also converted for consistency If a write or JSON.stringify throws, the original file is untouched because the tmp file is written first and only renamed on success. renameSync is atomic on both POSIX and NTFS (same filesystem). Fixes codepvg#182
Contributor
|
Thank you for submitting a pull request. Please ensure your changes comply with the project's contribution guidelines and that all workflow checks pass successfully. Formatting and Branching
|
jagdish-15
approved these changes
Jun 18, 2026
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.
Description
Prevent permanent data loss by replacing all in-place
fs.writeFileSync()calls with anatomicWrite()helper that writes to a temporary file first, then atomically renames. If the process crashes mid-write (GitHub Actions runner preemption, OOM, timeout), the original file is never touched.Linked Issue
Fixes #182
Changes Made
atomicWrite()helper — writesJSON.stringify(data, null, 2)to a.tmp.{pid}.{timestamp}file on the same directory, thenfs.renameSync()to the target path.renameSyncis atomic on both POSIX and NTFS (same filesystem).data/anddata/daily/for orphaned*.tmp.*files from previous crashes and deletes them.JSON.stringify(data, null, 2)is preserved inside the helper. Output files are byte-for-byte identical.Type of Change
Testing
Tested locally
Tested on mobile viewport (if applicable)
No console errors introduced
node --check scripts/sync-leaderboard.js— syntax check ✅npx prettier --check scripts/sync-leaderboard.js— formatting check ✅Checklist
npx prettier --write .before submittingfeature/*branch, not themainbranchScreenshots / Screen Recording
N/A — Backend-only change.