MCP calls time out instead of wedging
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run

A sync stuck on a dead connection held the one-at-a-time queue forever. Abandoned board polls piled up as CLOSE-WAIT sockets until the process hit its fd limit, the tokens file could no longer be opened, and a valid token read as HTTP 403.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWXgrX1QxVjqMiz6KFs
This commit is contained in:
Krzysztof Rudnicki 2026-11-16 07:50:43 +01:00
parent cd50fd0ae2
commit de5ad7d2dd
3 changed files with 73 additions and 3 deletions

View File

@ -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<void> _tail = Future.value();
Future<T> _serial<T>(Future<T> Function() body) {
final run = _tail.then((_) => body());
final run = _tail.then((_) => body()).timeout(callTimeout);
_tail = run.then((_) {}, onError: (_) {});
return run;
}

View File

@ -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

View File

@ -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<SyncResult> sync({required bool push}) => _calls++ == 0
? Completer<SyncResult>().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<TimeoutException>()));
expect(await tasks.list({}), isNotNull);
},
);
}