Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert that the HTTP/2 connection is open #687

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated cleanup. This was a remnant from earlier in the implementation, when the promise resolved to a state or to a http2 connection that is no longer necessary. The else block is dead code.

},
(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
);
}
}