mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-23 10:26:31 +01:00
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 <file>.<pid>.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 <noreply@anthropic.com>
This commit is contained in:
parent
ceb6e42c27
commit
171a7f8ae3
@ -1,4 +1,5 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'store.dart';
|
import 'store.dart';
|
||||||
|
|
||||||
@ -7,10 +8,12 @@ import 'store.dart';
|
|||||||
/// Writes atomically -- to a per-process temp file, then `rename`d over the
|
/// 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
|
/// 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.
|
/// 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
|
/// The pid plus a random per-instance token in the temp name keep two
|
||||||
/// instance, writes are serialized in issue order (last issued wins), so
|
/// writers from colliding: two processes, and two instances in one process
|
||||||
/// overlapping calls (e.g. per-keystroke saves) never share the temp file
|
/// (a Flutter app's UI engine and a headless engine share a pid -- wake_alarm
|
||||||
/// mid-flight. This generalizes diet_guard's hand-rolled `LogStorageService`
|
/// 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.
|
/// write scheme.
|
||||||
///
|
///
|
||||||
/// Lives behind the `package:crdt_sync/crdt_sync_io.dart` entrypoint, not the
|
/// Lives behind the `package:crdt_sync/crdt_sync_io.dart` entrypoint, not the
|
||||||
@ -19,10 +22,14 @@ import 'store.dart';
|
|||||||
class FileLogPersistence implements LogPersistence {
|
class FileLogPersistence implements LogPersistence {
|
||||||
/// Persists to [file]. The app chooses the path (e.g. via `path_provider`);
|
/// Persists to [file]. The app chooses the path (e.g. via `path_provider`);
|
||||||
/// the library takes no dependency on where app documents live.
|
/// 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;
|
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
|
/// Completes when the most recently issued write has settled. Never
|
||||||
/// rejects, so one failed write cannot block the ones queued after it.
|
/// rejects, so one failed write cannot block the ones queued after it.
|
||||||
Future<void> _tail = Future<void>.value();
|
Future<void> _tail = Future<void>.value();
|
||||||
@ -47,7 +54,7 @@ class FileLogPersistence implements LogPersistence {
|
|||||||
|
|
||||||
Future<void> _writeNow(String text) async {
|
Future<void> _writeNow(String text) async {
|
||||||
await _file.parent.create(recursive: true);
|
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.writeAsString(text);
|
||||||
await tmp.rename(_file.path);
|
await tmp.rename(_file.path);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ name: crdt_sync
|
|||||||
description: >-
|
description: >-
|
||||||
Shared CRDT merge scheme + GitHub-Contents-API sync transport for
|
Shared CRDT merge scheme + GitHub-Contents-API sync transport for
|
||||||
cross-device apps. Pure Dart, no Flutter dependency.
|
cross-device apps. Pure Dart, no Flutter dependency.
|
||||||
version: 0.14.1
|
version: 0.14.2
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
|
|||||||
@ -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 `<file>.<pid>.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(
|
test(
|
||||||
'a failed write rejects its caller but does not block later writes',
|
'a failed write rejects its caller but does not block later writes',
|
||||||
() async {
|
() async {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user