Skip to content

Write redd-block-data.json atomically to prevent torn reads dropping blocks#43

Open
kasnder wants to merge 2 commits into
mainfrom
claude/musing-kirch-e57e60
Open

Write redd-block-data.json atomically to prevent torn reads dropping blocks#43
kasnder wants to merge 2 commits into
mainfrom
claude/musing-kirch-e57e60

Conversation

@kasnder

@kasnder kasnder commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Problem

redd-block-data.json is the single source of truth for "what is blocked right now", and it is re-read continuously by independent readers:

  • the macOS Automation watcher (1 s tick),
  • the browser-spawned native-messaging host (2 s mtime poll — mtime changes at the start of a write, so it actively tends to read mid-write),
  • the compliance enforcer (5 s tick).

Every writer used a plain truncate-and-write (fs::write), so a reader could observe a half-written file. derive_payload() treats a JSON parse failure as an empty blocklist, which fails open: the extension is pushed an empty list and clears blocking, and the Automation watcher's "no domains → restore parked tabs" logic un-parks tabs from the block page. Users could hit this window on every block edit or settings save.

Fix

New write_data_file_atomic() helper in commands/data.rs: write to a uniquely-named temp file in the same directory, sync_all(), apply the shared-file permissions to the temp file (rename gives the destination the temp file's mode), then rename() over the destination — atomic on both APFS and NTFS, and Windows readers using std's default share flags are unaffected by the swap. On failure the temp file is cleaned up.

All data-file writers now go through it:

  • save_data and the three load_data migration/EULA-normalization writes (commands/data.rs)
  • set_enforcement_enabled (commands/enforcement_toggle.rs)
  • set_extension_grace_seconds (commands/grace.rs)
  • set_blocking_method (commands/blocking_method_cmd.rs)

(The settings commands previously wrote without re-applying shared permissions; going through the helper also fixes that.)

Also includes a one-line fix for web_automation::tests::file_url_encodes_spaces, which had been failing since the product rename (expected URL still said "ReDD Block.app").

Testing

  • cargo check clean (only pre-existing warnings)
  • cargo test --lib: 29 passed, 0 failed

Not addressed (follow-ups)

  • derive_payload() still fails open on a genuinely corrupt/missing file; keeping a last-known-good payload would be a further hardening step.
  • preserve_backend_settings / the settings commands still do unlocked read-modify-write, so a lost-update race between concurrent writers remains possible (much rarer than the torn-read issue fixed here).

🤖 Generated with Claude Code

…blocks

The data file is re-read continuously by the Automation watcher (1 s),
the browser-spawned native host (2 s mtime poll), and the enforcer
(5 s). Replacing it with a plain truncate-and-write let those readers
observe a half-written file: the JSON parse fails, derive_payload()
returns an empty blocklist, and blocking momentarily drops — the
extension clears its rules and the Automation watcher un-parks tabs
from the block page. The native host's mtime poll made this likely,
since mtime changes at the start of a write.

All writers (save_data, the load_data EULA/location migrations, and
the enforcement/grace/blocking-method settings commands) now go
through a single helper that writes to a temp file in the same
directory, fsyncs, applies the shared permissions, and renames over
the destination — atomic on both APFS and NTFS, so readers see either
the old or the new complete file.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The test fed path_to_file_url a "ReDD Blocker.app" path but still
asserted the pre-rename "ReDD%20Block.app" URL.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@kasnder kasnder requested a review from reddfocus July 7, 2026 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant