Remove or skip callbacks on old connections (#522)

Co-authored-by: Kevin Moore <kevmoo@users.noreply.github.com>
Co-authored-by: Vyacheslav Egorov <vegorov@google.com>
Fixes https://github.com/grpc/grpc-dart/issues/521
This commit is contained in:
Jakob Borg 2022-12-12 10:43:16 +01:00 committed by GitHub
parent 60311a7492
commit 4dc6e2b252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 2 deletions

View File

@ -77,7 +77,13 @@ class Http2ClientConnection implements connection.ClientConnection {
Future<ClientTransportConnection> connectTransport() async {
final connection = await _transportConnector.connect();
_transportConnector.done.then((_) => _abandonConnection());
_transportConnector.done.then((conn) {
if (conn == _transportConnection) {
// Only abandon the connection if the callback relates to the
// current one.
_abandonConnection();
}
});
// Give the settings settings-frame a bit of time to arrive.
// TODO(sigurdm): This is a hack. The http2 package should expose a way of
@ -124,6 +130,9 @@ class Http2ClientConnection implements connection.ClientConnection {
final shouldRefresh =
_connectionLifeTimer.elapsed > options.connectionTimeout;
if (shouldRefresh) {
// Deregister onActiveStateChanged callback from connection that we're
// not going to use any more.
_transportConnection!.onActiveStateChanged = (_) {};
_transportConnection!.finish();
}
if (!isHealthy || shouldRefresh) {
@ -271,7 +280,7 @@ class Http2ClientConnection implements connection.ClientConnection {
_cancelTimer();
_transportConnection = null;
if (_state == ConnectionState.idle && _state == ConnectionState.shutdown) {
if (_state == ConnectionState.idle || _state == ConnectionState.shutdown) {
// All good.
return;
}