diff --git a/lib/mcp/task_service.dart b/lib/mcp/task_service.dart index 68292f7..ad4fde8 100644 --- a/lib/mcp/task_service.dart +++ b/lib/mcp/task_service.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:todo/cli/cli_args.dart'; import 'package:todo/cli/cli_session.dart'; import 'package:todo/cli/note_lookup.dart'; @@ -11,14 +13,23 @@ import 'package:todo/mcp/task_split.dart'; class TaskService { /// Serves [_session]'s notes; every call pulls first, writes then push. /// [board] is the todoImplementer board `new_note` checks and starts on. - TaskService(this._session, {this._board}); + /// + /// A call that outlives [callTimeout] (a sync stuck on a dead connection) + /// fails with a TimeoutException instead of holding every later call, so + /// the queue never wedges and abandoned requests do not pile up sockets. + TaskService( + this._session, { + this._board, + this.callTimeout = const Duration(seconds: 60), + }); final CliSession _session; final BoardClient? _board; + final Duration callTimeout; Future _tail = Future.value(); Future _serial(Future Function() body) { - final run = _tail.then((_) => body()); + final run = _tail.then((_) => body()).timeout(callTimeout); _tail = run.then((_) {}, onError: (_) {}); return run; } diff --git a/pubspec.yaml b/pubspec.yaml index d8a691a..da9e80f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.88+1 +version: 1.0.89+1 environment: sdk: ^3.12.2 diff --git a/test/mcp/task_service_wedge_test.dart b/test/mcp/task_service_wedge_test.dart new file mode 100644 index 0000000..f0d880b --- /dev/null +++ b/test/mcp/task_service_wedge_test.dart @@ -0,0 +1,59 @@ +// A sync that never answers (dead connection after boot or a network blip) +// must not wedge the one-at-a-time queue: every later call, including the +// board's 4 s poll, used to wait behind it forever, abandoned connections +// piled up until the process ran out of file descriptors, and the tokens +// file became unreadable, so a valid token read as HTTP 403. +import 'dart:async'; + +import 'package:crdt_sync/crdt_sync.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:http/http.dart' as http; +import 'package:todo/cli/cli_session.dart'; +import 'package:todo/data/note_repository.dart'; +import 'package:todo/mcp/task_service.dart'; +import 'package:todo/sync/sync_service.dart'; + +/// Hangs the first [sync] forever; later ones return at once. +class _HangsOnce extends CliSession { + _HangsOnce(NoteRepository repo, FirebaseRestClient firebase) + : super('unused', repo, firebase); + + var _calls = 0; + + @override + Future sync({required bool push}) => _calls++ == 0 + ? Completer().future + : Future.value(const SyncResult(mergedDevices: 0, pushed: false)); +} + +Future<_HangsOnce> _session() async { + final firebase = FirebaseRestClient( + databaseUrl: 'https://example.test', + auth: FirebaseTokenProvider( + apiKey: 'k', + store: InMemoryCredentialStore( + FirebaseCredentials( + idToken: 'id', + refreshToken: 'rt', + expiresAt: DateTime.utc(2099), + ), + ), + ), + httpClient: http.Client(), + ); + return _HangsOnce(await NoteRepository.openInMemory(), firebase); +} + +void main() { + test( + '[T-wedge] a hung sync times out and the next call still runs', + () async { + final tasks = TaskService( + await _session(), + callTimeout: const Duration(milliseconds: 200), + ); + await expectLater(tasks.list({}), throwsA(isA())); + expect(await tasks.list({}), isNotNull); + }, + ); +}