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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
fix(core): release storage open queue slot on failure (#3662)
Jindřich Bär18ebc61b2726
diff --git a/packages/core/src/storages/storage_manager.ts b/packages/core/src/storages/storage_manager.tsindex aea40468c..6e5426c3b 100644--- a/packages/core/src/storages/storage_manager.ts+++ b/packages/core/src/storages/storage_manager.ts@@ -70,32 +70,34 @@ export class StorageManager<T extends IStorage = IStorage> {async openStorage(idOrName?: string | null, client?: StorageClient): Promise<T> {await this.storageOpenQueue.wait();- if (!idOrName) {- const defaultIdConfigKey = DEFAULT_ID_CONFIG_KEYS[this.name];- idOrName = this.config.get(defaultIdConfigKey) as string;- }-- const cacheKey = idOrName;- let storage = this.cache.get(cacheKey);-- if (!storage) {- client ??= this.config.getStorageClient();- const storageObject = await this._getOrCreateStorage(idOrName, this.name, client);- storage = new this.StorageConstructor(- {- id: storageObject.id,- name: storageObject.name,- storageObject,- client,- },- this.config,- );- this._addStorageToCache(storage);- }+ try {+ if (!idOrName) {+ const defaultIdConfigKey = DEFAULT_ID_CONFIG_KEYS[this.name];+ idOrName = this.config.get(defaultIdConfigKey) as string;+ }- this.storageOpenQueue.shift();+ const cacheKey = idOrName;+ let storage = this.cache.get(cacheKey);++ if (!storage) {+ client ??= this.config.getStorageClient();+ const storageObject = await this._getOrCreateStorage(idOrName, this.name, client);+ storage = new this.StorageConstructor(+ {+ id: storageObject.id,+ name: storageObject.name,+ storageObject,+ client,+ },+ this.config,+ );+ this._addStorageToCache(storage);+ }- return storage;+ return storage;+ } finally {+ this.storageOpenQueue.shift();+ }}closeStorage(storage: { id: string; name?: string }): void {diff --git a/test/core/storages/storage_manager.test.ts b/test/core/storages/storage_manager.test.tsnew file mode 100644index 000000000..2682ba61d--- /dev/null+++ b/test/core/storages/storage_manager.test.ts@@ -0,0 +1,37 @@+import { Configuration, Dataset } from '@crawlee/core';++import { MemoryStorageEmulator } from '../../shared/MemoryStorageEmulator';++const localStorageEmulator = new MemoryStorageEmulator();++beforeEach(async () => {+ await localStorageEmulator.init();+});++afterAll(async () => {+ await localStorageEmulator.destroy();+});++describe('StorageManager', () => {+ test('failed openStorage call does not block subsequent calls (#3661)', async () => {+ const goodClient = Configuration.getStorageClient();+ const failingClient = {+ ...goodClient,+ datasets: () => {+ throw new Error('boom');+ },+ dataset: () => {+ throw new Error('boom');+ },+ };++ await expect(Dataset.open('will-fail', { storageClient: failingClient as any })).rejects.toThrow('boom');++ await expect(+ Promise.race([+ Dataset.open('fallback'),+ new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 1000)),+ ]),+ ).resolves.toBeDefined();+ });+});