Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix: await retries inside `_timeoutAndRetry` (#3206)

Lukáš Průša9c1cf6d68acd
diff --git a/packages/basic-crawler/src/internals/basic-crawler.ts b/packages/basic-crawler/src/internals/basic-crawler.ts
index 62b2fe5bb..f3919651f 100644
--- a/packages/basic-crawler/src/internals/basic-crawler.ts
+++ b/packages/basic-crawler/src/internals/basic-crawler.ts
@@ -1511,28 +1511,21 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
const source = this.requestManager;
if (!source) throw new Error('Request provider is not initialized!');
- let request: Request | null | undefined;
- let session: Session | undefined;
-
- await this._timeoutAndRetry(
- async () => {
- request = await this._fetchNextRequest();
- },
+ const request = await this._timeoutAndRetry(
+ this._fetchNextRequest.bind(this),
this.internalTimeoutMillis,
`Fetching next request timed out after ${this.internalTimeoutMillis / 1e3} seconds.`,
);
tryCancel();
- if (this.useSessionPool) {
- await this._timeoutAndRetry(
- async () => {
- session = await this.sessionPool!.getSession();
- },
- this.internalTimeoutMillis,
- `Fetching session timed out after ${this.internalTimeoutMillis / 1e3} seconds.`,
- );
- }
+ const session = this.useSessionPool
+ ? await this._timeoutAndRetry(
+ this.sessionPool!.getSession.bind(this.sessionPool),
+ this.internalTimeoutMillis,
+ `Fetching session timed out after ${this.internalTimeoutMillis / 1e3} seconds.`,
+ )
+ : undefined;
tryCancel();
@@ -1694,24 +1687,23 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
}
/**
- * Run async callback with given timeout and retry.
+ * Run async callback with given timeout and retry. Returns the result of the callback.
* @ignore
*/
- protected async _timeoutAndRetry(
- handler: () => Promise<unknown>,
+ protected async _timeoutAndRetry<T>(
+ handler: () => Promise<T>,
timeout: number,
error: Error | string,
maxRetries = 3,
retried = 1,
- ): Promise<void> {
+ ): Promise<T> {
try {
- await addTimeoutToPromise(handler, timeout, error);
+ return await addTimeoutToPromise(handler, timeout, error);
} catch (e) {
if (retried <= maxRetries) {
// we retry on any error, not just timeout
this.log.warning(`${(e as Error).message} (retrying ${retried}/${maxRetries})`);
- void this._timeoutAndRetry(handler, timeout, error, maxRetries, retried + 1);
- return;
+ return this._timeoutAndRetry(handler, timeout, error, maxRetries, retried + 1);
}
throw e;