From c7437fd15e5aa0bc253e687e98a222e2557b9064 Mon Sep 17 00:00:00 2001 From: Jan Buchar Date: Tue, 1 Dec 2026 13:43:03 +0100 Subject: [PATCH 1/2] fix: Handle exceptions from addBatchOfRequests --- packages/core/src/storages/batched-adds.ts | 27 +++++++++++++++------- test/core/storages/batched-adds.test.ts | 22 +++++++++++------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/packages/core/src/storages/batched-adds.ts b/packages/core/src/storages/batched-adds.ts index ae451accdeda..e0934f04b160 100644 --- a/packages/core/src/storages/batched-adds.ts +++ b/packages/core/src/storages/batched-adds.ts @@ -114,19 +114,30 @@ export async function drainRequestBatches( return buildResult(addedRequests, Promise.resolve([])); } + // Under `deferred` a writer that finishes after commit would have nowhere to put its journal entries. + const awaitsRemainder = waitForAllRequestsToBeAdded || deferred; + + const backgroundFailures: unknown[] = []; + const processRemainingChunks = async () => { const added: ProcessedRequest[] = []; for await (const chunk of chunks) { - added.push(...(await addChunk(chunk, false))); + try { + added.push(...(await addChunk(chunk, false))); + } catch (error) { + if (awaitsRemainder) throw error; + // Nobody is waiting for this chunk, and losing it must not cost the ones after it. + log.exception(error as Error, 'A batch of requests could not be added to the queue in the background'); + backgroundFailures.push(error); + } // Under `deferred` no chunk performs backend I/O, so pacing them would only stall the handler. await sleep(deferred ? 0 : waitBetweenBatchesMillis); } + // Whoever does await the remainder still learns that it was not added in full. + if (backgroundFailures.length > 0) throw backgroundFailures[0]; return added; }; - // Under `deferred` a writer that finishes after commit would have nowhere to put its journal entries. - const awaitsRemainder = waitForAllRequestsToBeAdded || deferred; - // An un-awaited writer outlives the transaction scope it inherits, so it must not record into a // transaction that may already be closed. It writes directly - its write-through additions were never // going to be rolled back anyway - which means the requests it adds are not journaled. @@ -134,12 +145,12 @@ export async function drainRequestBatches( const remainder = awaitsRemainder ? processRemainingChunks() : withDirectStorageAccess(processRemainingChunks); // The caller is not obliged to await `remainder`, so give it a handler of its own - an unhandled - // rejection here would otherwise take the process down. Awaited failures reach the caller, so only - // background ones get logged. + // rejection here would otherwise take the process down. Awaited failures reach the caller, and failed + // chunks are logged as they happen, so what is left to log here is a failure of the input iterable. trackBackgroundBatches?.( remainder.catch((error) => { - if (!awaitsRemainder) { - log.exception(error as Error, 'A batch of requests could not be added to the queue in the background'); + if (!awaitsRemainder && !backgroundFailures.includes(error)) { + log.exception(error as Error, 'Adding requests to the queue in the background failed'); } }), ); diff --git a/test/core/storages/batched-adds.test.ts b/test/core/storages/batched-adds.test.ts index 7f6560fb790d..e79cfe2753d3 100644 --- a/test/core/storages/batched-adds.test.ts +++ b/test/core/storages/batched-adds.test.ts @@ -4,23 +4,26 @@ import { describe, expect, it, vitest } from 'vitest'; import { drainRequestBatches } from '../../../packages/core/src/storages/batched-adds.js'; import type { CrawleeLogger } from '../../../packages/core/src/log.js'; -async function* twoItems() { +async function* threeItems() { yield { url: 'https://example.com/1' }; yield { url: 'https://example.com/2' }; + yield { url: 'https://example.com/3' }; } function setup(waitForAllRequestsToBeAdded: boolean) { const exception = vitest.fn(); let tracked: Promise | undefined; + const processedUrls: string[] = []; const result = drainRequestBatches({ - items: twoItems(), + items: threeItems(), batchSize: 1, waitBetweenBatchesMillis: 0, waitForAllRequestsToBeAdded, log: { exception } as unknown as CrawleeLogger, - processChunk: async (chunk, isInitial) => { - if (!isInitial) throw new Error('boom'); + processChunk: async (chunk) => { + if (chunk[0].url.endsWith('/2')) throw new Error('boom'); + processedUrls.push(chunk[0].url); return chunk.map((): ProcessedRequest => ({ uniqueKey: '', requestId: '', @@ -33,16 +36,19 @@ function setup(waitForAllRequestsToBeAdded: boolean) { }, }); - return { exception, result, tracked: () => tracked }; + return { exception, result, tracked: () => tracked, processedUrls }; } describe('drainRequestBatches', () => { - it('logs a failed background batch', async () => { - const { exception, result, tracked } = setup(false); + it('logs a failed background batch, still adds the ones after it, and rejects at the end', async () => { + const { exception, result, tracked, processedUrls } = setup(false); - expect((await result).addedRequests).toHaveLength(1); + const { addedRequests, waitForAllRequestsToBeAdded } = await result; + expect(addedRequests).toHaveLength(1); + await expect(waitForAllRequestsToBeAdded).rejects.toThrow('boom'); await tracked(); + expect(processedUrls).toEqual(['https://example.com/1', 'https://example.com/3']); expect(exception).toHaveBeenCalledOnce(); }); From 8eb2db0459f51d9e8cb1407980476175e523032c Mon Sep 17 00:00:00 2001 From: Jan Buchar Date: Tue, 1 Dec 2026 14:59:27 +0100 Subject: [PATCH 2/2] Use AggregateError --- packages/core/src/storages/batched-adds.ts | 15 +++++++++++---- test/core/storages/batched-adds.test.ts | 5 ++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/core/src/storages/batched-adds.ts b/packages/core/src/storages/batched-adds.ts index e0934f04b160..f1e3210bb0e6 100644 --- a/packages/core/src/storages/batched-adds.ts +++ b/packages/core/src/storages/batched-adds.ts @@ -117,7 +117,8 @@ export async function drainRequestBatches( // Under `deferred` a writer that finishes after commit would have nowhere to put its journal entries. const awaitsRemainder = waitForAllRequestsToBeAdded || deferred; - const backgroundFailures: unknown[] = []; + const backgroundFailures: Error[] = []; + let backgroundFailuresError: AggregateError | undefined; const processRemainingChunks = async () => { const added: ProcessedRequest[] = []; @@ -128,13 +129,19 @@ export async function drainRequestBatches( if (awaitsRemainder) throw error; // Nobody is waiting for this chunk, and losing it must not cost the ones after it. log.exception(error as Error, 'A batch of requests could not be added to the queue in the background'); - backgroundFailures.push(error); + backgroundFailures.push(error as Error); } // Under `deferred` no chunk performs backend I/O, so pacing them would only stall the handler. await sleep(deferred ? 0 : waitBetweenBatchesMillis); } // Whoever does await the remainder still learns that it was not added in full. - if (backgroundFailures.length > 0) throw backgroundFailures[0]; + if (backgroundFailures.length > 0) { + backgroundFailuresError = new AggregateError( + backgroundFailures, + `${backgroundFailures.length} batch(es) of requests could not be added to the queue; first: ${backgroundFailures[0].message}`, + ); + throw backgroundFailuresError; + } return added; }; @@ -149,7 +156,7 @@ export async function drainRequestBatches( // chunks are logged as they happen, so what is left to log here is a failure of the input iterable. trackBackgroundBatches?.( remainder.catch((error) => { - if (!awaitsRemainder && !backgroundFailures.includes(error)) { + if (!awaitsRemainder && error !== backgroundFailuresError) { log.exception(error as Error, 'Adding requests to the queue in the background failed'); } }), diff --git a/test/core/storages/batched-adds.test.ts b/test/core/storages/batched-adds.test.ts index e79cfe2753d3..443eed4f904a 100644 --- a/test/core/storages/batched-adds.test.ts +++ b/test/core/storages/batched-adds.test.ts @@ -45,7 +45,10 @@ describe('drainRequestBatches', () => { const { addedRequests, waitForAllRequestsToBeAdded } = await result; expect(addedRequests).toHaveLength(1); - await expect(waitForAllRequestsToBeAdded).rejects.toThrow('boom'); + const rejection = await waitForAllRequestsToBeAdded.catch((error) => error); + expect(rejection).toBeInstanceOf(AggregateError); + expect(rejection.message).toContain('boom'); + expect(rejection.errors).toHaveLength(1); await tracked(); expect(processedUrls).toEqual(['https://example.com/1', 'https://example.com/3']);