Skip to content

Commit

Permalink
Fix Peer requests not terminating when the channel closes
Browse files Browse the repository at this point in the history
The `listen()` method of `Peer` never propagates close events from its manager to the `client` field. This causes in-flight requests to never terminate as the clean up handler at https://github.com/dart-lang/json_rpc_2/blob/d589e635d8ccb7cda6a804bd571f88abbabab146/lib/src/client.dart#L65-L72 is never called.
  • Loading branch information
jiahaog committed May 27, 2020
1 parent d589e63 commit 2872f62
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.1

* Fix `Peer` requests not terminating when the underlying channel is closed.

## 2.2.0

* Added `strictProtocolChecks` named parameter to `Server` and `Peer`
Expand Down
6 changes: 4 additions & 2 deletions lib/src/peer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ class Peer implements Client, Server {
// Shared methods.

@override
Future listen() {
Future listen() async {
_client.listen();
_server.listen();
return _manager.listen((message) {
await _manager.listen((message) {
if (message is Map) {
if (message.containsKey('result') || message.containsKey('error')) {
_clientIncomingForwarder.add(message);
Expand All @@ -143,6 +143,8 @@ class Peer implements Client, Server {
_serverIncomingForwarder.add(message);
}
});

return close();
}

@override
Expand Down
14 changes: 14 additions & 0 deletions test/peer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ void main() {
expect(peer.sendRequest('w', {'x': 'y'}), completion(equals('z')));
});
});

test('requests terminates when the channel is closed', () {
var incomingController = StreamController();
var channel = StreamChannel.withGuarantees(
incomingController.stream, StreamController(),
);
var peer = json_rpc.Peer.withoutJson(channel);
peer.listen();

var response = peer.sendRequest('foo');
incomingController.close();

expect(response, throwsStateError);
});
});

group('like a server,', () {
Expand Down

0 comments on commit 2872f62

Please sign in to comment.