Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/features/execution/__tests__/useCodeExecution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,30 @@ describe('useCodeExecution live state', () => {
expect(result.current.output?.status).toBe('succeeded');
});
});

describe('useCodeExecution Stop', () => {
it('cancel() asks the server to stop the run, not only closes the stream', async () => {
const cancelRpc = vi.fn(() => ({ cancelled: true }));
const { wrapper } = rpcWrapper(({ service }) =>
service(ExecutionService, {
async *run(_req: unknown, ctx: { signal: AbortSignal }) {
yield { seq: 1n, event: { case: 'accepted', value: { executionId: 'exec_0123456789abcdefghijkl' } } };
await new Promise<void>((resolve) => ctx.signal.addEventListener('abort', () => resolve()));
},
cancel: cancelRpc,
} as never)
);
const { result } = renderHook(() => useCodeExecution(), { wrapper });
let done: Promise<void> = Promise.resolve();
act(() => {
done = result.current.execute('출력(1)', 'kingsejong', languages);
});
await waitFor(() => expect(result.current.streaming).toBe(true));
await act(async () => {
result.current.cancel();
await done;
});
expect(cancelRpc).toHaveBeenCalledWith(expect.objectContaining({ executionId: 'exec_0123456789abcdefghijkl' }), expect.anything());
expect(result.current.output?.status).toBe('cancelled');
});
});
92 changes: 91 additions & 1 deletion src/lib/rpc/__tests__/run-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
RunEvent_Stream,
type RunEvent,
} from '@kingsejong-lang/playground-proto';
import { RunAccumulator, runSession, type ExecutionResult } from '../run-stream';
import { ACCEPT_GRACE_MS, RunAccumulator, runSession, type ExecutionResult } from '../run-stream';
import { createApiTransport } from '../transport';

const enc = new TextEncoder();
Expand Down Expand Up @@ -318,3 +318,93 @@ describe('runSession output replay (ResumeRun)', () => {
expect(result.status).toBe('cancelled');
});
});

describe('runSession Stop sends an explicit Cancel', () => {
const id = 'exec_0123456789abcdefghijkl';

/** A run that streams Accepted (optionally after a delay) and output until its stream is closed. */
function liveClient(opts: { acceptAfter?: Promise<void>; never?: boolean } = {}) {
const cancel = vi.fn().mockResolvedValue({ cancelled: true });
const closed = vi.fn();
const client = clientWith(({ service }) =>
service(ExecutionService, {
async *run(_req: unknown, ctx: { signal: AbortSignal }) {
ctx.signal.addEventListener('abort', closed);
await opts.acceptAfter;
if (!opts.never) yield accepted();
yield out(2, 'working\n');
await new Promise<void>((resolve) => ctx.signal.addEventListener('abort', () => resolve()));
},
cancel,
} as never)
);
return { client, cancel, closed };
}

it('cancels the run on the server and closes the stream when stopped mid-stream', async () => {
const { client, cancel } = liveClient();
const ac = new AbortController();
const pending = runSession({ client, request, signal: ac.signal, onUpdate: (r) => r.chunks.length && ac.abort() });
const result = await pending;
expect(cancel).toHaveBeenCalledTimes(1);
expect(cancel).toHaveBeenCalledWith(expect.objectContaining({ executionId: id }), expect.anything());
expect(result.status).toBe('cancelled');
expect(result.chunks[0].text).toBe('working\n');
});

it('stopped before Accepted: keeps the stream open for the id, then cancels', async () => {
let accept!: () => void;
const { client, cancel } = liveClient({ acceptAfter: new Promise<void>((r) => (accept = r)) });
const ac = new AbortController();
const pending = runSession({ client, request, signal: ac.signal });
ac.abort();
await new Promise((r) => setTimeout(r, 10));
expect(cancel).not.toHaveBeenCalled();
accept();
const result = await pending;
expect(cancel).toHaveBeenCalledWith(expect.objectContaining({ executionId: id }), expect.anything());
expect(result.status).toBe('cancelled');
});

it('gives up waiting for Accepted after the grace period', async () => {
vi.useFakeTimers();
try {
const { client, cancel } = liveClient({ acceptAfter: new Promise<void>(() => {}) });
const ac = new AbortController();
const pending = runSession({ client, request, signal: ac.signal });
ac.abort();
await vi.advanceTimersByTimeAsync(ACCEPT_GRACE_MS + 10);
const result = await pending;
expect(cancel).not.toHaveBeenCalled();
expect(result.status).toBe('cancelled');
} finally {
vi.useRealTimers();
}
});

it('still ends as cancelled when Cancel fails', async () => {
const { client, cancel } = liveClient();
cancel.mockRejectedValue(new ConnectError('down', Code.Unavailable));
const ac = new AbortController();
const result = await runSession({ client, request, signal: ac.signal, onUpdate: (r) => r.executionId && ac.abort() });
expect(cancel).toHaveBeenCalledTimes(1);
expect(result.status).toBe('cancelled');
});

it('does not call Cancel for a run that finished normally', async () => {
const cancel = vi.fn();
const client = clientWith(({ service }) =>
service(ExecutionService, {
async *run() {
yield accepted();
yield finished(2);
},
cancel,
} as never)
);
const ac = new AbortController();
await runSession({ client, request, signal: ac.signal });
ac.abort(); // e.g. the component unmounts afterwards
expect(cancel).not.toHaveBeenCalled();
});
});
78 changes: 62 additions & 16 deletions src/lib/rpc/run-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* last received seq), which replays the missed events (de-duplicated by seq)
* and continues live. When the server cannot replay (REPLAY_UNAVAILABLE) or
* does not implement ResumeRun yet, it falls back to polling GetExecution
* for the final status. Aborting while reconnecting calls Cancel.
* for the final status.
* - Aborting (Stop) always sends Cancel(execution_id): closing the stream alone
* does not stop the run on the server, so ResumeRun can recover it.
*
* Usage:
* const ac = new AbortController();
Expand Down Expand Up @@ -320,6 +322,15 @@ export interface RunSessionOptions {

export const DEFAULT_RECONNECT_DELAYS_MS = [1000, 2000, 4000, 8000, 8000] as const;

/**
* After Stop is pressed before Accepted, how long the stream is kept open to
* learn the execution id so the run can be cancelled explicitly.
*/
export const ACCEPT_GRACE_MS = 3000;

/** Longest wait for the Cancel response before the run is shown as cancelled. */
export const CANCEL_WAIT_MS = 1000;

export const LOST_CONNECTION_MESSAGE = 'Lost connection to the server; the final status is unknown.';

function defaultSleep(ms: number, signal: AbortSignal): Promise<void> {
Expand Down Expand Up @@ -347,17 +358,51 @@ export async function runSession(opts: RunSessionOptions): Promise<ExecutionResu
const acc = new RunAccumulator();
const emit = () => onUpdate?.(acc.snapshot());

// Closing the stream does not stop the run on the server (so ResumeRun can
// recover it after a network drop); only an explicit Cancel does. Stop
// (aborting `signal`) therefore sends Cancel(execution_id) and then closes the
// stream. Before Accepted the id is unknown: the stream stays open for up to
// ACCEPT_GRACE_MS to receive it.
const stream = new AbortController();
let cancelRequested = false;
let cancelCall: Promise<unknown> | null = null;
const sendCancel = (executionId: string) => {
// Not tied to `signal`, which is already aborted. Best effort: errors are ignored.
cancelCall ??= client.cancel({ executionId }).catch(() => {});
};
const onAbort = () => {
cancelRequested = true;
if (acc.executionId) {
sendCancel(acc.executionId);
stream.abort();
} else {
setTimeout(() => stream.abort(), ACCEPT_GRACE_MS);
}
};
if (signal.aborted) onAbort();
else signal.addEventListener('abort', onAbort, { once: true });
/** Ends as cancelled once Cancel was answered (or after CANCEL_WAIT_MS, so Stop never hangs). */
const cancelled = async () => {
if (cancelCall) await Promise.race([cancelCall, new Promise((resolve) => setTimeout(resolve, CANCEL_WAIT_MS))]);
acc.end('cancelled', 'Cancelled', acc.snapshot().reconnected);
emit();
return acc.snapshot();
};

try {
// timeoutMs: 0 disables the transport's unary default; the server bounds the stream.
for await (const ev of client.run(request, { signal, timeoutMs: 0 })) {
for await (const ev of client.run(request, { signal: stream.signal, timeoutMs: 0 })) {
if (acc.apply(ev)) emit();
if (acc.isFinished) break;
if (cancelRequested && acc.executionId) {
sendCancel(acc.executionId);
break;
}
}
} catch (err) {
if (signal.aborted || isAbort(err)) {
acc.end('cancelled', 'Cancelled');
emit();
return acc.snapshot();
if (cancelRequested || isAbort(err)) {
if (acc.executionId) sendCancel(acc.executionId);
return cancelled();
}
if (!acc.executionId) throw err; // rejected before Accepted: an RPC error
const failure = toRpcFailure(err);
Expand All @@ -368,7 +413,11 @@ export async function runSession(opts: RunSessionOptions): Promise<ExecutionResu
}
}

if (acc.isFinished) return acc.snapshot();
if (acc.isFinished) {
signal.removeEventListener('abort', onAbort);
return acc.snapshot();
}
if (cancelRequested) return cancelled();

if (!acc.executionId) {
// Stream closed without Accepted: contract violation; nothing to reconnect to.
Expand All @@ -384,15 +433,8 @@ export async function runSession(opts: RunSessionOptions): Promise<ExecutionResu
for (const delay of delays) {
await sleep(delay, signal);
if (signal.aborted) {
// No stream to abort any more; ask the server to stop the run.
try {
await client.cancel({ executionId });
} catch {
// best effort; the server also stops idle runs
}
acc.end('cancelled', 'Cancelled', true);
emit();
return acc.snapshot();
sendCancel(executionId);
return cancelled();
}
if (replay) {
let resumedNow = false;
Expand Down Expand Up @@ -425,6 +467,10 @@ export async function runSession(opts: RunSessionOptions): Promise<ExecutionResu
// keep polling until retries are exhausted
}
}
if (signal.aborted) {
sendCancel(executionId);
return cancelled();
}
acc.end('unknown', LOST_CONNECTION_MESSAGE, true);
emit();
return acc.snapshot();
Expand Down
Loading