Skip to content

fix: prevent data corruption in sync-leaderboard.js with atomic writes#211

Merged
jagdish-15 merged 1 commit into
codepvg:mainfrom
rishab11250:feature/atomic-writes-issue182
Jun 18, 2026
Merged

fix: prevent data corruption in sync-leaderboard.js with atomic writes#211
jagdish-15 merged 1 commit into
codepvg:mainfrom
rishab11250:feature/atomic-writes-issue182

Conversation

@rishab11250

Copy link
Copy Markdown
Contributor

Description

Prevent permanent data loss by replacing all in-place fs.writeFileSync() calls with an atomicWrite() 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 — writes JSON.stringify(data, null, 2) to a .tmp.{pid}.{timestamp} file on the same directory, then fs.renameSync() to the target path. renameSync is atomic on both POSIX and NTFS (same filesystem).
  • Startup cleanup — scans data/ and data/daily/ for orphaned *.tmp.* files from previous crashes and deletes them.
  • 8 writes converted — daily snapshot, overall.json, daily.json, weekly.json, monthly.json, changes.json, last-sync.json, and user history files (updateUserHistory).
  • No formatting changesJSON.stringify(data, null, 2) is preserved inside the helper. Output files are byte-for-byte identical.

Type of Change

  • Bug fix
  • New feature
  • UI/Visual update
  • Documentation update
  • Refactor

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

  • My code follows the project's coding style
  • I have formatted my code locally by running npx prettier --write . before submitting
  • I am submitting my PR from a dedicated feature/* branch, not the main branch
  • I have performed a self-review of my code
  • My changes generate no new warnings or errors
  • I have updated documentation if required
  • I have linked the relevant issue

Screenshots / Screen Recording

N/A — Backend-only change.

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
@github-actions

Copy link
Copy Markdown
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

  • Please confirm you have formatted your code locally using npx prettier --write ., or you can simply comment /format on this PR to have our bot do it for you!
  • Ensure this PR is made from a feature/* branch and not main.

Note: This project is currently maintained by a solo maintainer, so reviews and responses may sometimes take a little time. Thanks for your patience.

@jagdish-15 jagdish-15 merged commit 310756e into codepvg:main Jun 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] Data Corruption Risk: sync-leaderboard.js Writes 7 JSON Files In-Place with No Crash Recovery

2 participants