App: replace a silent socket instead of hanging; blink the terminal cursor on a timer; keep one markdown cache entry per streamed reply
A socket that outlived a sleep or network change looked open, so every queued request waited its own 30 s timeout behind the last until the page was reopened. The client now replaces a socket whose request times out unheard (or after two timeouts in a row), and probes it with a ping after a quiet spell or on returning to the foreground. The terminal's blinking cursor ran a Ticker (a frame every vsync); a 530 ms timer now flips it. Streamed deltas no longer flood the markdown cache and evict finished answers. Adds tool/perf/idle_session_bench.mjs (idle frames, long-session heap, snapshot census) and notes/perf-hangs-idle.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,10 @@ void reply(FakeChannel channel, {int index = 0, String type = 'pong'}) =>
|
||||
jsonEncode({'id': sent(channel, index)['id'], 'type': type}),
|
||||
);
|
||||
|
||||
extension<T> on T {
|
||||
void let(void Function(T) f) => f(this);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'requests serialize through replies, errors do not poison queue',
|
||||
@@ -51,14 +55,126 @@ void main() {
|
||||
requestTimeout: const Duration(milliseconds: 10),
|
||||
);
|
||||
await client.connect(Uri.parse('ws://example.test'));
|
||||
await expectLater(client.call('ping'), throwsA(isA<TimeoutException>()));
|
||||
final late = client.frames.first;
|
||||
final call = client.call('ping');
|
||||
await flush();
|
||||
// The socket is alive (a pushed frame arrives while the call waits),
|
||||
// so its first timeout keeps it.
|
||||
channel.incoming.add(jsonEncode({'type': 'run_event'}));
|
||||
await expectLater(call, throwsA(isA<TimeoutException>()));
|
||||
expect(client.state.kind, ConnectionKind.connected);
|
||||
final late = client.frames.firstWhere((f) => f['type'] == 'pong');
|
||||
reply(channel);
|
||||
expect((await late)['type'], 'pong');
|
||||
await client.dispose();
|
||||
},
|
||||
);
|
||||
|
||||
test('a timeout on a silent socket replaces it and fails the queue at once '
|
||||
'instead of timing out each queued request in turn', () async {
|
||||
final channels = <FakeChannel>[];
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (_) =>
|
||||
(FakeChannel()..readyCompleter.complete())..let(channels.add),
|
||||
requestTimeout: const Duration(milliseconds: 20),
|
||||
backoffBase: const Duration(milliseconds: 5),
|
||||
);
|
||||
await client.connect(Uri.parse('ws://example.test'));
|
||||
final first = client.call('ping');
|
||||
final queued = client.call('list_runs');
|
||||
final reconnected = client.states
|
||||
.where((s) => s.kind == ConnectionKind.connected)
|
||||
.first;
|
||||
final clock = Stopwatch()..start();
|
||||
await expectLater(first, throwsA(isA<TimeoutException>()));
|
||||
await expectLater(queued, throwsStateError);
|
||||
expect(clock.elapsedMilliseconds, lessThan(200));
|
||||
await reconnected.timeout(const Duration(seconds: 2));
|
||||
expect(channels, hasLength(2));
|
||||
expect(channels.first.outgoing, hasLength(1));
|
||||
await client.dispose();
|
||||
});
|
||||
|
||||
test('a second timeout in a row replaces a socket that still pushes '
|
||||
'frames: its request lane is stuck', () async {
|
||||
final channels = <FakeChannel>[];
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (_) =>
|
||||
(FakeChannel()..readyCompleter.complete())..let(channels.add),
|
||||
requestTimeout: const Duration(milliseconds: 20),
|
||||
backoffBase: const Duration(milliseconds: 5),
|
||||
);
|
||||
await client.connect(Uri.parse('ws://example.test'));
|
||||
for (var i = 0; i < 2; i++) {
|
||||
final call = client.call('ping');
|
||||
await flush();
|
||||
channels.first.incoming.add(jsonEncode({'type': 'run_event'}));
|
||||
await expectLater(call, throwsA(isA<TimeoutException>()));
|
||||
if (i == 0) expect(client.state.kind, ConnectionKind.connected);
|
||||
}
|
||||
await client.states
|
||||
.where((s) => s.kind == ConnectionKind.connected)
|
||||
.first
|
||||
.timeout(const Duration(seconds: 2));
|
||||
expect(channels, hasLength(2));
|
||||
await client.dispose();
|
||||
});
|
||||
|
||||
test(
|
||||
'the first request after a quiet spell is preceded by a probe',
|
||||
() async {
|
||||
final channel = FakeChannel()..readyCompleter.complete();
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (_) => channel,
|
||||
quietProbeAfter: const Duration(milliseconds: 20),
|
||||
probeTimeout: const Duration(seconds: 5),
|
||||
);
|
||||
await client.connect(Uri.parse('ws://example.test'));
|
||||
final first = client.call('list_runs');
|
||||
await flush();
|
||||
expect(channel.outgoing, hasLength(1), reason: 'not quiet yet');
|
||||
reply(channel, type: 'runs');
|
||||
await first;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 40));
|
||||
final second = client.call('list_runs');
|
||||
await flush();
|
||||
expect(sent(channel, 1)['type'], 'ping');
|
||||
expect(sent(channel, 2)['type'], 'list_runs');
|
||||
reply(channel, index: 2, type: 'runs');
|
||||
await second;
|
||||
await client.dispose();
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a probe keeps a socket that answers and replaces one that does not',
|
||||
() async {
|
||||
final channels = <FakeChannel>[];
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (_) =>
|
||||
(FakeChannel()..readyCompleter.complete())..let(channels.add),
|
||||
probeTimeout: const Duration(milliseconds: 20),
|
||||
backoffBase: const Duration(milliseconds: 5),
|
||||
);
|
||||
final frames = <Map<String, dynamic>>[];
|
||||
client.frames.listen(frames.add);
|
||||
await client.connect(Uri.parse('ws://example.test'));
|
||||
client.probe();
|
||||
final probe = sent(channels.first);
|
||||
expect(probe['type'], 'ping');
|
||||
reply(channels.first);
|
||||
await Future<void>.delayed(const Duration(milliseconds: 40));
|
||||
expect(channels, hasLength(1));
|
||||
expect(frames, isEmpty, reason: 'the probe reply is not a frame');
|
||||
client.probe();
|
||||
await client.states
|
||||
.where((s) => s.kind == ConnectionKind.connected)
|
||||
.first
|
||||
.timeout(const Duration(seconds: 2));
|
||||
expect(channels, hasLength(2));
|
||||
await client.dispose();
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'replacement fails queued calls rather than sending on new socket',
|
||||
() async {
|
||||
|
||||
Reference in New Issue
Block a user