@@ -195,6 +195,7 @@ class Redis extends Commander implements DataHandledable {
195195 ? [ options . username , options . password ]
196196 : options . password ,
197197 subscriber : false ,
198+ hasIssuedSubscribe : false ,
198199 } ;
199200
200201 const _this = this ;
@@ -429,6 +430,17 @@ class Redis extends Commander implements DataHandledable {
429430 command . reject ( new Error ( CONNECTION_CLOSED_ERROR_MSG ) ) ;
430431 return command . promise ;
431432 }
433+
434+ // Make sure know that a subscribe command is sent to the server
435+ // In order to prevent race condition by sending another non-subscribe command
436+ // before we've received the response of the previous subscribe command
437+ if (
438+ ( this . status === "connect" || this . status === "ready" ) &&
439+ Command . checkFlag ( "ENTER_SUBSCRIBER_MODE" , command . name )
440+ ) {
441+ this . condition . hasIssuedSubscribe = true ;
442+ }
443+
432444 if (
433445 this . condition ?. subscriber &&
434446 ! Command . checkFlag ( "VALID_IN_SUBSCRIBER_MODE" , command . name )
@@ -527,7 +539,10 @@ class Redis extends Commander implements DataHandledable {
527539 this . manuallyClosing = true ;
528540 }
529541
530- if ( this . options . socketTimeout !== undefined && this . socketTimeoutTimer === undefined ) {
542+ if (
543+ this . options . socketTimeout !== undefined &&
544+ this . socketTimeoutTimer === undefined
545+ ) {
531546 this . setSocketTimeout ( ) ;
532547 }
533548 }
@@ -546,7 +561,11 @@ class Redis extends Commander implements DataHandledable {
546561
547562 private setSocketTimeout ( ) {
548563 this . socketTimeoutTimer = setTimeout ( ( ) => {
549- this . stream . destroy ( new Error ( `Socket timeout. Expecting data, but didn't receive any in ${ this . options . socketTimeout } ms.` ) ) ;
564+ this . stream . destroy (
565+ new Error (
566+ `Socket timeout. Expecting data, but didn't receive any in ${ this . options . socketTimeout } ms.`
567+ )
568+ ) ;
550569 this . socketTimeoutTimer = undefined ;
551570 } , this . options . socketTimeout ) ;
552571
0 commit comments