Fix regression on fetching the remote address of a closed socket. (#664)

* Fix regression on fetching the remote address of a closed socket

* Changes as per review

* Add changelog entry

* Rev version for publish
This commit is contained in:
Moritz 2023-09-08 17:41:32 +02:00 committed by GitHub
parent dae290cc5a
commit c1fa94951a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 23 deletions

View File

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

View File

@ -241,28 +241,33 @@ class Server extends ConnectionServer {
bool requireClientCertificate = false, bool requireClientCertificate = false,
}) async { }) async {
// TODO(dart-lang/grpc-dart#9): Handle HTTP/1.1 upgrade to h2c, if allowed. // 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; final securityContext = security?.securityContext;
if (securityContext != null) { if (securityContext != null) {
_secureServer = await SecureServerSocket.bind( final _server = await SecureServerSocket.bind(
address ?? InternetAddress.anyIPv4, port ?? 443, securityContext, address ?? InternetAddress.anyIPv4,
backlog: backlog, port ?? 443,
shared: shared, securityContext,
v6Only: v6Only, backlog: backlog,
requestClientCertificate: requestClientCertificate, shared: shared,
requireClientCertificate: requireClientCertificate); v6Only: v6Only,
server = _secureServer; requestClientCertificate: requestClientCertificate,
requireClientCertificate: requireClientCertificate,
);
_secureServer = _server;
server = _server;
} else { } else {
_insecureServer = await ServerSocket.bind( final _server = await ServerSocket.bind(
address ?? InternetAddress.anyIPv4, address ?? InternetAddress.anyIPv4,
port ?? 80, port ?? 80,
backlog: backlog, backlog: backlog,
shared: shared, shared: shared,
v6Only: v6Only, 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. // Don't wait for io buffers to fill up before sending requests.
if (socket.address.type != InternetAddressType.unix) { if (socket.address.type != InternetAddressType.unix) {
socket.setOption(SocketOption.tcpNoDelay, true); socket.setOption(SocketOption.tcpNoDelay, true);
@ -282,7 +287,7 @@ class Server extends ConnectionServer {
serveConnection( serveConnection(
connection: connection, connection: connection,
clientCertificate: clientCertificate, clientCertificate: clientCertificate,
remoteAddress: socket.remoteAddress, remoteAddress: socket.remoteAddressOrNull,
); );
}, onError: (error, stackTrace) { }, onError: (error, stackTrace) {
if (error is Error) { if (error is Error) {
@ -320,15 +325,24 @@ class Server extends ConnectionServer {
} }
Future<void> shutdown() async { Future<void> shutdown() async {
final done = _connections.map((connection) => connection.finish()).toList(); await Future.wait([
if (_insecureServer != null) { for (var connection in _connections) connection.finish(),
done.add(_insecureServer!.close()); if (_insecureServer != null) _insecureServer!.close(),
} if (_secureServer != null) _secureServer!.close(),
if (_secureServer != null) { ]);
done.add(_secureServer!.close());
}
await Future.wait(done);
_insecureServer = null; _insecureServer = null;
_secureServer = 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;
}
}
}

View File

@ -1,6 +1,6 @@
name: grpc name: grpc
description: Dart implementation of gRPC, a high performance, open-source universal RPC framework. 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 repository: https://github.com/grpc/grpc-dart