Skip to content

Commit

Permalink
Fix Peer requests not terminating when the channel closes (dart-arc…
Browse files Browse the repository at this point in the history
…hive/json_rpc_2#52)

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 in `client.dart` is never called.
  • Loading branch information
jiahaog authored Jun 8, 2020
1 parent 0c05f88 commit 0413c15
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkgs/json_rpc_2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 2.2.1-dev

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

## 2.2.0

* Added `strictProtocolChecks` named parameter to `Server` and `Peer`
Expand Down
2 changes: 1 addition & 1 deletion pkgs/json_rpc_2/lib/src/peer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Peer implements Client, Server {
// server since it knows how to send error responses.
_serverIncomingForwarder.add(message);
}
});
}).whenComplete(close);
}

@override
Expand Down
16 changes: 16 additions & 0 deletions pkgs/json_rpc_2/test/peer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';
import 'dart:convert';

import 'package:pedantic/pedantic.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -84,6 +85,21 @@ void main() {
expect(peer.sendRequest('w', {'x': 'y'}), completion(equals('z')));
});
});

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

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

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

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

0 comments on commit 0413c15

Please sign in to comment.