@@ -26,11 +26,7 @@ import * as Socket from "effect/socket/Socket";
2626
2727import { makeWsRpcProtocolClient , PING_TIMEOUT , type WsRpcProtocolClient } from "./protocol.ts" ;
2828import { NETWORK_BLOCKING_HINT } from "../errors/network.ts" ;
29- import type {
30- ConnectionAttemptError ,
31- ConnectionTransientError ,
32- PreparedConnection ,
33- } from "../connection/model.ts" ;
29+ import type { ConnectionAttemptError , PreparedConnection } from "../connection/model.ts" ;
3430import {
3531 ConnectionBlockedError ,
3632 ConnectionTransientError as ConnectionTransientErrorClass ,
@@ -124,10 +120,12 @@ function serverConfigReplayEvents(
124120const isSocketErrorReason = Schema . is ( Socket . SocketErrorReason ) ;
125121
126122function mapSessionRpcError (
127- error : InitialConfigError | ProbeError | ServerConfigSubscriptionError ,
128- networkHint : string ,
123+ error : InitialConfigError | ProbeError | ServerConfigSubscriptionError | ConnectionBlockedError ,
124+ transportDetail : string ,
129125) : ConnectionAttemptError {
130126 switch ( error . _tag ) {
127+ case "ConnectionBlockedError" :
128+ return error ;
131129 case "EnvironmentAuthorizationError" :
132130 return new ConnectionBlockedError ( {
133131 reason : "permission" ,
@@ -142,7 +140,7 @@ function mapSessionRpcError(
142140 case "RpcClientError" :
143141 return new ConnectionTransientErrorClass ( {
144142 reason : "transport" ,
145- detail : ` ${ error . message } ${ isSocketErrorReason ( error . reason ) ? networkHint : "" } ` ,
143+ detail : isSocketErrorReason ( error . reason ) ? transportDetail : error . message ,
146144 } ) ;
147145 }
148146}
@@ -161,35 +159,28 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
161159 const connect = Effect . fnUntraced ( function * ( connection : PreparedConnection ) {
162160 const networkHint =
163161 connection . target . _tag === "RelayConnectionTarget" ? ` ${ NETWORK_BLOCKING_HINT } ` : "" ;
162+ let socketOpened = false ;
163+ let pingTimedOut = false ;
164164 const mapRpcError = ( error : Parameters < typeof mapSessionRpcError > [ 0 ] ) =>
165- mapSessionRpcError ( error , networkHint ) ;
165+ mapSessionRpcError (
166+ error ,
167+ `${
168+ ! socketOpened
169+ ? `${ connection . label } could not establish a WebSocket connection.`
170+ : pingTimedOut
171+ ? `${ connection . label } stopped responding.`
172+ : `${ connection . label } disconnected.`
173+ } ${ networkHint } `,
174+ ) ;
166175 yield * Effect . annotateCurrentSpan ( {
167176 "connection.environment.id" : connection . environmentId ,
168177 } ) ;
169178
170- const connected = yield * Deferred . make < void > ( ) ;
171- const disconnected = yield * Deferred . make < never , ConnectionTransientError > ( ) ;
172- const pingTimedOut = yield * Ref . make ( false ) ;
173179 const hooks = RpcClient . ConnectionHooks . of ( {
174- onConnect : Deferred . succeed ( connected , undefined ) . pipe ( Effect . asVoid ) ,
175- onDisconnect : Effect . all ( [ Deferred . isDone ( connected ) , Ref . get ( pingTimedOut ) ] ) . pipe (
176- Effect . flatMap ( ( [ wasConnected , timedOut ] ) =>
177- Deferred . fail (
178- disconnected ,
179- new ConnectionTransientErrorClass ( {
180- reason : "transport" ,
181- detail : `${
182- ! wasConnected
183- ? `${ connection . label } could not establish a WebSocket connection.`
184- : timedOut
185- ? `${ connection . label } stopped responding.`
186- : `${ connection . label } disconnected.`
187- } ${ networkHint } `,
188- } ) ,
189- ) ,
190- ) ,
191- Effect . asVoid ,
192- ) ,
180+ onConnect : Effect . sync ( ( ) => {
181+ socketOpened = true ;
182+ } ) ,
183+ onDisconnect : Effect . void ,
193184 } ) ;
194185 const layerSocket = Socket . layerWebSocket ( connection . socketUrl , {
195186 openTimeout : SOCKET_OPEN_TIMEOUT ,
@@ -200,7 +191,10 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
200191 pingTimeout : PING_TIMEOUT ,
201192 retryTransientErrors : false ,
202193 retryPolicy : Schedule . recurs ( 0 ) ,
203- onPingTimeout : Ref . set ( pingTimedOut , true ) ,
194+ // RpcClient runs this before broadcasting the failure to active requests.
195+ onPingTimeout : Effect . sync ( ( ) => {
196+ pingTimedOut = true ;
197+ } ) ,
204198 } ) ,
205199 ) . pipe (
206200 Layer . provide (
@@ -216,7 +210,10 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
216210 ) ;
217211 const protocolClient = yield * makeWsRpcProtocolClient . pipe ( Effect . provide ( protocolContext ) ) ;
218212 const initialConfigDeferred = yield * Deferred . make < ServerConfig > ( ) ;
219- const serverConfigExit = yield * Deferred . make < void , ServerConfigSubscriptionError > ( ) ;
213+ const serverConfigExit = yield * Deferred . make <
214+ void ,
215+ ServerConfigSubscriptionError | ConnectionBlockedError
216+ > ( ) ;
220217 const configSubscriptionClosed = yield * Deferred . make < never , ConnectionAttemptError > ( ) ;
221218 const serverConfigState = yield * Ref . make ( Option . none < ServerConfigReplayState > ( ) ) ;
222219 const serverConfigUpdates = yield * PubSub . sliding < BufferedServerConfigEvent > ( 64 ) ;
@@ -271,6 +268,16 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
271268 }
272269 } ) ,
273270 ) ,
271+ Effect . catchDefect ( ( defect ) =>
272+ Schema . isSchemaError ( defect )
273+ ? Effect . fail (
274+ new ConnectionBlockedError ( {
275+ reason : "unsupported" ,
276+ detail : `${ connection . label } sent an incompatible server configuration. Update the client and server to compatible versions.` ,
277+ } ) ,
278+ )
279+ : Effect . die ( defect ) ,
280+ ) ,
274281 Effect . onExit ( ( exit ) => {
275282 if ( Exit . isSuccess ( exit ) ) {
276283 return Effect . all ( [
@@ -333,7 +340,9 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
333340 ) . pipe (
334341 Stream . catchCause ( ( cause ) => {
335342 if ( Cause . hasInterruptsOnly ( cause ) ) {
336- return Stream . failCause ( cause ) ;
343+ return Stream . failCause (
344+ Cause . fromReasons < never > ( cause . reasons . filter ( Cause . isInterruptReason ) ) ,
345+ ) ;
337346 }
338347 // The supervisor keeps the original cause. Shared durable consumers
339348 // need a transport-shaped failure so they wait for its replacement.
@@ -383,16 +392,10 @@ export const make = Effect.fn("RpcSessionFactory.make")(function* (
383392 client : protocolClient ,
384393 initialConfig,
385394 subscribeServerConfig,
386- ready : Deferred . await ( connected ) . pipe (
387- Effect . andThen ( initialConfig ) ,
388- Effect . asVoid ,
389- Effect . raceFirst ( Deferred . await ( disconnected ) ) ,
390- ) ,
395+ ready : initialConfig . pipe ( Effect . asVoid ) ,
391396 probe,
392- closed : Effect . raceFirst (
393- Deferred . await ( disconnected ) ,
394- Deferred . await ( configSubscriptionClosed ) ,
395- ) ,
397+ // Config decoding must reach the supervisor before a generic disconnect can mask it.
398+ closed : Deferred . await ( configSubscriptionClosed ) ,
396399 } satisfies RpcSession ;
397400 } ) ;
398401
0 commit comments