Skip to content

fix(persistence): serialize atomic file writes per path - #143

Merged
PapyElGringo merged 1 commit into
mainfrom
fix/persistence-write-race
Oct 14, 2026
Merged

PapyElGringo merged 1 commit into
mainfrom
fix/persistence-write-race

Conversation

@PapyElGringo

Copy link
Copy Markdown
Contributor

Problem

The debug log attached to #128 contains six unhandled PathNotFoundExceptions while persisting shell state, e.g.:

[ERROR:flutter/runtime/dart_initializer.cc(40)] Unhandled Exception: PathNotFoundException: Cannot rename file to
  '.../persistence/persistent_window_614b0038-....json', path = '.../persistence/persistent_window_614b0038-....json.tmp'
#2  writeFileAtomically.<anonymous closure> (package:shell/shared/util/file.dart:32:5)

[ERROR:flutter/runtime/dart_initializer.cc(40)] Unhandled Exception: PathNotFoundException: Cannot delete file, path =
  '.../persistence/workspace_9bdfc910-....json.tmp'
#2  writeFileAtomically.<anonymous closure> (package:shell/shared/util/file.dart:28:7)

(The same failure hit WindowManager.json and the workspace files.)

Root cause

writeFileAtomically wrote through a fixed temp name ($path.tmp) and its debounce timer was fire-and-forget. When a write was slow enough that the debounce fired again while the first flush was still in flight, the second flush deleted and renamed the temp file the first was still using — hence Cannot rename/delete file. Because the cache (_latestFileContents) was updated before the write, a failed write also made later identical writes look like no-ops, so state silently stopped persisting.

Fix

  • Serialize flushes per path through a small write chain, so a slow write can never overlap the next one and the shared $path.tmp is safe.
  • Treat persistence as best-effort: a transient I/O failure no longer surfaces as an unhandled async error and no longer breaks the chain for later writes. The pending content was already dropped from the cache, so the next call re-queues it.

Tests

  • New test/write_file_atomically_test.dart: coalescing + last-write-wins + no leftover .tmp, and a burst across several debounce windows asserts no unhandled errors and the final content.
  • flutter analyze clean, flutter test green, cargo check green.

Refs #128.

writeFileAtomically reused a fixed `$path.tmp` and its debounce timer was
fire-and-forget, so two flushes for the same path could overlap whenever a
write was slow. The second flush then deleted or renamed the temp file the
first was still using, throwing unhandled PathNotFoundException (`Cannot
rename/delete file`) and silently losing the write.

Serialize flushes per path through a write chain, and treat persistence as
best-effort so a transient I/O failure neither surfaces as an unhandled async
error nor breaks later writes.

Add a contract test covering coalescing, last-write-wins and temp-file cleanup.
@PapyElGringo
PapyElGringo merged commit b6f58bf into main Oct 14, 2026
2 checks passed
@PapyElGringo
PapyElGringo deleted the fix/persistence-write-race branch October 14, 2026 19:14
@PapyElGringo PapyElGringo mentioned this pull request Oct 14, 2026
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