Q56: disconnecting a provider stops the sessions using it
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9e1f8da11c
commit
d72333a04c
5 files changed
+46
-17
No files matched your search
@@ -17,12 +17,12 @@ export async function withRunControl<T>(runId: string, operation: () => Promise<
|
||||
}
|
||||
}
|
||||
|
||||
/** Stops one in-flight run; false when it is not running in this process. */
|
||||
export function stopRun(runId: string): boolean {
|
||||
/** Stops one in-flight run with a reason; false when it is not running in this process. */
|
||||
export function stopRun(runId: string, reason: string = RUN_STOPPED_MESSAGE): boolean {
|
||||
const controller = controllers.get(runId);
|
||||
if (!controller) return false;
|
||||
console.info(`[run-control] stopping run ${runId}`);
|
||||
controller.abort();
|
||||
console.info(`[run-control] stopping run ${runId}: ${reason}`);
|
||||
controller.abort(new Error(reason));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -37,17 +37,25 @@ export function currentRunSignal(): AbortSignal | undefined {
|
||||
return runSignals.getStore();
|
||||
}
|
||||
|
||||
/** Why the current run was stopped, or null while it may continue. */
|
||||
export function runStopReason(): string | null {
|
||||
const signal = currentRunSignal();
|
||||
if (!signal?.aborted) return null;
|
||||
return signal.reason instanceof Error ? signal.reason.message : RUN_STOPPED_MESSAGE;
|
||||
}
|
||||
|
||||
export function throwIfRunStopped(): void {
|
||||
if (currentRunSignal()?.aborted) throw new Error(RUN_STOPPED_MESSAGE);
|
||||
const reason = runStopReason();
|
||||
if (reason) throw new Error(reason);
|
||||
}
|
||||
|
||||
/** Settles with the operation, or rejects as soon as the run is stopped; work without its own abort is abandoned. */
|
||||
export function untilStopped<T>(operation: Promise<T>): Promise<T> {
|
||||
const signal = currentRunSignal();
|
||||
if (!signal) return operation;
|
||||
if (signal.aborted) return Promise.reject(new Error(RUN_STOPPED_MESSAGE));
|
||||
if (signal.aborted) return Promise.reject(new Error(runStopReason() ?? RUN_STOPPED_MESSAGE));
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const onAbort = () => reject(new Error(RUN_STOPPED_MESSAGE));
|
||||
const onAbort = () => reject(new Error(runStopReason() ?? RUN_STOPPED_MESSAGE));
|
||||
signal.addEventListener("abort", onAbort, { once: true });
|
||||
operation.then(resolve, reject).finally(() => signal.removeEventListener("abort", onAbort));
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user