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