Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lib/core/mcp/mcp_server_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,21 @@ class McpServerController {
}
}

Future<void> start() async {
/// Runs [op] after every earlier start / stop has finished. `start` awaits
/// between its `_host != null` check and the assignment, so two overlapping
/// calls (a toggle clicked twice, `startIfEnabled` racing the settings page)
/// each built a host and the first one was leaked, still listening (#1563).
Future<void> _lifecycle = Future<void>.value();

Future<T> _serialized<T>(Future<T> Function() op) {
final run = _lifecycle.then((_) => op());
_lifecycle = run.then<void>((_) {}, onError: (Object _) {});
return run;
}

Future<void> start() => _serialized(_start);

Future<void> _start() async {
if (_host != null) return;
final version = await _version();
final host = McpSocketHost(
Expand All @@ -125,7 +139,9 @@ class McpServerController {
}

/// Disconnects clients and removes the endpoint file.
Future<void> stop() async {
Future<void> stop() => _serialized(_stop);

Future<void> _stop() async {
final host = _host;
_host = null;
for (final s in _servers.toList()) {
Expand Down Expand Up @@ -166,11 +182,11 @@ class McpServerController {

/// Stops and starts again with a new token; connected clients must
/// reconnect (their `querya-mcp` reads the new endpoint file).
Future<void> regenerateToken() async {
if (_host == null) return;
await stop();
await start();
}
Future<void> regenerateToken() => _serialized(() async {
if (_host == null) return;
await _stop();
await _start();
});

void _publish(McpSocketHost host) {
if (_host != host && _host != null) return;
Expand Down
41 changes: 41 additions & 0 deletions test/core/mcp/mcp_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,47 @@ void main() {
expect(msg['error']['message'], kMcpAppNotRunning);
});

test('overlapping start calls build one host, and stop closes it (#1563)',
() async {
final ports = <int>{};
void note() {
final port = controller.status.value.port;
if (port != null) ports.add(port);
}

controller.status.addListener(note);
addTearDown(() => controller.status.removeListener(note));

await Future.wait([controller.start(), controller.start(), controller.start()]);
note();
expect(ports, hasLength(1));

await controller.stop();
for (final port in ports) {
await expectLater(
Socket.connect(InternetAddress.loopbackIPv4, port,
timeout: const Duration(milliseconds: 500)),
throwsA(isA<SocketException>()),
reason: 'no host may still listen on $port',
);
}
});

test('stop waits for a start that is still running', () async {
final starting = controller.start();
await controller.stop();
await starting;
expect(controller.status.value.running, isFalse);
expect(await endpoint.exists(), isFalse);
});

test('a start after a stop works again', () async {
await controller.start();
await controller.stop();
await controller.start();
expect(controller.status.value.running, isTrue);
});

test('stopping the server disconnects a live client', () async {
await controller.start();
final s = await _Session.open(endpoint);
Expand Down
Loading