{"record":{"id":"51580dabfe177266","repo":"apify/crawlee","slug":"cannot-add-session-with-id-id-as-it-already-e","errorCode":null,"errorMessage":"Cannot add session with id '${id}' as it already exists in the pool","messagePattern":"Cannot add session with id '(.+?)' as it already exists in the pool","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/session_pool/session_pool.ts","lineNumber":273,"sourceCode":"        await this.maybeLoadSessionPool();\n\n        this.#listener = this.persistState.bind(this);\n        this.#events.on(EventType.PERSIST_STATE, this.#listener);\n    }\n\n    /**\n     * Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,\n     * but this allows you to add more sessions once the max pool size is reached.\n     * This also allows you to add session with overridden session options (e.g. with specific session id).\n     * @param [options] The configuration options for the session being added to the session pool.\n     */\n    async addSession(options: Session | SessionOptions = {}): Promise<void> {\n        await this.ensureInitialized();\n        const { id } = options;\n        if (id) {\n            const sessionExists = this.#sessionMap.has(id);\n            if (sessionExists) {\n                throw new Error(`Cannot add session with id '${id}' as it already exists in the pool`);\n            }\n        }\n\n        if (!this.hasSpaceForSession()) {\n            this.removeRetiredSessions();\n        }\n\n        const newSession = options instanceof Session ? options : await this.invokeCreateSessionFunction(options);\n        this.#log.debug(`Adding new Session - ${newSession.id}`);\n\n        this.registerSession(newSession);\n    }\n\n    /**\n     * Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,\n     * but this allows you to add more sessions once the max pool size is reached.\n     * This also allows you to add session with overridden session options (e.g. with specific session id).\n     * @param [options] The configuration options for the session being added to the session pool.","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/core/src/session_pool/session_pool.ts#L255-L291","documentation":"SessionPool.addSession() throws when a session with the same explicit id already exists in the pool's internal map. Duplicate session ids would break pooling state tracking, so the pool rejects them eagerly.","triggerScenarios":"Calling pool.addSession({ id: 'same-id' }) twice without removing the first session; or calling addSession for an id that crawlee itself already created inside the pool.","commonSituations":"Pre-seeding sessions from a saved state file and also re-adding them in code, retry logic that re-adds sessions on pool reinitialization, or concurrent addSession calls with the same id.","solutions":["Check pool session existence (e.g. via getSession(id) / internal state) before calling addSession with an explicit id.","Omit the id and let the pool generate unique ids.","Remove the existing/retired session before re-adding one with the same id.","Guard re-add logic in recovery code so it runs only when the pool is empty."],"exampleFix":"// before\nawait pool.addSession({ id: 'user-1' });\nawait pool.addSession({ id: 'user-1' }); // throws\n\n// after\nif (!await pool.getSession('user-1') && !pool.retiredSessionsCount) {\n    await pool.addSession({ id: 'user-1' });\n}","handlingStrategy":"validation","validationCode":"const existing = await pool.getSession(id).catch(() => null);\nif (!existing) {\n    await pool.addSession({ id });\n}","typeGuard":null,"tryCatchPattern":"try {\n    await pool.addSession({ id });\n} catch (e) {\n    if (e instanceof Error && e.message.includes('already exists in the pool')) {\n        // session already present – safe to skip\n    } else throw e;\n}","preventionTips":["Look up the session before adding one with an explicit id.","Let the pool auto-generate ids unless you must control them.","Make session-seeding idempotent (check-then-add or catch-and-skip)."],"tags":["session","duplicate","session-pool"],"backgroundTag":"duplicate-resource-id","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}