Merge pull request #2552 from murgatroid99/grpc-js_deferred_write_callback

grpc-js: Defer actions in http2 stream write callback
This commit is contained in:
Michael Lumish 2023-08-21 17:22:35 -07:00 committed by GitHub
commit cd25bada71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 10 deletions

View File

@ -501,16 +501,22 @@ export class Http2SubchannelCall implements SubchannelCall {
sendMessageWithContext(context: MessageContext, message: Buffer) { sendMessageWithContext(context: MessageContext, message: Buffer) {
this.trace('write() called with message of length ' + message.length); this.trace('write() called with message of length ' + message.length);
const cb: WriteCallback = (error?: Error | null) => { const cb: WriteCallback = (error?: Error | null) => {
let code: Status = Status.UNAVAILABLE; /* nextTick here ensures that no stream action can be taken in the call
if ( * stack of the write callback, in order to hopefully work around
(error as NodeJS.ErrnoException)?.code === 'ERR_STREAM_WRITE_AFTER_END' * https://github.com/nodejs/node/issues/49147 */
) { process.nextTick(() => {
code = Status.INTERNAL; let code: Status = Status.UNAVAILABLE;
} if (
if (error) { (error as NodeJS.ErrnoException)?.code ===
this.cancelWithStatus(code, `Write error: ${error.message}`); 'ERR_STREAM_WRITE_AFTER_END'
} ) {
context.callback?.(); code = Status.INTERNAL;
}
if (error) {
this.cancelWithStatus(code, `Write error: ${error.message}`);
}
context.callback?.();
});
}; };
this.trace('sending data chunk of length ' + message.length); this.trace('sending data chunk of length ' + message.length);
this.callEventTracker.addMessageSent(); this.callEventTracker.addMessageSent();