Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(mobile): a replaced relay connection can no longer speak for its machine

deede6de1a6c4b2
diff --git a/mobile/lib/ws/ws_pool.dart b/mobile/lib/ws/ws_pool.dart
index ce0993bbd..52a192ebd 100644
--- a/mobile/lib/ws/ws_pool.dart
+++ b/mobile/lib/ws/ws_pool.dart
@@ -67,7 +67,18 @@ class WsPool {
);
unawaited(current.close());
}
- final conn = WsConn(
+ // ⚠️ Once another connection holds this machine, this one speaks for nothing. A connection's
+ // last word — the `disconnected` from its socket closing — can land after its successor has
+ // come up, and it marked that working connection's machine as lost ("Connection lost.
+ // Reconnecting…"). The desktop's pool shuts a replaced connection up the same way. One that
+ // was only closed, with nothing in its place yet, still reports its end: the app reads it.
+ late final WsConn conn;
+ bool replaced() {
+ final owner = _conns[machineId];
+ return owner != null && !identical(owner, conn);
+ }
+
+ conn = WsConn(
wsBaseUrl: wsBaseUrl,
autonomousEnv: autonomousEnv,
relayCodecs: transportKind == WsTransportKind.cloudE2ee
@@ -78,12 +89,20 @@ class WsPool {
: null,
machineId: machineId,
accessTokenProvider: accessTokenProvider,
- onAuthFailure: onAuthFailure,
+ onAuthFailure: (message) {
+ if (!replaced()) onAuthFailure(message);
+ },
onLocalFailure: onLocalFailure == null
? null
- : (code, reason) => onLocalFailure!(machineId, code, reason),
- onEvent: (event) => onEvent(machineId, event),
- onStatus: (status) => onStatus(machineId, status),
+ : (code, reason) {
+ if (!replaced()) onLocalFailure!(machineId, code, reason);
+ },
+ onEvent: (event) {
+ if (!replaced()) return onEvent(machineId, event);
+ },
+ onStatus: (status) {
+ if (!replaced()) onStatus(machineId, status);
+ },
transportKind: transportKind,
localWsUri: localWsUri,
localApiKey: localApiKey,
diff --git a/mobile/test/ws/ws_pool_test.dart b/mobile/test/ws/ws_pool_test.dart
new file mode 100644
index 000000000..f8d182df4
--- /dev/null
+++ b/mobile/test/ws/ws_pool_test.dart
@@ -0,0 +1,124 @@
+import 'dart:async';
+
+import 'package:flutter_test/flutter_test.dart';
+
+import 'package:harness_mobile/core/models.dart';
+import 'package:harness_mobile/ws/ws_conn.dart';
+import 'package:harness_mobile/ws/ws_pool.dart';
+
+/// One connection per machine, and who may speak for that machine.
+void main() {
+ late List<String> events;
+ late WsPool pool;
+
+ setUp(() {
+ events = [];
+ pool = WsPool(
+ wsBaseUrl: 'ws://fixture.invalid',
+ autonomousEnv: 'test',
+ // Never resolves: nothing is ever dialled.
+ accessTokenProvider: (_, _) => Completer<String>().future,
+ onAuthFailure: (message) => events.add('auth:$message'),
+ onLocalFailure: (machineId, code, _) =>
+ events.add('local:$machineId:$code'),
+ onStatus: (machineId, status) =>
+ events.add('status:$machineId:${status.name}'),
+ onEvent: (machineId, event) =>
+ events.add('event:$machineId:${event['type']}'),
+ );
+ addTearDown(pool.closeAll);
+ });
+
+ test('a machine keeps its one connection while it is open', () {
+ final conn = pool.connFor('m');
+ expect(pool.connFor('m'), same(conn));
+ expect(pool['m'], same(conn));
+ expect(pool.has('m'), isTrue);
+ expect(pool.has('other'), isFalse);
+ expect(conn.machineId, 'm');
+ expect(conn.endpointKey, 'cloud:ws://fixture.invalid:test');
+ });
+
+ test('its callbacks carry the machine they are for', () async {
+ final conn = pool.connFor('m');
+ events.clear();
+ conn.onStatus(ConnectionStatus.connected);
+ conn.onLocalFailure!(4404, 'NO_PEER_LINK');
+ await conn.onEvent({'type': 'node_status'});
+ conn.onAuthFailure('gone');
+ expect(events, [
+ 'status:m:connected',
+ 'local:m:4404',
+ 'event:m:node_status',
+ 'auth:gone',
+ ]);
+ });
+
+ test('a closed connection is replaced, not handed back', () async {
+ final first = pool.connFor('m');
+ await first.close();
+ final second = pool.connFor('m');
+ expect(second, isNot(same(first)));
+ });
+
+ // Carried over from the desktop's pool: once another connection holds the machine, the one it
+ // replaced speaks for nothing. Its final `disconnected` landing late used to mark the new,
+ // working connection's machine as lost ("Connection lost. Reconnecting…").
+ test('a replaced connection cannot publish late callbacks', () async {
+ final old = pool.connFor('m');
+ await pool.closeMachine('m');
+ final current = pool.connFor('m');
+ events.clear();
+
+ old.onAuthFailure('old');
+ old.onLocalFailure!(4404, 'old');
+ await old.onEvent({'type': 'old'});
+ old.onStatus(ConnectionStatus.disconnected);
+ expect(events, isEmpty);
+
+ current.onStatus(ConnectionStatus.connected);
+ await current.onEvent({'type': 'current'});
+ expect(events, ['status:m:connected', 'event:m:current']);
+ });
+
+ test('closing a machine reports its end, then forgets it', () async {
+ pool.connFor('m');
+ events.clear();
+ await pool.closeMachine('m');
+ expect(events, ['status:m:disconnected']);
+ expect(pool.has('m'), isFalse);
+ // Closing what is not there is nothing.
+ await pool.closeMachine('m');
+ });
+
+ test('closing everything leaves nothing to reconnect', () async {
+ final a = pool.connFor('a');
+ final b = pool.connFor('b');
+ await pool.closeAll();
+ expect(a.isClosed, isTrue);
+ expect(b.isClosed, isTrue);
+ expect(pool.has('a') || pool.has('b'), isFalse);
+ pool.reconnectAll();
+ });
+
+ test('coming back to the foreground asks every connection to redial', () {
+ final a = pool.connFor('a');
+ final b = pool.connFor('b');
+ // Both are mid-dial (the credential never arrives), so there is nothing to redo.
+ pool.reconnectAll();
+ expect(a.isClosed || b.isClosed, isFalse);
+ });
+
+ test('a local connection keys on its loopback address', () {
+ final conn = pool.connFor(
+ 'm',
+ transportKind: WsTransportKind.localPlaintext,
+ localWsUri: Uri.parse('ws://127.0.0.1:1/ws'),
+ );
+ expect(conn.endpointKey, 'local:ws://127.0.0.1:1/ws');
+ expect(conn.isLocal, isTrue);
+ // A different transport for the same machine replaces it.
+ final cloud = pool.connFor('m');
+ expect(cloud, isNot(same(conn)));
+ });
+}