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.

Add explicit termination of puppeteer subprocesses

Ondra Urbanb15749b19940
diff --git a/src/puppeteer_pool.js b/src/puppeteer_pool.js
index bbe51eb0e..be2bb5f51 100644
--- a/src/puppeteer_pool.js
+++ b/src/puppeteer_pool.js
@@ -120,6 +120,9 @@ export default class PuppeteerPool {
this.activeInstances = {};
this.retiredInstances = {};
this.instanceKillerInterval = setInterval(() => this._killRetiredInstances(), instanceKillerIntervalMillis);
+
+ // ensure termination on SIGINT
+ this._terminateInstancesOnSigint();
}
/**
@@ -303,4 +306,21 @@ export default class PuppeteerPool {
.all(closePromises)
.catch(err => log.exception(err, 'PuppeteerPool: cannot close the browsers'));
}
+
+ /**
+ * Ensures all Puppeteer processes terminate on SIGINT and Chromium isn't left running.
+ * @ignore
+ */
+ _terminateInstancesOnSigint() {
+ process.on('SIGINT', () => {
+ const allInstances = Object.values(this.activeInstances).concat(Object.values(this.retiredInstances));
+ allInstances.forEach((instance) => {
+ try {
+ instance.childProcess.kill('SIGINT');
+ } catch (e) {
+ // do nothing, it's dead
+ }
+ });
+ });
+ }
}