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: make SessionPool queue up getSession calls to prevent overruns (#2239)

Vlad Frangu0f5665c47337
diff --git a/packages/core/package.json b/packages/core/package.json
index 1ae7be085..e1acaed14 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -62,7 +62,7 @@
"@crawlee/memory-storage": "3.6.2",
"@crawlee/types": "3.6.2",
"@crawlee/utils": "3.6.2",
- "@sapphire/async-queue": "^1.5.0",
+ "@sapphire/async-queue": "^1.5.1",
"@types/tough-cookie": "^4.0.2",
"@vladfrangu/async_event_emitter": "^2.2.2",
"csv-stringify": "^6.2.0",
diff --git a/packages/core/src/session_pool/session_pool.ts b/packages/core/src/session_pool/session_pool.ts
index 31ff548cb..64fad7ee7 100644
--- a/packages/core/src/session_pool/session_pool.ts
+++ b/packages/core/src/session_pool/session_pool.ts
@@ -2,6 +2,7 @@ import { EventEmitter } from 'node:events';
import type { Log } from '@apify/log';
import type { Dictionary } from '@crawlee/types';
+import { AsyncQueue } from '@sapphire/async-queue';
import ow from 'ow';
import type { SessionOptions } from './session';
@@ -139,6 +140,8 @@ export class SessionPool extends EventEmitter {
protected events: EventManager;
protected readonly blockedStatusCodes: number[];
+ private queue = new AsyncQueue();
+
/**
* @internal
*/
@@ -270,24 +273,31 @@ export class SessionPool extends EventEmitter {
* @param [sessionId] If provided, it returns the usable session with this id, `undefined` otherwise.
*/
async getSession(sessionId?: string): Promise<Session | undefined> {
- this._throwIfNotInitialized();
- if (sessionId) {
- const session = this.sessionMap.get(sessionId);
- if (session && session.isUsable()) return session;
- return undefined;
- }
+ await this.queue.wait();
- if (this._hasSpaceForSession()) {
- return this._createSession();
- }
+ try {
+ this._throwIfNotInitialized();
- const pickedSession = this._pickSession();
- if (pickedSession.isUsable()) {
- return pickedSession;
- }
+ if (sessionId) {
+ const session = this.sessionMap.get(sessionId);
+ if (session && session.isUsable()) return session;
+ return undefined;
+ }
- this._removeRetiredSessions();
- return this._createSession();
+ if (this._hasSpaceForSession()) {
+ return await this._createSession();
+ }
+
+ const pickedSession = this._pickSession();
+ if (pickedSession.isUsable()) {
+ return pickedSession;
+ }
+
+ this._removeRetiredSessions();
+ return await this._createSession();
+ } finally {
+ this.queue.shift();
+ }
}
async resetStore() {
diff --git a/yarn.lock b/yarn.lock
index d14853e1a..a40e1849a 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -506,7 +506,7 @@ __metadata:
"@crawlee/memory-storage": "npm:3.6.2"
"@crawlee/types": "npm:3.6.2"
"@crawlee/utils": "npm:3.6.2"
- "@sapphire/async-queue": "npm:^1.5.0"
+ "@sapphire/async-queue": "npm:^1.5.1"
"@types/tough-cookie": "npm:^4.0.2"
"@vladfrangu/async_event_emitter": "npm:^2.2.2"
csv-stringify: "npm:^6.2.0"
@@ -1777,7 +1777,7 @@ __metadata:
languageName: node
linkType: hard
-"@sapphire/async-queue@npm:^1.5.0":
+"@sapphire/async-queue@npm:^1.5.0, @sapphire/async-queue@npm:^1.5.1":
version: 1.5.1
resolution: "@sapphire/async-queue@npm:1.5.1"
checksum: 176903d9763f93e27455ebb903d68fe8b96ba65eca78a1199ed6bec2260bb2e02c5f469d493aa8c8d796fc9647df22e41e912de73bfe3d4e3d55e4c3adbfb99b