Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(worker-pool): a throwing callback joins sibling workers before rejecting (#8913)

garrytanc62e0d084e52
diff --git a/src/core/worker-pool.ts b/src/core/worker-pool.ts
index 76c34ac967..e05cafb0f5 100644
--- a/src/core/worker-pool.ts
+++ b/src/core/worker-pool.ts
@@ -175,8 +175,9 @@ export interface SlidingPoolResult {
*
* A must-abort error from an item (BudgetExhausted etc.) stops claiming,
* aborts the local signal so sibling workers finish their current item and
- * stop, waits for every worker, then rethrows the error. No worker is left
- * running detached after the pool has reported failure.
+ * stop, waits for every worker, then rethrows the error. An error thrown by
+ * the onError or failureLabel callback does the same (#8913). No worker is
+ * left running detached after the pool has reported failure.
*/
export async function runSlidingPool<T>(opts: SlidingPoolOpts<T>): Promise<SlidingPoolResult> {
const items = opts.items;
@@ -213,10 +214,11 @@ export async function runSlidingPool<T>(opts: SlidingPoolOpts<T>): Promise<Slidi
// that put an `await` between the `nextIdx` read and write OR import
// `worker_threads` alongside this module.
let nextIdx = 0;
- // First must-abort error. Recorded by the worker that saw it, rethrown
- // after every worker has finished its current item. (A holder object: TS
- // does not see assignments made inside the worker closure.)
- const mustAbort: { failure: { error: unknown } | null } = { failure: null };
+ // First fatal error: a must-abort item error, or what a throwing onError /
+ // failureLabel callback raised (#8913). Recorded by the worker that saw it,
+ // rethrown after every worker has finished its current item. (A holder
+ // object: TS does not see assignments made inside the worker closure.)
+ const fatal: { failure: { error: unknown } | null } = { failure: null };
async function worker(workerIdx: number): Promise<void> {
while (true) {
@@ -241,10 +243,10 @@ export async function runSlidingPool<T>(opts: SlidingPoolOpts<T>): Promise<Slidi
// error is rethrown after the join (see the function doc).
if (isMustAbortError(err)) {
result.aborted = true;
+ if (!fatal.failure) fatal.failure = { error: err };
+ localAbort.abort();
result.errored++;
result.failures.push({ idx, label: labelFn(item), error: err });
- if (!mustAbort.failure) mustAbort.failure = { error: err };
- localAbort.abort();
return;
}
result.errored++;
@@ -262,12 +264,15 @@ export async function runSlidingPool<T>(opts: SlidingPoolOpts<T>): Promise<Slidi
try {
await Promise.all(
- Array.from({ length: workerCount }, (_, w) => worker(w)),
+ Array.from({ length: workerCount }, (_, w) => worker(w).catch((err: unknown) => {
+ if (!fatal.failure) fatal.failure = { error: err };
+ localAbort.abort();
+ })),
);
} finally {
if (opts.signal) opts.signal.removeEventListener('abort', onCallerAbort);
}
- if (mustAbort.failure) throw mustAbort.failure.error;
+ if (fatal.failure) throw fatal.failure.error;
return result;
}
diff --git a/test/worker-pool.test.ts b/test/worker-pool.test.ts
index b3c1178b2e..72cbad4c5a 100644
--- a/test/worker-pool.test.ts
+++ b/test/worker-pool.test.ts
@@ -391,6 +391,16 @@ describe('runSlidingPool — BudgetExhausted bypass (D13)', () => {
expect(done).toBe(1);
});
+ test('a must-abort error survives a throwing failureLabel', async () => {
+ const budget = Object.assign(new Error('cap'), { tag: 'BUDGET_EXHAUSTED' as const });
+ await expect(runSlidingPool({
+ items: [1, 2],
+ workers: 1,
+ onItem: async () => { throw budget; },
+ failureLabel: () => { throw new Error('label projector failed'); },
+ })).rejects.toBe(budget);
+ });
+
test('isMustAbortError + MUST_ABORT_ERROR_TAGS exposed and stable', () => {
expect(MUST_ABORT_ERROR_TAGS.has('BUDGET_EXHAUSTED')).toBe(true);
expect(isMustAbortError({ tag: 'BUDGET_EXHAUSTED' })).toBe(true);
@@ -486,3 +496,33 @@ describe('runSlidingPool — worker slot index passed to onItem', () => {
expect(slotsSeen.size).toBe(5);
});
});
+
+describe('runSlidingPool — a throwing callback (#8913)', () => {
+ for (const callback of ['onError', 'failureLabel'] as const) {
+ test(`a throwing ${callback} rejects only after in-flight siblings finish, and nothing new is claimed`, async () => {
+ const callbackError = new Error(`${callback} threw`);
+ let release!: () => void;
+ const held = new Promise<void>((resolve) => { release = resolve; });
+ const claimed: number[] = [];
+ let siblingDone = false;
+ let settled = false;
+ const outcome = runSlidingPool({
+ items: [0, 1, 2, 3],
+ workers: 2,
+ onItem: async (item: number) => {
+ claimed.push(item);
+ if (item === 0) throw new Error('item failed');
+ await held;
+ siblingDone = true;
+ },
+ [callback]: () => { throw callbackError; },
+ }).then(() => null, (err: unknown) => err).finally(() => { settled = true; });
+ await new Promise((r) => setTimeout(r, 20));
+ expect(settled).toBe(false);
+ release();
+ expect(await outcome).toBe(callbackError);
+ expect(siblingDone).toBe(true);
+ expect(claimed).toEqual([0, 1]);
+ });
+ }
+});