{"record":{"id":"890a88a3294a3d71","repo":"apify/crawlee","slug":"fetchnextrequest-called-on-an-uninitialized-crawle","errorCode":null,"errorMessage":"fetchNextRequest called on an uninitialized crawler","messagePattern":"fetchNextRequest called on an uninitialized crawler","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/basic-crawler/src/internals/basic-crawler.ts","lineNumber":2606,"sourceCode":"                        this.log.exception(\n                            err,\n                            'An unexpected error occurred when the crawler ' +\n                                \"attempted to persist its request list's state.\",\n                        );\n                    }\n                });\n            }\n        })();\n\n        await Promise.all([requestManagerPersistPromise, this.statistics.persistState?.()]);\n    }\n\n    /**\n     * Fetches the next request to process from the underlying request provider.\n     */\n    private async fetchNextRequest() {\n        if (this.requestManager === undefined) {\n            throw new Error(`fetchNextRequest called on an uninitialized crawler`);\n        }\n\n        return this.requestManager.fetchNextRequest();\n    }\n\n    /** Handles a single request - runs the request handler with retries, error handling, and lifecycle management. */\n    private async handleRequest(crawlingContext: ExtendedContext, requestSource: IRequestManager, request: Request) {\n        // An earlier phase we cannot cancel (e.g. a slow `extendContext`) may have run past the internal timeout,\n        // which already failed the request in `runTaskFunction`. Bail before running the handler so it does not\n        // execute (and re-report) on top of a request the crawler has already moved past.\n        if ((crawlingContext as PendingCrawlingContext)[timeoutExpiredKey]?.()) {\n            return;\n        }\n\n        const statisticsId = request.id || request.uniqueKey;\n\n        // Opened by `runInStorageTransaction`; absent when disabled or when the subclass opens its own.\n        const transaction = currentStorageTransaction();","sourceCodeStart":2588,"sourceCodeEnd":2624,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/basic-crawler/src/internals/basic-crawler.ts#L2588-L2624","documentation":"This internal invariant error means BasicCrawler's fetchNextRequest was invoked before the crawler finished initialization: the requestManager (request provider) is still undefined. It indicates the crawler's internal lifecycle was violated, typically by calling internal methods or racing the crawler before startup.","triggerScenarios":"Calling internal/private crawler plumbing (or a subclass overriding lifecycle incorrectly) before run()/initialization completes; a bug or race where the request provider wasn't created before requests were fetched.","commonSituations":"Subclassing BasicCrawler and overriding _run or initialization steps incorrectly; calling internal APIs from tests; running into a library bug after upgrading versions.","solutions":["Ensure you only interact with the crawler through its public API (run(), addRequests())","If subclassing BasicCrawler, call super lifecycle methods and don't fetch requests before initialization completes","Update Crawlee to the latest version, as this may be an internal race condition bug; report it if reproducible"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await crawler.run(); } catch (e) { if (/fetchNextRequest called on an uninitialized crawler/.test(String(e.message))) { /* library bug/misuse: report; do not retry blindly */ } else throw e; }","preventionTips":["Use only the public crawler API (run, addRequests)","When subclassing, call super lifecycle methods in the correct order","Keep Crawlee updated to get lifecycle race fixes"],"tags":["internal-state","lifecycle","crawler"],"backgroundTag":"crawler-not-initialized","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}