From ef8b7fa37d56f4bd4f8d4e2133a5834776dbeed1 Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Wed, 12 Jul 2023 17:55:46 +0200 Subject: [PATCH] Assert that the HTTP/2 connection is open (#687) --- .../connect-node/src/http2-session-manager.ts | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/packages/connect-node/src/http2-session-manager.ts b/packages/connect-node/src/http2-session-manager.ts index 5f6cb97f7..8942a2985 100644 --- a/packages/connect-node/src/http2-session-manager.ts +++ b/packages/connect-node/src/http2-session-manager.ts @@ -305,11 +305,7 @@ export class Http2SessionManager { case "verifying": state.verified.then( (value) => { - if ("t" in value) { - this.setState(value); - } else { - this.setState(ready(value, this.options)); - } + this.setState(value); }, (reason) => { this.setState(closedOrError(reason)); @@ -543,6 +539,12 @@ function ready( conn: http2.ClientHttp2Session, options: Required ): StateReady { + // Users have reported an error "The session has been destroyed" raised + // from H2SessionManager.request(), see https://github.com/bufbuild/connect-es/issues/683 + // This assertion will show whether the session already died in the + // "connecting" state. + assertSessionOpen(conn); + // the last time we were sure that the connection is alive, via a PING // response, or via received response bytes let lastAliveAt = Date.now(); @@ -785,3 +787,24 @@ function safeSetTimeout( } return setTimeout(callback, ms); } + +function assertSessionOpen(conn: http2.ClientHttp2Session) { + if (conn.connecting) { + throw new ConnectError( + "expected open session, but it is connecting", + Code.Internal + ); + } + if (conn.destroyed) { + throw new ConnectError( + "expected open session, but it is destroyed", + Code.Internal + ); + } + if (conn.closed) { + throw new ConnectError( + "expected open session, but it is closed", + Code.Internal + ); + } +}