{"record":{"id":"34f64cdc1875b770","repo":"apify/crawlee","slug":"the-current-sessionpool-instance-couldn-t-find-a-v","errorCode":null,"errorMessage":"The current SessionPool instance couldn't find a valid session for the following id: ${request.sessionId}.","messagePattern":"The current SessionPool instance couldn't find a valid session for the following id: (.+?)\\.","errorType":"exception","errorClass":"ContextPipelineInitializationError","httpStatus":null,"severity":"error","filePath":"packages/basic-crawler/src/internals/basic-crawler.ts","lineNumber":1527,"sourceCode":"            this.internalTimeoutMillis,\n            `Fetching next request timed out after ${this.internalTimeoutMillis / 1e3} seconds.`,\n        );\n\n        // Reset loadedUrl so an old one is not carried over to retries.\n        if (request) {\n            request.loadedUrl = undefined;\n        }\n\n        return request;\n    }\n\n    private async resolveSession({ request }: { request: Request }) {\n        const session = await this.timeoutAndRetry(\n            async () => {\n                const existingSession = await this.sessionPool.getSession(request.sessionId);\n\n                if (!existingSession) {\n                    throw new ContextPipelineInitializationError(new MissingSessionError(request.sessionId));\n                }\n\n                return existingSession;\n            },\n            this.internalTimeoutMillis,\n            `Fetching session timed out after ${this.internalTimeoutMillis / 1e3} seconds.`,\n        );\n\n        return { session, proxyInfo: session?.proxyInfo };\n    }\n\n    private async createContextHelpers({ request, session }: { request: Request; session: ISession }) {\n        const addRequests: CrawlingContext['addRequests'] = async (requests, options = {}) => {\n            const newCrawlDepth = request!.crawlDepth + 1;\n            const requestsGenerator = this.addCrawlDepthRequestGenerator(requests, newCrawlDepth);\n\n            return await this.addRequests(requestsGenerator, options);\n        };","sourceCodeStart":1509,"sourceCodeEnd":1545,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/basic-crawler/src/internals/basic-crawler.ts#L1509-L1545","documentation":"resolveSession retries fetching the session identified by `request.sessionId` from the SessionPool; if the pool cannot produce a valid session for that id within the internal timeout, the request fails with a MissingSessionError wrapped in ContextPipelineInitializationError. This typically means the session was retired/invalidated and the pool has no usable replacement.","triggerScenarios":"A request carries a `sessionId` that no longer exists in the SessionPool (session expired, pool purged, or session pool recreated between runs), and repeated `sessionPool.getSession(id)` calls return null until the internal timeout elapses.","commonSituations":"Long-running crawls where sessions rotate; persisting requests with sessionId across crawler restarts into a fresh session pool; session pool max age smaller than crawl duration.","solutions":["Let the crawler assign sessions itself — remove a stale hardcoded `sessionId` from the request so a fresh one is allocated.","Increase sessionPool `maxPoolSize` or session `maxAgeSeconds` so valid sessions outlive the queue backlog.","Persist and restore the SessionPool (same storage) between runs so old sessionIds remain resolvable.","Clear `request.sessionId` in a request-handler retry path when a session is known to have been retired."],"exampleFix":"// before (reusing stale session across restarts)\nawait crawler.addRequests([{ url, sessionId: 'sess-123' }]);\n// after (let the pool pick a valid session)\nawait crawler.addRequests([{ url }]);","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await crawler.run(requests);\n} catch (err) {\n  if (err instanceof ContextPipelineInitializationError && err.message.includes('valid session')) {\n    // drop stale sessionId from the request and re-enqueue it\n    delete request.sessionId;\n    await crawler.addRequests([request]);\n  } else throw err;\n}","preventionTips":["Do not hardcode or persist sessionId values across crawler restarts unless the SessionPool storage is also persisted.","Size the session pool (maxPoolSize, maxAgeSeconds) to outlive your queue backlog.","Let BasicCrawler assign sessions automatically rather than pinning them per request.","Monitor session pool utilization and retirement rates."],"tags":["session","session-pool","timeout"],"backgroundTag":"session-not-found","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}