From ce2765f7fb1e5694a9990ee57f1142c1c87dcfa1 Mon Sep 17 00:00:00 2001 From: Michael Lumish Date: Fri, 6 Aug 2021 11:53:48 -0700 Subject: [PATCH] grpc-js: Handle errors thrown by writing to http2 stream --- packages/grpc-js/src/call-stream.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/grpc-js/src/call-stream.ts b/packages/grpc-js/src/call-stream.ts index 57693343..8d72bb3e 100644 --- a/packages/grpc-js/src/call-stream.ts +++ b/packages/grpc-js/src/call-stream.ts @@ -637,7 +637,15 @@ export class Http2CallStream implements Call { this.pendingWrite.length + ' (deferred)' ); - stream.write(this.pendingWrite, this.pendingWriteCallback); + try { + stream.write(this.pendingWrite, this.pendingWriteCallback); + } catch (error) { + this.endCall({ + code: Status.UNAVAILABLE, + details: `Write failed with error ${error.message}`, + metadata: new Metadata() + }); + } } this.maybeCloseWrites(); } @@ -762,7 +770,15 @@ export class Http2CallStream implements Call { this.pendingWriteCallback = cb; } else { this.trace('sending data chunk of length ' + message.message.length); - this.http2Stream.write(message.message, cb); + try { + this.http2Stream.write(message.message, cb); + } catch (error) { + this.endCall({ + code: Status.UNAVAILABLE, + details: `Write failed with error ${error.message}`, + metadata: new Metadata() + }); + } this.maybeCloseWrites(); } }, this.handleFilterError.bind(this));