Repository navigation
[coverage] Fix connection disposed error #2903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
liamappelbe
wants to merge
5
commits into
main
Choose a base branch
from
coverage_connection_disposed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+299
−34
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| // Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:coverage/coverage.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| typedef _RpcHandler = | ||
| FutureOr<void> Function( | ||
| _ClientConnection connection, | ||
| Map<String, dynamic> rpc, | ||
| ); | ||
|
|
||
| class _ClientConnection { | ||
| _ClientConnection(this.index, this.socket, _RpcHandler onRpc) { | ||
| _subscription = socket.listen((message) { | ||
| final rpc = jsonDecode(message as String) as Map<String, dynamic>; | ||
| onRpc(this, rpc); | ||
| }); | ||
| } | ||
|
|
||
| final int index; | ||
| final WebSocket socket; | ||
| late final StreamSubscription<dynamic> _subscription; | ||
|
|
||
| void pause() => _subscription.pause(); | ||
|
|
||
| void resume() => _subscription.resume(); | ||
|
|
||
| Future<void> close() async { | ||
| await _subscription.cancel(); | ||
| await socket.close(); | ||
| } | ||
|
|
||
| void reply(Object? id, Map<String, dynamic> result) { | ||
| if (socket.readyState == WebSocket.open) { | ||
| socket.add(jsonEncode({'jsonrpc': '2.0', 'id': id, 'result': result})); | ||
| } | ||
| } | ||
|
|
||
| void replyError(Object? id, String message) { | ||
| if (socket.readyState == WebSocket.open) { | ||
| socket.add( | ||
| jsonEncode({ | ||
| 'jsonrpc': '2.0', | ||
| 'id': id, | ||
| 'error': {'code': -32000, 'message': message}, | ||
| }), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| void failUnexpectedRpc(Map<String, dynamic> rpc) { | ||
| final message = 'Unexpected RPC on connection $index: ${rpc['method']}'; | ||
| replyError(rpc['id'], message); | ||
| fail(message); | ||
| } | ||
| } | ||
|
|
||
| class _FakeVmServiceServer { | ||
| _FakeVmServiceServer._(this._server); | ||
|
|
||
| final HttpServer _server; | ||
| final List<_ClientConnection> connections = []; | ||
|
|
||
| Uri get uri => Uri.parse('http://127.0.0.1:${_server.port}/'); | ||
|
|
||
| static Future<_FakeVmServiceServer> start(_RpcHandler onRpc) async { | ||
| final httpServer = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); | ||
| final fakeServer = _FakeVmServiceServer._(httpServer); | ||
| addTearDown(fakeServer.close); | ||
|
|
||
| httpServer.listen((request) async { | ||
| expect(request.uri.path, '/ws'); | ||
| final socket = await WebSocketTransformer.upgrade(request); | ||
| fakeServer.connections.add( | ||
| _ClientConnection(fakeServer.connections.length + 1, socket, onRpc), | ||
| ); | ||
| }); | ||
|
|
||
| return fakeServer; | ||
| } | ||
|
|
||
| Future<void> close() async { | ||
| await _server.close(force: true); | ||
| for (final connection in connections) { | ||
| await connection.close(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Map<String, dynamic> _vmResponse(List<String> isolateIds) => { | ||
| 'type': 'VM', | ||
| 'isolates': [ | ||
| for (var i = 0; i < isolateIds.length; i++) | ||
| { | ||
| 'type': '@Isolate', | ||
| 'id': isolateIds[i], | ||
| 'isolateGroupId': 'isolateGroups/${i + 1}', | ||
| 'name': 'suite_${i + 1}', | ||
| 'number': '${i + 1}', | ||
| 'isSystemIsolate': false, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const _versionResponse = <String, dynamic>{ | ||
| 'type': 'Version', | ||
| 'major': 4, | ||
| 'minor': 0, | ||
| }; | ||
|
|
||
| const _emptySourceReport = <String, dynamic>{ | ||
| 'type': 'SourceReport', | ||
| 'ranges': <Object?>[], | ||
| 'scripts': <Object?>[], | ||
| }; | ||
|
|
||
| void main() { | ||
| test('disposes failed connection on non-timeout getVersion error', () async { | ||
| var reports = 0; | ||
| final server = await _FakeVmServiceServer.start((connection, rpc) { | ||
| final method = rpc['method']; | ||
| if (connection.index == 1) { | ||
| if (method != 'getVersion') { | ||
| connection.failUnexpectedRpc(rpc); | ||
| } | ||
| // Return an RPC error while keeping connection 1 open on the server. | ||
| // collect() must dispose and close this failed connection itself. | ||
| connection.replyError(rpc['id'], 'Transient readiness failure'); | ||
| return; | ||
| } | ||
|
|
||
| if (method == 'getVersion') { | ||
| connection.reply(rpc['id'], _versionResponse); | ||
| } else if (method == 'getVM') { | ||
| connection.reply(rpc['id'], _vmResponse(['isolates/1'])); | ||
| } else if (method == 'getSourceReport') { | ||
| reports++; | ||
| connection.reply(rpc['id'], _emptySourceReport); | ||
| } else { | ||
| connection.failUnexpectedRpc(rpc); | ||
| } | ||
| }); | ||
|
|
||
| final coverage = await collect( | ||
| server.uri, | ||
| false, | ||
| false, | ||
| false, | ||
| {}, | ||
| timeout: const Duration(seconds: 5), | ||
| ); | ||
|
|
||
| expect(coverage, {'type': 'CodeCoverage', 'coverage': <Object?>[]}); | ||
| expect(server.connections.length, 2); | ||
| expect(reports, 1); | ||
| await server.connections.first.socket.done.timeout( | ||
| const Duration(seconds: 1), | ||
| onTimeout: () => | ||
| fail('Expected collect() to close the failed connection'), | ||
| ); | ||
| }); | ||
|
|
||
| test( | ||
| 'delayed close of a failed attempt does not dispose active retry', | ||
| () async { | ||
| var reports = 0; | ||
| late final _FakeVmServiceServer server; | ||
| server = await _FakeVmServiceServer.start((connection, rpc) async { | ||
| final method = rpc['method']; | ||
| if (connection.index == 1) { | ||
| if (method != 'getVersion') { | ||
| connection.failUnexpectedRpc(rpc); | ||
| } | ||
| // Stall getVersion past _retryInterval and pause reading so the | ||
| // server withholds its close handshake response until connection 2 | ||
| // has an in-flight getSourceReport RPC. | ||
| connection.pause(); | ||
| return; | ||
| } | ||
|
|
||
| if (method == 'getVersion') { | ||
| connection.reply(rpc['id'], _versionResponse); | ||
| } else if (method == 'getVM') { | ||
| connection.reply( | ||
| rpc['id'], | ||
| _vmResponse(['isolates/1', 'isolates/2']), | ||
| ); | ||
| } else if (method == 'getSourceReport') { | ||
| reports++; | ||
| if (reports == 1) { | ||
| // Complete connection 1's close handshake while connection 2's | ||
| // getSourceReport is in flight, delivering connection 1's onDone | ||
| // to the client before the second isolate's RPC completes. | ||
| final first = server.connections.first; | ||
| first.resume(); | ||
| await first.socket.close(); | ||
| await first.socket.done; | ||
| } | ||
| connection.reply(rpc['id'], _emptySourceReport); | ||
| } else { | ||
| connection.failUnexpectedRpc(rpc); | ||
| } | ||
| }); | ||
|
|
||
| final coverage = await collect( | ||
| server.uri, | ||
| false, | ||
| false, | ||
| false, | ||
| {}, | ||
| timeout: const Duration(seconds: 5), | ||
| ); | ||
|
|
||
| expect(coverage, {'type': 'CodeCoverage', 'coverage': <Object?>[]}); | ||
| expect(server.connections.length, 2); | ||
| expect(reports, 2); | ||
| }, | ||
| ); | ||
|
|
||
| test('retries when peer closes socket during getVersion readiness', () async { | ||
| var reports = 0; | ||
| final server = await _FakeVmServiceServer.start((connection, rpc) async { | ||
| final method = rpc['method']; | ||
| if (connection.index == 1) { | ||
| if (method != 'getVersion') { | ||
| connection.failUnexpectedRpc(rpc); | ||
| } | ||
| await connection.socket.close(); | ||
| return; | ||
| } | ||
|
|
||
| if (method == 'getVersion') { | ||
| connection.reply(rpc['id'], _versionResponse); | ||
| } else if (method == 'getVM') { | ||
| connection.reply(rpc['id'], _vmResponse(['isolates/1'])); | ||
| } else if (method == 'getSourceReport') { | ||
| reports++; | ||
| connection.reply(rpc['id'], _emptySourceReport); | ||
| } else { | ||
| connection.failUnexpectedRpc(rpc); | ||
| } | ||
| }); | ||
|
|
||
| final coverage = await collect( | ||
| server.uri, | ||
| false, | ||
| false, | ||
| false, | ||
| {}, | ||
| timeout: const Duration(seconds: 5), | ||
| ); | ||
|
|
||
| expect(coverage, {'type': 'CodeCoverage', 'coverage': <Object?>[]}); | ||
| expect(server.connections.length, 2); | ||
| expect(reports, 1); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.