Skip to content

Commit

Permalink
Assert that the HTTP/2 connection is open (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm committed Jul 12, 2023
1 parent f94fb12 commit ef8b7fa
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions packages/connect-node/src/http2-session-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -543,6 +539,12 @@ function ready(
conn: http2.ClientHttp2Session,
options: Required<Http2SessionOptions>
): 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();
Expand Down Expand Up @@ -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
);
}
}

0 comments on commit ef8b7fa

Please sign in to comment.