@@ -39,7 +39,12 @@ import * as Stream from "effect/Stream";
3939import * as SubscriptionRef from "effect/SubscriptionRef" ;
4040
4141import * as EnvironmentSupervisor from "../connection/supervisor.ts" ;
42- import { getInitialServerConfig , isRpcClientError , request } from "../rpc/client.ts" ;
42+ import {
43+ EnvironmentRpcUnavailableError ,
44+ getInitialServerConfig ,
45+ isRpcClientError ,
46+ request ,
47+ } from "../rpc/client.ts" ;
4348
4449interface CommandMetadata {
4550 readonly commandId ?: CommandId ;
@@ -293,15 +298,31 @@ const dispatch = (command: OrchestrationV2Command) =>
293298// stops a frame that kills the socket from looping.
294299const MAX_LOST_TRANSPORT_RESENDS = 2 ;
295300
301+ // A live socket can still carry a command the host never answers; the heartbeat
302+ // alone would leave the send pending forever.
303+ const MESSAGE_DISPATCH_TIMEOUT = "60 seconds" ;
304+
296305const dispatchMessage = Effect . fn ( "EnvironmentCommands.dispatchMessage" ) ( function * (
297306 command : Extract < OrchestrationV2Command , { type : "message.dispatch" } > ,
298307 resendAfterSocketLoss : boolean ,
299308) {
300- if ( ! resendAfterSocketLoss ) return yield * dispatch ( command ) ;
301309 const supervisor = yield * EnvironmentSupervisor . EnvironmentSupervisor ;
310+ const attempt = dispatch ( command ) . pipe (
311+ Effect . timeoutOrElse ( {
312+ duration : MESSAGE_DISPATCH_TIMEOUT ,
313+ orElse : ( ) =>
314+ Effect . fail (
315+ new EnvironmentRpcUnavailableError ( {
316+ environmentId : supervisor . target . environmentId ,
317+ message : `${ supervisor . target . label } did not confirm the message within 60 seconds.` ,
318+ } ) ,
319+ ) ,
320+ } ) ,
321+ ) ;
322+ if ( ! resendAfterSocketLoss ) return yield * attempt ;
302323 for ( let resends = 0 ; ; resends += 1 ) {
303324 const attemptSession = yield * SubscriptionRef . get ( supervisor . session ) ;
304- const exit = yield * Effect . exit ( dispatch ( command ) ) ;
325+ const exit = yield * Effect . exit ( attempt ) ;
305326 if ( Exit . isSuccess ( exit ) ) return exit . value ;
306327 // Closing the session interrupts the request, which can beat the protocol error.
307328 const lostTransport =
0 commit comments