@@ -1080,15 +1080,11 @@ export const makeProtocolSocket = (options?: {
10801080
10811081 // Encode each ping with the current connection's parser.
10821082 const pinger = yield * makePinger ( Effect . suspend ( ( ) => writer . write ( parser . encode ( constPing ) ! ) ) , options )
1083- const onPingTimeout = options ?. onPingTimeout ?
1084- Effect . ignoreCause ( options . onPingTimeout , {
1085- log : true ,
1086- message : "RpcClient onPingTimeout hook failed"
1087- } ) :
1088- Effect . void
1083+ const onPingTimeout = Effect . ignoreCause ( options ?. onPingTimeout ?? Effect . void , {
1084+ log : true ,
1085+ message : "RpcClient onPingTimeout hook failed"
1086+ } )
10891087 let currentError : RpcClientError | undefined
1090- let connected = false
1091- let pingTimeoutError : Socket . SocketError | undefined
10921088
10931089 const broadcast = ( response : FromServerEncoded ) =>
10941090 Effect . forEach ( clientIds , ( clientId ) => writeResponse ( clientId , response ) )
@@ -1141,7 +1137,7 @@ export const makeProtocolSocket = (options?: {
11411137 yield * Effect . suspend ( ( ) => {
11421138 parser = serialization . makeUnsafe ( )
11431139 pinger . reset ( )
1144- connected = false
1140+ let connected = false
11451141 return Effect . gen ( function * ( ) {
11461142 const { pull } = yield * socket . reader
11471143 currentError = undefined
@@ -1157,21 +1153,21 @@ export const makeProtocolSocket = (options?: {
11571153 }
11581154 } ) . pipe (
11591155 Effect . scoped ,
1160- Effect . raceFirst ( Effect . flatMap (
1161- pinger . timeout ,
1162- ( ) =>
1163- Effect . fail (
1164- pingTimeoutError = new Socket . SocketError ( {
1165- reason : new Socket . SocketReadError ( {
1166- cause : new Error ( "ping timeout" )
1167- } )
1156+ // The read loop never succeeds, so the race only succeeds on a ping
1157+ // timeout, after the socket has been cleaned up.
1158+ Effect . raceFirst ( pinger . timeout ) ,
1159+ Effect . andThen ( ( ) => connected ? onPingTimeout : Effect . void ) ,
1160+ Effect . andThen ( ( ) =>
1161+ Effect . fail (
1162+ new Socket . SocketError ( {
1163+ reason : new Socket . SocketReadError ( {
1164+ cause : new Error ( "ping timeout" )
11681165 } )
1169- )
1170- ) )
1166+ } )
1167+ )
1168+ )
11711169 )
11721170 } ) . pipe (
1173- // Run the hook after socket cleanup so socket errors cannot interrupt it.
1174- Effect . tapError ( ( error ) => connected && error === pingTimeoutError ? onPingTimeout : Effect . void ) ,
11751171 Option . isSome ( hooks ) ? Effect . ensuring ( hooks . value . onDisconnect ) : identity ,
11761172 Effect . tapCause ( ( cause ) => {
11771173 const error = Cause . findError ( cause )
0 commit comments