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 USB dial stalls and recover backend machine discovery

deehw0d49f6909803
diff --git a/cli/src/cable/__fixtures__/serialIdle.ts b/cli/src/cable/__fixtures__/serialIdle.ts
new file mode 100644
index 000000000..9a9a356bc
--- /dev/null
+++ b/cli/src/cable/__fixtures__/serialIdle.ts
@@ -0,0 +1,37 @@
+// Runs in a separate process with ONE libuv worker. An idle blocking tty read would prevent
+// both DNS and file I/O from completing, even though the event loop and existing sockets work.
+import assert from 'node:assert/strict'
+import { lookup } from 'node:dns/promises'
+import { stat } from 'node:fs/promises'
+import { createServer } from 'node:http'
+import { SerialLink } from '../serial.js'
+
+const server = createServer((_req, res) => res.end('ok'))
+await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
+const address = server.address() as { port: number }
+const limit = <T>(work: Promise<T>) => Promise.race([
+ work,
+ new Promise<never>((_, reject) => setTimeout(() => reject(new Error('I/O blocked by idle serial port')), 1_000).unref()),
+])
+
+try {
+ for (let i = 0; i < 8; i++) {
+ let closed = 0
+ const link = await SerialLink.open(process.argv[2]!, () => {}, () => closed++)
+ await new Promise((resolve) => setTimeout(resolve, 20))
+ await limit(Promise.all([
+ lookup('localhost'),
+ stat(process.execPath),
+ fetch(`http://localhost:${address.port}`, { signal: AbortSignal.timeout(2_000) }).then((res) => res.text()),
+ ]))
+ await limit(Promise.all([link.close('silence'), link.close('silence')]))
+ assert.equal(closed, 1)
+ assert.equal(link.isOpen, false)
+ }
+ process.stdout.write('8 idle reopen cycles: DNS, HTTP, file I/O and close succeeded\n')
+ process.exit(0)
+} catch (error) {
+ console.error(error)
+ // The regression leaves a native read blocked forever; let the parent test finish in that case.
+ process.exit(1)
+}
diff --git a/cli/src/cable/serial.real.spec.ts b/cli/src/cable/serial.real.spec.ts
new file mode 100644
index 000000000..cff6dc16b
--- /dev/null
+++ b/cli/src/cable/serial.real.spec.ts
@@ -0,0 +1,38 @@
+import { execFile, spawn, spawnSync } from 'node:child_process'
+import { once } from 'node:events'
+import { mkdtemp, rm } from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { createInterface } from 'node:readline'
+import { fileURLToPath } from 'node:url'
+import { promisify } from 'node:util'
+import { build } from 'esbuild'
+import { expect, it } from 'vitest'
+
+const hasPty = process.platform !== 'win32' && spawnSync('python3', ['-c', 'import pty']).status === 0
+
+it.skipIf(!hasPty)('keeps DNS and file I/O working across repeated silent tty closes with one worker', async () => {
+ const scratch = await mkdtemp(join(tmpdir(), 'serial-idle-'))
+ // The master stays open and sends nothing. This exercises the real kernel tty and real fs reads,
+ // without touching a USB device or requiring a native production dependency.
+ const python = spawn('python3', ['-u', '-c',
+ 'import os, pty, sys\nmaster, slave = pty.openpty()\nprint(os.ttyname(slave), flush=True)\nsys.stdin.read()\n',
+ ], { stdio: ['pipe', 'pipe', 'pipe'] })
+ const lines = createInterface({ input: python.stdout })
+ try {
+ const [path] = await once(lines, 'line')
+ const worker = join(scratch, 'serial-idle.mjs')
+ await build({
+ entryPoints: [fileURLToPath(new URL('./__fixtures__/serialIdle.ts', import.meta.url))],
+ outfile: worker, bundle: true, platform: 'node', format: 'esm', target: 'node20',
+ })
+ const { stdout } = await promisify(execFile)(process.execPath, [worker, path], {
+ env: { ...process.env, UV_THREADPOOL_SIZE: '1' }, timeout: 10_000,
+ })
+ expect(stdout).toContain('8 idle reopen cycles: DNS, HTTP, file I/O and close succeeded')
+ } finally {
+ lines.close()
+ python.kill()
+ await rm(scratch, { recursive: true, force: true })
+ }
+}, 15_000)
diff --git a/cli/src/cable/serial.spec.ts b/cli/src/cable/serial.spec.ts
new file mode 100644
index 000000000..3f04b80e6
--- /dev/null
+++ b/cli/src/cable/serial.spec.ts
@@ -0,0 +1,75 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+const io = vi.hoisted(() => ({ open: vi.fn(), configure: vi.fn() }))
+vi.mock('node:fs/promises', () => ({ open: io.open }))
+vi.mock('node:child_process', () => ({ execFile: io.configure }))
+
+import { SerialLink } from './serial.js'
+
+const again = () => Object.assign(new Error('busy'), { code: 'EAGAIN' })
+
+describe('nonblocking serial link', () => {
+ beforeEach(() => {
+ vi.useFakeTimers()
+ io.configure.mockImplementation((_cmd, _args, callback) => callback(null, '', ''))
+ })
+ afterEach(() => { vi.useRealTimers(); vi.clearAllMocks() })
+
+ it('preserves frame order across short writes and backpressure', async () => {
+ const sent: number[] = []
+ let calls = 0
+ const handle = {
+ read: vi.fn().mockRejectedValue(again()),
+ write: vi.fn(async (bytes: Uint8Array, offset: number, length: number) => {
+ if (++calls % 2 === 0) throw again()
+ const count = Math.min(2, length)
+ sent.push(...bytes.subarray(offset, offset + count))
+ return { bytesWritten: count }
+ }),
+ close: vi.fn().mockResolvedValue(undefined),
+ }
+ io.open.mockResolvedValue(handle)
+ const link = await SerialLink.open('/dev/fake', vi.fn(), vi.fn())
+ const writes = Promise.all([link.write(Buffer.from([1, 2, 3, 4, 5])), link.write(Buffer.from([6, 7, 8]))])
+ await vi.advanceTimersByTimeAsync(50)
+ await writes
+ expect(sent).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
+ await link.close()
+ })
+
+ it('stops retrying backpressured writes when closed and rejects queued frames', async () => {
+ const handle = {
+ read: vi.fn().mockRejectedValue(again()),
+ write: vi.fn().mockRejectedValue(again()),
+ close: vi.fn().mockResolvedValue(undefined),
+ }
+ io.open.mockResolvedValue(handle)
+ const closed = vi.fn()
+ const link = await SerialLink.open('/dev/fake', vi.fn(), closed)
+ const writes = Promise.allSettled([link.write(Buffer.from('first')), link.write(Buffer.from('second'))])
+ await vi.advanceTimersByTimeAsync(10)
+ await Promise.all([link.close(), link.close()])
+ const before = handle.write.mock.calls.length
+ await vi.advanceTimersByTimeAsync(50)
+ expect((await writes).map((result) => result.status)).toEqual(['rejected', 'rejected'])
+ expect(handle.write).toHaveBeenCalledTimes(before)
+ expect(handle.close).toHaveBeenCalledTimes(1)
+ expect(closed).toHaveBeenCalledTimes(1)
+ })
+
+ it('does not deliver an old read after the link is closed', async () => {
+ let finish!: (result: { bytesRead: number }) => void
+ const handle = {
+ read: vi.fn((buf: Buffer) => { buf[0] = 42; return new Promise((resolve) => { finish = resolve }) }),
+ close: vi.fn().mockResolvedValue(undefined),
+ }
+ io.open.mockResolvedValue(handle)
+ const received = vi.fn()
+ const link = await SerialLink.open('/dev/fake', received, vi.fn())
+ const closing = link.close()
+ finish({ bytesRead: 1 })
+ await closing
+ expect(received).not.toHaveBeenCalled()
+ expect(handle.read).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/cli/src/cable/serial.ts b/cli/src/cable/serial.ts
index aa51a16f9..4a1f3efe8 100644
--- a/cli/src/cable/serial.ts
+++ b/cli/src/cable/serial.ts
@@ -4,12 +4,19 @@
// dependencies; a serial library would be its first native module and would drag node-gyp, prebuilds and
// a per-platform release matrix into a distribution that is currently a download. A tty is a file, `stty`
// puts it in raw mode, and that is the whole of what this needs.
-import { exec } from 'node:child_process'
+import { execFile } from 'node:child_process'
import { constants, existsSync, readFileSync, readdirSync } from 'node:fs'
import { open, type FileHandle } from 'node:fs/promises'
import { promisify } from 'node:util'
-const execFile = promisify(exec)
+const runFile = promisify(execFile)
+
+const waitForPort = () => new Promise<void>((resolve) => setTimeout(resolve, 5))
+
+function wouldBlock(error: unknown): boolean {
+ const code = (error as NodeJS.ErrnoException).code
+ return code === 'EAGAIN' || code === 'EWOULDBLOCK' || code === 'EINTR'
+}
/** The SoC's own USB peripheral. The dial exposes this and nothing else — measured, see findDialPort. */
export const DIAL_VENDOR_ID = 0x303a
@@ -48,7 +55,7 @@ function depthOf(line: string): number {
async function findDarwin(): Promise<DialPort | null> {
let dump: string
try {
- const { stdout } = await execFile('ioreg -p IOService -w0 -l', { maxBuffer: 64 * 1024 * 1024 })
+ const { stdout } = await runFile('ioreg', ['-p', 'IOService', '-w0', '-l'], { maxBuffer: 64 * 1024 * 1024 })
dump = stdout
} catch {
return null
@@ -123,6 +130,7 @@ export class SerialLink {
) {}
private closed = false
+ private closePromise: Promise<void> | null = null
static async open(
path: string,
@@ -137,7 +145,7 @@ export class SerialLink {
// carrier — which is what unplugging a USB serial device looks like — does not hang the port up
// underneath us.
const flag = process.platform === 'darwin' ? '-f' : '-F'
- await execFile(`stty ${flag} ${JSON.stringify(path)} raw clocal -echo -echoe -echok -echoctl -echoke`)
+ await runFile('stty', [flag, path, 'raw', 'clocal', '-echo', '-echoe', '-echok', '-echoctl', '-echoke', 'min', '1', 'time', '0'])
// O_NOCTTY IS LOAD-BEARING, AND ITS ABSENCE KILLED THE DAEMON.
//
@@ -149,7 +157,12 @@ export class SerialLink {
//
// Measured 2026-08-24: the daemon died at the exact second the cable came out, every time, and the
// dial then greeted an empty room until it timed out and showed no agents.
- const handle = await open(path, constants.O_RDWR | constants.O_NOCTTY)
+ // An async FileHandle read still occupies a libuv worker until the syscall returns. With a
+ // blocking tty, close() waits for that read forever when the dial is silent. Reopening strands
+ // another worker each time; after four opens even DNS lookup and unrelated file I/O stop.
+ // O_NONBLOCK makes idle reads/writes return EAGAIN so they can yield without holding a worker,
+ // and lets close finish without needing the device to send another byte.
+ const handle = await open(path, constants.O_RDWR | constants.O_NOCTTY | constants.O_NONBLOCK)
const link = new SerialLink(path, handle, onData, onClosed)
void link.readLoop()
return link
@@ -160,6 +173,7 @@ export class SerialLink {
while (!this.closed) {
try {
const { bytesRead } = await this.handle.read(buf, 0, buf.length, null)
+ if (this.closed) return
if (bytesRead > 0) this.onData(Buffer.from(buf.subarray(0, bytesRead)))
// A zero-byte read on a tty means EOF, which for a USB CDC device means it went away.
else if (bytesRead === 0) return this.fail('end of stream')
@@ -167,8 +181,8 @@ export class SerialLink {
const code = (err as NodeJS.ErrnoException).code
if (this.closed) return
// EAGAIN is a port with nothing to say, not a broken one.
- if (code === 'EAGAIN') {
- await new Promise((r) => setTimeout(r, 5))
+ if (wouldBlock(err)) {
+ await waitForPort()
continue
}
return this.fail(code ?? String(err))
@@ -177,10 +191,7 @@ export class SerialLink {
}
private fail(why: string): void {
- if (this.closed) return
- this.closed = true
- void this.handle.close().catch(() => {})
- this.onClosed(why)
+ void this.close(why)
}
/**
@@ -211,9 +222,15 @@ export class SerialLink {
if (this.closed) throw new Error('port closed')
let off = 0
while (off < bytes.length) {
- const { bytesWritten } = await this.handle.write(bytes, off, bytes.length - off)
- if (bytesWritten <= 0) throw new Error('short write')
- off += bytesWritten
+ if (this.closed) throw new Error('port closed')
+ try {
+ const { bytesWritten } = await this.handle.write(bytes, off, bytes.length - off)
+ if (bytesWritten <= 0) throw new Error('short write')
+ off += bytesWritten
+ } catch (err) {
+ if (this.closed || !wouldBlock(err)) throw err
+ await waitForPort()
+ }
}
}
@@ -221,10 +238,10 @@ export class SerialLink {
return !this.closed
}
- async close(why = 'closed'): Promise<void> {
- if (this.closed) return
+ close(why = 'closed'): Promise<void> {
+ if (this.closePromise) return this.closePromise
this.closed = true
- await this.handle.close().catch(() => {})
- this.onClosed(why)
+ this.closePromise = this.handle.close().catch(() => {}).then(() => this.onClosed(why))
+ return this.closePromise
}
}
diff --git a/desktop/lib/api/api_client.dart b/desktop/lib/api/api_client.dart
index 3ecd444c0..b2274f617 100644
--- a/desktop/lib/api/api_client.dart
+++ b/desktop/lib/api/api_client.dart
@@ -93,6 +93,21 @@ bool isUnauthorizedError(Object error) =>
error is DioException && error.response?.statusCode == 401 ||
error is ApiException && error.status == 401;
+/// The local daemon can answer normally while its separate backend request fails.
+/// Those gateway errors need recovery just as a broken loopback connection does.
+bool isTransientApiError(Object error) {
+ if (error is ApiException) {
+ return const {502, 503, 504}.contains(error.status);
+ }
+ return error is DioException &&
+ const {
+ DioExceptionType.connectionError,
+ DioExceptionType.connectionTimeout,
+ DioExceptionType.sendTimeout,
+ DioExceptionType.receiveTimeout,
+ }.contains(error.type);
+}
+
/// The sentence a failed local-CLI call earns on an error strip. A raw
/// `DioException` is a paragraph about `RequestOptions.receiveTimeout` — true,
/// and useless to the person reading it: what they need is which leg failed.
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index 620a1a80b..e87ad6f2d 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -361,6 +361,11 @@ class AppNotifier extends ChangeNotifier {
// successful bootstrap (see `ensureCliDaemonReady`), cancelled on dispose. Cancelling only stops this
// Dart-side loop; the daemon itself self-daemonizes and must keep running after the app quits.
Timer? _daemonSupervisionTimer;
+ // Backend REST can fail while the daemon and its WebSocket remain ready. Recover that list
+ // independently, with capped backoff and the same in-flight request as a manual retry.
+ Timer? _machineRecoveryTimer;
+ int _machineRecoveryAttempts = 0;
+ String? _machineLoadError;
/// The last [ensureCliDaemonReady] did not reach a ready daemon — the one
/// error the supervisor's ready transition is allowed to retry away.
@@ -386,6 +391,8 @@ class AppNotifier extends ChangeNotifier {
!_disposed && revision == _authRevision;
int _invalidateAuthWork() {
+ _stopMachineRecovery();
+ _machineLoadError = null;
_resetLoginBrowser();
_loginAuthorized = false;
_profileInFlight = null;
@@ -2123,8 +2130,7 @@ class AppNotifier extends ChangeNotifier {
await refreshMachines();
} catch (error) {
if (!_authWorkCurrent(revision)) return;
- _lastError = 'Could not load machines: ${describeApiError(error)}';
- _lastErrorRetryable = true;
+ _reportMachineLoadError(error);
}
if (_authWorkCurrent(revision)) notifyListeners();
}
@@ -2655,6 +2661,23 @@ class AppNotifier extends ChangeNotifier {
}
try {
await _refreshMachines(revision);
+ if (_authWorkCurrent(revision)) {
+ _stopMachineRecovery();
+ if (_lastError != null && _lastError == _machineLoadError) {
+ _lastError = null;
+ notifyListeners();
+ }
+ _machineLoadError = null;
+ }
+ } catch (error) {
+ if (_authWorkCurrent(revision)) {
+ if (isTransientApiError(error)) {
+ _scheduleMachineRecovery(revision);
+ } else {
+ _stopMachineRecovery();
+ }
+ }
+ rethrow;
} finally {
// Said out loud: the list's own notify fires before this, so a flag
// dropped silently here would leave the rail on its placeholders.
@@ -2665,6 +2688,49 @@ class AppNotifier extends ChangeNotifier {
}
}
+ void _stopMachineRecovery() {
+ _machineRecoveryTimer?.cancel();
+ _machineRecoveryTimer = null;
+ _machineRecoveryAttempts = 0;
+ }
+
+ void _scheduleMachineRecovery(int revision) {
+ if (!_authWorkCurrent(revision) ||
+ status != AppStatus.authenticated ||
+ _machineRecoveryTimer != null) {
+ return;
+ }
+ const seconds = [2, 4, 8, 16, 30];
+ final delay = seconds[_machineRecoveryAttempts];
+ if (_machineRecoveryAttempts < seconds.length - 1) {
+ _machineRecoveryAttempts++;
+ }
+ _machineRecoveryTimer = Timer(Duration(seconds: delay), () {
+ _machineRecoveryTimer = null;
+ if (_authWorkCurrent(revision) && status == AppStatus.authenticated) {
+ unawaited(
+ _retryMachines(automatic: true).whenComplete(() {
+ // A timer can join a manual retry already in progress. Keep recovering if that run
+ // stopped at the daemon gate; success and non-transient errors reset the counter.
+ if (_machineRecoveryAttempts > 0) {
+ _scheduleMachineRecovery(revision);
+ }
+ }),
+ );
+ }
+ });
+ }
+
+ void _reportMachineLoadError(Object error, {bool automatic = false}) {
+ final message = 'Could not load machines: ${describeApiError(error)}';
+ // A recovery must not replace a later agent error or redisplay a dismissed strip.
+ if (!automatic || (_lastError != null && _lastError == _machineLoadError)) {
+ _lastError = message;
+ _lastErrorRetryable = true;
+ }
+ _machineLoadError = message;
+ }
+
Future<void> _refreshMachines(int revision) async {
final discovery = _discovery;
// The CLI computer id is the local identity source of truth. The loopback
@@ -2739,7 +2805,7 @@ class AppNotifier extends ChangeNotifier {
api.machines,
maxAttempts: 2,
initialDelay: const Duration(milliseconds: 500),
- isRetryable: (error) => error is DioException,
+ isRetryable: (error) => error is DioException && isTransientApiError(error),
);
void _startOfflineRetry(MachineState machine) {
@@ -3038,12 +3104,14 @@ class AppNotifier extends ChangeNotifier {
/// the error strip's own retry.
Future<void>? _retryInFlight;
- Future<void> retryMachines() {
+ Future<void> retryMachines() => _retryMachines(automatic: false);
+
+ Future<void> _retryMachines({required bool automatic}) {
if (_disposed) return Future<void>.value();
final inFlight = _retryInFlight;
if (inFlight != null) return inFlight;
late final Future<void> run;
- run = _performRetryMachines().whenComplete(() {
+ run = _performRetryMachines(automatic: automatic).whenComplete(() {
if (identical(_retryInFlight, run)) {
_retryInFlight = null;
if (!_disposed) notifyListeners();
@@ -3054,7 +3122,7 @@ class AppNotifier extends ChangeNotifier {
return run;
}
- Future<void> _performRetryMachines() async {
+ Future<void> _performRetryMachines({required bool automatic}) async {
final revision = _authRevision;
if (!_authWorkCurrent(revision)) return;
// Re-verify the daemon first: a retry that skips straight to `refreshMachines()` can hit
@@ -3063,8 +3131,12 @@ class AppNotifier extends ChangeNotifier {
await ensureCliDaemonReady();
} catch (error) {
if (!_authWorkCurrent(revision)) return;
- _lastError = '$error';
- _lastErrorRetryable = true;
+ if (automatic) {
+ _scheduleMachineRecovery(revision);
+ } else {
+ _lastError = '$error';
+ _lastErrorRetryable = true;
+ }
notifyListeners();
return;
}
@@ -3075,11 +3147,10 @@ class AppNotifier extends ChangeNotifier {
try {
await refreshMachines();
if (!_authWorkCurrent(revision)) return;
- _lastError = null;
+ if (!automatic) _lastError = null;
} catch (error) {
if (!_authWorkCurrent(revision)) return;
- _lastError = 'Could not load machines: ${describeApiError(error)}';
- _lastErrorRetryable = true;
+ _reportMachineLoadError(error, automatic: automatic);
notifyListeners();
return;
}
@@ -5743,6 +5814,7 @@ class AppNotifier extends ChangeNotifier {
_localGitProjects.dispose();
sessionPreviews.dispose();
_disposed = true;
+ _stopMachineRecovery();
if (signingIn) cliLogin.cancel();
_closedHistory.clear();
_daemonSupervisionTimer?.cancel();
diff --git a/desktop/test/api_client_test.dart b/desktop/test/api_client_test.dart
index 349a5233e..2766b61d3 100644
--- a/desktop/test/api_client_test.dart
+++ b/desktop/test/api_client_test.dart
@@ -101,4 +101,36 @@ void main() {
},
);
});
+
+ test('only transient connection and gateway errors qualify for recovery', () {
+ for (final status in [502, 503, 504]) {
+ expect(
+ isTransientApiError(ApiException('unavailable', status: status)),
+ isTrue,
+ );
+ }
+ for (final status in [400, 401, 403, 404]) {
+ expect(
+ isTransientApiError(ApiException('refused', status: status)),
+ isFalse,
+ );
+ }
+ final options = RequestOptions(path: '/api/machines');
+ expect(
+ isTransientApiError(
+ DioException(
+ requestOptions: options,
+ type: DioExceptionType.connectionTimeout,
+ ),
+ ),
+ isTrue,
+ );
+ expect(
+ isTransientApiError(
+ DioException(requestOptions: options, type: DioExceptionType.cancel),
+ ),
+ isFalse,
+ );
+ expect(isTransientApiError(StateError('invalid response')), isFalse);
+ });
}
diff --git a/desktop/test/profile_startup_test.dart b/desktop/test/profile_startup_test.dart
index ee069c713..c697ea14c 100644
--- a/desktop/test/profile_startup_test.dart
+++ b/desktop/test/profile_startup_test.dart
@@ -235,6 +235,118 @@ void main() {
expect(app.machinesRefreshing, isFalse);
});
+ testWidgets(
+ 'startup recovers a backend timeout even while the daemon stays ready',
+ (tester) async {
+ final start = app.bootstrap();
+ await tester.pump();
+ api.profiles.single.complete(_profile('current'));
+ api.lists.single.completeError(
+ ApiException('Backend unreachable', status: 502),
+ );
+ await tester.pump();
+ await start;
+ expect(app.lastError, contains('Backend unreachable'));
+
+ await tester.pump(const Duration(milliseconds: 1999));
+ expect(api.lists, hasLength(1));
+ await tester.pump(const Duration(milliseconds: 1));
+ expect(api.lists, hasLength(2));
+ // Manual retry joins the automatic request rather than duplicating it.
+ final joined = app.retryMachines();
+ await tester.pump(const Duration(seconds: 30));
+ expect(api.lists, hasLength(2));
+ api.lists.last.complete([_machine]);
+ await tester.pump();
+ await joined;
+ expect(app.lastError, isNull);
+ expect(app.machineStates['fixture']!.agents, hasLength(1));
+ expect(app.daemonChecks, 2);
+ await tester.pump(const Duration(seconds: 35));
+ expect(api.lists, hasLength(2));
+ disposeApp();
+ },
+ );
+
+ testWidgets(
+ 'machine recovery backs off and keeps a dismissed error dismissed',
+ (tester) async {
+ app.status = AppStatus.authenticated;
+ final first = app.retryMachines();
+ await tester.pump();
+ api.profiles.single.complete(_profile('current'));
+ api.lists.single.completeError(
+ ApiException('Backend timeout', status: 504),
+ );
+ await tester.pump();
+ await first;
+ app.dismissError();
+ await tester.pump(const Duration(seconds: 2));
+ expect(api.lists, hasLength(2));
+ api.lists.last.completeError(
+ ApiException('Still unavailable', status: 503),
+ );
+ await tester.pump();
+ expect(app.lastError, isNull);
+ await tester.pump(const Duration(seconds: 3));
+ expect(api.lists, hasLength(2));
+ await tester.pump(const Duration(seconds: 1));
+ expect(api.lists, hasLength(3));
+ api.lists.last.complete([]);
+ await tester.pump();
+ expect(app.lastError, isNull);
+ disposeApp();
+ },
+ );
+
+ testWidgets('machine recovery stops on an authentication refusal', (
+ tester,
+ ) async {
+ app.status = AppStatus.authenticated;
+ final first = app.retryMachines();
+ await tester.pump();
+ api.profiles.single.complete(_profile('current'));
+ api.lists.single.completeError(
+ ApiException('Backend unreachable', status: 502),
+ );
+ await tester.pump();
+ await first;
+ await tester.pump(const Duration(seconds: 2));
+ api.lists.last.completeError(ApiException('Sign in again', status: 401));
+ await tester.pump();
+ expect(app.lastError, contains('Sign in again'));
+ await tester.pump(const Duration(minutes: 2));
+ expect(api.lists, hasLength(2));
+ disposeApp();
+ });
+
+ for (final closeApp in [false, true]) {
+ testWidgets(
+ '${closeApp ? 'dispose' : 'sign-out'} cancels pending machine recovery',
+ (tester) async {
+ app.status = AppStatus.authenticated;
+ final first = app.retryMachines();
+ await tester.pump();
+ api.profiles.single.complete(_profile('current'));
+ api.lists.single.completeError(
+ ApiException('Backend unreachable', status: 502),
+ );
+ await tester.pump();
+ await first;
+ if (closeApp) {
+ disposeApp();
+ } else {
+ final logout = app.logout();
+ await tester.pump();
+ await logout;
+ }
+ await tester.pump(const Duration(minutes: 2));
+ expect(api.lists, hasLength(1));
+ disposeApp();
+ },
+ );
+ }
+
test('a late profile response cannot restore a signed-out account', () async {
final start = app.login();
await Future<void>.delayed(Duration.zero);