Skip to content

Commit

Permalink
Merge pull request #2808 from murgatroid99/grpc-js_transport_error_re…
Browse files Browse the repository at this point in the history
…porting

grpc-js: Report session error events instead of waiting for close
  • Loading branch information
murgatroid99 authored Sep 5, 2024
2 parents f5ea6ce + d409311 commit c63bdae
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/grpc-js/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,8 @@ class Http2Transport implements Transport {
);

session.once('error', error => {
/* Do nothing here. Any error should also trigger a close event, which is
* where we want to handle that. */
this.trace('connection closed with error ' + (error as Error).message);
this.handleDisconnect();
});

if (logging.isTracerEnabled(TRACER_NAME)) {
Expand Down Expand Up @@ -383,6 +382,9 @@ class Http2Transport implements Transport {
* Handle connection drops, but not GOAWAYs.
*/
private handleDisconnect() {
if (this.disconnectHandled) {
return;
}
this.clearKeepaliveTimeout();
this.reportDisconnectToOwner(false);
/* Give calls an event loop cycle to finish naturally before reporting the
Expand Down Expand Up @@ -768,6 +770,7 @@ export class Http2SubchannelConnector implements SubchannelConnector {
);
this.session = session;
let errorMessage = 'Failed to connect';
let reportedError = false;
session.unref();
session.once('connect', () => {
session.removeAllListeners();
Expand All @@ -778,12 +781,19 @@ export class Http2SubchannelConnector implements SubchannelConnector {
this.session = null;
// Leave time for error event to happen before rejecting
setImmediate(() => {
reject(`${errorMessage} (${new Date().toISOString()})`);
if (!reportedError) {
reportedError = true;
reject(`${errorMessage} (${new Date().toISOString()})`);
}
});
});
session.once('error', error => {
errorMessage = (error as Error).message;
this.trace('connection failed with error ' + errorMessage);
if (!reportedError) {
reportedError = true;
reject(`${errorMessage} (${new Date().toISOString()})`);
}
});
});
}
Expand Down

0 comments on commit c63bdae

Please sign in to comment.