From ae334976568ffc9c90e14e775662ba86f0c0e030 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Fri, 6 Sep 2019 13:21:47 -0700 Subject: [PATCH] Check stream.session.socket before adding event handler --- packages/grpc-js/src/call-stream.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/grpc-js/src/call-stream.ts b/packages/grpc-js/src/call-stream.ts index b1d5fd96..67880212 100644 --- a/packages/grpc-js/src/call-stream.ts +++ b/packages/grpc-js/src/call-stream.ts @@ -342,14 +342,20 @@ export class Http2CallStream extends Duplex implements Call { * from bubbling up. However, errors here should all correspond to * "close" events, where we will handle the error more granularly */ }); - /* If the underlying TLS or TCP connection closes, we want to end the - * call with an UNAVAILABLE status to match the behavior of the other - * library. In this handler we don't wait for trailers before ending the - * call. This should ensure that this endCall happens sooner than the one - * in the stream.on('close', ...) handler. */ - stream.session.socket.on('close', () => { - this.endCall({code: Status.UNAVAILABLE, details: 'Connection dropped', metadata: new Metadata()}); - }); + /* For some reason, stream.session.socket can sometimes be undefined. + * When that happens trying to add this event handler throws an error. + * This check is a stopgap measure to get this working sometimes until + * we understand/eliminate those cases. */ + if (stream.session.socket) { + /* If the underlying TLS or TCP connection closes, we want to end the + * call with an UNAVAILABLE status to match the behavior of the other + * library. In this handler we don't wait for trailers before ending the + * call. This should ensure that this endCall happens sooner than the one + * in the stream.on('close', ...) handler. */ + stream.session.socket.on('close', () => { + this.endCall({code: Status.UNAVAILABLE, details: 'Connection dropped', metadata: new Metadata()}); + }); + } if (!this.pendingRead) { stream.pause(); }