@@ -369,7 +369,23 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
369369
370370 // always emit the message, in case we are streaming
371371 this . emit ( 'message' , message ) ;
372- const operationDescription = this [ kQueue ] . get ( message . responseTo ) ;
372+ let operationDescription = this [ kQueue ] . get ( message . responseTo ) ;
373+
374+ if ( ! operationDescription && this . isMonitoringConnection ) {
375+ // NODE-4783: How do we recover from this when the initial hello's requestId is not
376+ // the responseTo when hello responses have been skipped?
377+ //
378+ // Get the first orphaned operation description.
379+ const entry = this [ kQueue ] . entries ( ) . next ( ) ;
380+ if ( entry ) {
381+ const [ requestId , orphaned ] : [ number , OperationDescription ] = entry . value ;
382+ // If the orphaned operation description exists then set it.
383+ operationDescription = orphaned ;
384+ // Remove the entry with the bad request id from the queue.
385+ this [ kQueue ] . delete ( requestId ) ;
386+ }
387+ }
388+
373389 if ( ! operationDescription ) {
374390 return ;
375391 }
@@ -381,7 +397,10 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
381397 // making the `responseTo` change on each response
382398 this [ kQueue ] . delete ( message . responseTo ) ;
383399 if ( 'moreToCome' in message && message . moreToCome ) {
384- // requeue the callback for next synthetic request
400+ // NODE-4783: If the operation description check above does find an orphaned
401+ // description and sets the operationDescription then this line will put one
402+ // back in the queue with the correct requestId and will resolve not being able
403+ // to find the next one via the responseTo of the next streaming hello.
385404 this [ kQueue ] . set ( message . requestId , operationDescription ) ;
386405 } else if ( operationDescription . socketTimeoutOverride ) {
387406 this [ kStream ] . setTimeout ( this . socketTimeoutMS ) ;
0 commit comments