From 171a7f8ae34cec78df17a680e42878ca0b97c137 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 22 Nov 2026 16:05:54 +0100 Subject: [PATCH] crdt_sync_dart 0.14.2: per-instance temp file name Two FileLogPersistence instances on one file in one process (a Flutter UI engine and a headless engine) shared ..tmp, so one rename moved the other's temp away and that write threw PathNotFoundException: wake_alarm's bypass publish was lost to it on the emulator, 2026-11-22. A random per-instance token now goes beside the pid. Co-Authored-By: Claude Opus 5.5 --- crdt_sync_dart/lib/src/io_store.dart | 19 ++++++++++----- crdt_sync_dart/pubspec.yaml | 2 +- .../test/default_client_and_io_test.dart | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/crdt_sync_dart/lib/src/io_store.dart b/crdt_sync_dart/lib/src/io_store.dart index 6ef9acc..98b990d 100644 --- a/crdt_sync_dart/lib/src/io_store.dart +++ b/crdt_sync_dart/lib/src/io_store.dart @@ -1,4 +1,5 @@ import 'dart:io'; +import 'dart:math'; import 'store.dart'; @@ -7,10 +8,12 @@ import 'store.dart'; /// Writes atomically -- to a per-process temp file, then `rename`d over the /// real path -- so a concurrent reader (e.g. a background sync isolate writing /// while the foreground app also writes) never observes a half-written file. -/// The pid in the temp name keeps two processes from colliding; within one -/// instance, writes are serialized in issue order (last issued wins), so -/// overlapping calls (e.g. per-keystroke saves) never share the temp file -/// mid-flight. This generalizes diet_guard's hand-rolled `LogStorageService` +/// The pid plus a random per-instance token in the temp name keep two +/// writers from colliding: two processes, and two instances in one process +/// (a Flutter app's UI engine and a headless engine share a pid -- wake_alarm +/// lost a publish to exactly that, 2026-11-22). Within one instance, writes +/// are serialized in issue order (last issued wins), so overlapping calls +/// (e.g. per-keystroke saves) never share the temp file mid-flight. This generalizes diet_guard's hand-rolled `LogStorageService` /// write scheme. /// /// Lives behind the `package:crdt_sync/crdt_sync_io.dart` entrypoint, not the @@ -19,10 +22,14 @@ import 'store.dart'; class FileLogPersistence implements LogPersistence { /// Persists to [file]. The app chooses the path (e.g. via `path_provider`); /// the library takes no dependency on where app documents live. - FileLogPersistence(this._file); + FileLogPersistence(this._file) + : _token = Random.secure().nextInt(1 << 32).toRadixString(36); final File _file; + /// Tells this instance's temp file apart from another's in this process. + final String _token; + /// Completes when the most recently issued write has settled. Never /// rejects, so one failed write cannot block the ones queued after it. Future _tail = Future.value(); @@ -47,7 +54,7 @@ class FileLogPersistence implements LogPersistence { Future _writeNow(String text) async { await _file.parent.create(recursive: true); - final tmp = File('${_file.path}.$pid.tmp'); + final tmp = File('${_file.path}.$pid.$_token.tmp'); await tmp.writeAsString(text); await tmp.rename(_file.path); } diff --git a/crdt_sync_dart/pubspec.yaml b/crdt_sync_dart/pubspec.yaml index 0abad8a..99327c4 100644 --- a/crdt_sync_dart/pubspec.yaml +++ b/crdt_sync_dart/pubspec.yaml @@ -2,7 +2,7 @@ name: crdt_sync description: >- Shared CRDT merge scheme + GitHub-Contents-API sync transport for cross-device apps. Pure Dart, no Flutter dependency. -version: 0.14.1 +version: 0.14.2 publish_to: 'none' environment: diff --git a/crdt_sync_dart/test/default_client_and_io_test.dart b/crdt_sync_dart/test/default_client_and_io_test.dart index b6f7dcf..41c67e0 100644 --- a/crdt_sync_dart/test/default_client_and_io_test.dart +++ b/crdt_sync_dart/test/default_client_and_io_test.dart @@ -149,6 +149,30 @@ void main() { }, ); + test( + 'two instances on one file in one process never share a temp file', + () async { + // Regression (wake_alarm, 2026-11-22): a Flutter app's UI engine and + // a headless engine each hold a FileLogPersistence for the same log + // in one process, so one pid. Both wrote `..tmp`; the + // first rename moved it away and the other publish died with + // PathNotFoundException. + final file = File('${dir.path}/log.json'); + final a = FileLogPersistence(file); + final b = FileLogPersistence(file); + await Future.wait([ + for (var i = 0; i < 25; i++) ...[ + a.write('{"a":$i}'), + b.write('{"b":$i}'), + ], + ]); + expect(await a.read(), anyOf('{"a":24}', '{"b":24}')); + expect(dir.listSync().map((e) => e.uri.pathSegments.last), [ + 'log.json', + ]); + }, + ); + test( 'a failed write rejects its caller but does not block later writes', () async {