Skip to content

Commit 139dbbc

Browse files
fix(client): a message the host never answers fails after 60 seconds instead of spinning forever
Fixes #23661 AI-assisted; verified with the tests and tools listed above.
1 parent b68aedf commit 139dbbc

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎packages/client-runtime/src/operations/commands.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,45 @@ describe("V2 environment commands", () => {
419419
}).pipe(Effect.provide(layerTestRandom)),
420420
);
421421

422+
it.effect("fails a message dispatch the host never answers on a live socket", () =>
423+
Effect.gen(function* () {
424+
const commands: OrchestrationV2Command[] = [];
425+
const supervisor = yield* makeSupervisor({ commands, projects: [] });
426+
const healthySession = Option.getOrThrow(yield* SubscriptionRef.get(supervisor.session));
427+
const silentClient = {
428+
...healthySession.client,
429+
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command: OrchestrationV2Command) =>
430+
Effect.sync(() => commands.push(command)).pipe(Effect.andThen(Effect.never)),
431+
} as unknown as WsRpcProtocolClient;
432+
yield* SubscriptionRef.set(
433+
supervisor.session,
434+
Option.some({ ...healthySession, client: silentClient }),
435+
);
436+
437+
const sendFiber = yield* startThreadTurn({
438+
commandId: CommandId.make("command-unanswered"),
439+
threadId: v2ThreadId,
440+
message: {
441+
messageId: MessageId.make("message-unanswered"),
442+
role: "user",
443+
text: "hello",
444+
attachments: [],
445+
},
446+
runtimeMode: "full-access",
447+
interactionMode: "default",
448+
}).pipe(
449+
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
450+
Effect.flip,
451+
Effect.forkChild,
452+
);
453+
yield* TestClock.adjust("1 minute");
454+
455+
expect(sendFiber.pollUnsafe()).toBeDefined();
456+
expect((yield* Fiber.join(sendFiber))._tag).toBe("EnvironmentRpcUnavailableError");
457+
expect(commands.map((command) => command.commandId)).toEqual(["command-unanswered"]);
458+
}).pipe(Effect.provide(layerTestRandom)),
459+
);
460+
422461
it.effect("holds a lost message through reconnect backoff until disconnect", () =>
423462
Effect.gen(function* () {
424463
const commands: OrchestrationV2Command[] = [];

‎packages/client-runtime/src/operations/commands.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ import * as Stream from "effect/Stream";
3939
import * as SubscriptionRef from "effect/SubscriptionRef";
4040

4141
import * 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

4449
interface CommandMetadata {
4550
readonly commandId?: CommandId;
@@ -293,15 +298,31 @@ const dispatch = (command: OrchestrationV2Command) =>
293298
// stops a frame that kills the socket from looping.
294299
const 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+
296305
const 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

Comments
 (0)