crawlee icon indicating copy to clipboard operation
crawlee copied to clipboard

AdaptivePlaywrightCrawler: a rejected HTTP-only attempt leaves request.noRetry set for the browser attempt

Open nikitachapovskii-dev opened this issue 1 month ago • 2 comments

Context

Migrating WCC to v4. In adaptive mode a non-HTML page fails the HTTP-only attempt and is rendered in a browser. If the browser attempt then fails for a transient reason, the page is lost instead of retried.

Which package is this bug report for?

@crawlee/playwright (AdaptivePlaywrightCrawler)

Issue description

When the HTTP-only attempt gets a Content-Type the HTTP crawler doesn't accept, #abortDownloadOfBody sets request.noRetry = true and throws. The adaptive crawler then falls back to the browser. Both attempts share the same request, because crawlOne() only copies the context shallowly, so the flag stays set. If the browser attempt fails, the request isn't retried at all, even though its error is retryable.

Expected behavior: a rejected HTTP-only attempt doesn't change how the browser attempt is retried. For example, a noRetry set during the HTTP-only attempt could be reset when the crawler falls back to the browser.

Code sample

import { createServer } from 'node:http';
import { AdaptivePlaywrightCrawler } from '@crawlee/playwright';

// A plain-text page: the HTTP-only attempt rejects its Content-Type, a browser renders it fine.
const server = createServer((req, res) => res.writeHead(200, { 'content-type': 'text/plain' }).end('hello'));
await new Promise((resolve) => server.listen(0, resolve));
const url = `http://127.0.0.1:${server.address().port}/`;

async function crawl(renderingType) {
    let handlerCalls = 0;
    const crawler = new AdaptivePlaywrightCrawler({
        renderingTypePredictor: {
            predict: () => ({ renderingType, detectionProbabilityRecommendation: 0 }),
            storeResult: () => {},
            initialize: async () => {},
        },
        maxRequestRetries: 3,
        requestHandler: async () => {
            handlerCalls++;
            if (handlerCalls === 1) throw new Error('a transient failure');
            console.log(`${renderingType}: handled on retry ${handlerCalls - 1}`);
        },
        failedRequestHandler: async ({ request }) => {
            console.log(`${renderingType}: failed after ${request.retryCount} retries (noRetry: ${request.noRetry})`);
        },
    });
    await crawler.run([url]);
}

await crawl('clientOnly'); // browser only
await crawl('static'); // HTTP-only first, then the browser
server.close();

Output:

clientOnly: handled on retry 1
static: failed after 0 retries (noRetry: true)

Same page, same transient failure: it is retried when the browser runs alone, and not retried after the HTTP-only attempt.

Package version

Node.js version

v24.15.0

Operating system

macOS 26.6.2 (arm64)

reproduced locally.

nikitachapovskii-dev avatar Oct 13 '26 15:10 nikitachapovskii-dev

Verified against current master — the mechanism checks out end to end:

  1. packages/http-crawler/src/internals/http-crawler.ts #abortDownloadOfBody (called from the ContextPipeline teardown at the ({ request, response }) => this.#abortDownloadOfBody(...) hook): for an unsupported non-transient Content-Type it sets request.noRetry = true on the shared CrawlingRequest instance and throws.
  2. packages/playwright-crawler/src/internals/adaptive-playwright-crawler.ts: the HTTP-only attempt and the browser fallback both go through #crawlOne(..., crawlingContext, ...) with the same crawlingContext, so the same request object carries the flag into the browser run.
  3. packages/basic-crawler/src/internals/basic-crawler.ts #canRequestBeRetried: if (request.noRetry || error instanceof NonRetryableError) return false; — so when the browser attempt then fails with a retryable error, the request is settled as failed with zero retries, exactly as @nikitachapovskii-dev describes.

One nuance worth noting for the fix discussion: noRetry is also load-bearing for the intended skip semantics (#trySkipRequest sets it deliberately), so clearing it globally in the adaptive layer should be scoped — capture request.noRetry before the HTTP-only attempt and restore it just before the browser fallback #crawlOne call, so a rejected static attempt contributes no retry state to the browser attempt, while genuine skips still short-circuit.

Happy to send a small PR along those lines (snapshot/restore around the fallback) if nobody's picked it up — cc @barjin for the direction preference (restore vs. cloning request state per attempt, same axis as #4260).

heyuhe2003 avatar Oct 14 '26 01:10 heyuhe2003

The range of modifications that a request handler can perform on the request object are much wider — user data, for instance. We should come up with a way to monitor these systematically instead of whack-a-moling noRetry now. crawlee-python already has some tracking mechanism in place already, I believe.

janbuchar avatar Oct 15 '26 09:10 janbuchar