Skip to content

Commit

Permalink
Fix regression on fetching the remote address of a closed socket. (#664)
Browse files Browse the repository at this point in the history
* Fix regression on fetching the remote address of a closed socket

* Changes as per review

* Add changelog entry

* Rev version for publish
  • Loading branch information
mosuem committed Sep 8, 2023
1 parent dae290c commit c1fa949
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## 3.2.4-wip
## 3.2.4

* Forward internal `GrpcError` on when throwing while sending a request.
* Add support for proxies, see [#33](https://github.com/grpc/grpc-dart/issues/33).
* Remove canceled `ServerHandler`s from tracking list.
* Fix regression on fetching the remote address of a closed socket.

## 3.2.3

Expand Down
56 changes: 35 additions & 21 deletions lib/src/server/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,28 +241,33 @@ class Server extends ConnectionServer {
bool requireClientCertificate = false,
}) async {
// TODO(dart-lang/grpc-dart#9): Handle HTTP/1.1 upgrade to h2c, if allowed.
Stream<Socket>? server;
Stream<Socket> server;
final securityContext = security?.securityContext;
if (securityContext != null) {
_secureServer = await SecureServerSocket.bind(
address ?? InternetAddress.anyIPv4, port ?? 443, securityContext,
backlog: backlog,
shared: shared,
v6Only: v6Only,
requestClientCertificate: requestClientCertificate,
requireClientCertificate: requireClientCertificate);
server = _secureServer;
final _server = await SecureServerSocket.bind(
address ?? InternetAddress.anyIPv4,
port ?? 443,
securityContext,
backlog: backlog,
shared: shared,
v6Only: v6Only,
requestClientCertificate: requestClientCertificate,
requireClientCertificate: requireClientCertificate,
);
_secureServer = _server;
server = _server;
} else {
_insecureServer = await ServerSocket.bind(
final _server = await ServerSocket.bind(
address ?? InternetAddress.anyIPv4,
port ?? 80,
backlog: backlog,
shared: shared,
v6Only: v6Only,
);
server = _insecureServer;
_insecureServer = _server;
server = _server;
}
server!.listen((socket) {
server.listen((socket) {
// Don't wait for io buffers to fill up before sending requests.
if (socket.address.type != InternetAddressType.unix) {
socket.setOption(SocketOption.tcpNoDelay, true);
Expand All @@ -282,7 +287,7 @@ class Server extends ConnectionServer {
serveConnection(
connection: connection,
clientCertificate: clientCertificate,
remoteAddress: socket.remoteAddress,
remoteAddress: socket.remoteAddressOrNull,
);
}, onError: (error, stackTrace) {
if (error is Error) {
Expand Down Expand Up @@ -320,15 +325,24 @@ class Server extends ConnectionServer {
}

Future<void> shutdown() async {
final done = _connections.map((connection) => connection.finish()).toList();
if (_insecureServer != null) {
done.add(_insecureServer!.close());
}
if (_secureServer != null) {
done.add(_secureServer!.close());
}
await Future.wait(done);
await Future.wait([
for (var connection in _connections) connection.finish(),
if (_insecureServer != null) _insecureServer!.close(),
if (_secureServer != null) _secureServer!.close(),
]);
_insecureServer = null;
_secureServer = null;
}
}

extension on Socket {
InternetAddress? get remoteAddressOrNull {
try {
// Using a try-catch control flow as dart:io Sockets don't expose their
// connectivity state.
return remoteAddress;
} on Exception catch (_) {
return null;
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: grpc
description: Dart implementation of gRPC, a high performance, open-source universal RPC framework.
version: 3.2.4-wip
version: 3.2.4

repository: https://github.com/grpc/grpc-dart

Expand Down

0 comments on commit c1fa949

Please sign in to comment.