From e973bdc14e5aebf811277b2913e416491dee9133 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 4 Jan 2021 11:14:15 -0800 Subject: [PATCH] doc: avoid memory leak warning in async_hooks example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/35952 PR-URL: https://github.com/nodejs/node/pull/36783 Reviewed-By: Gerhard Stöbich Reviewed-By: Juan José Arboleda --- doc/api/async_hooks.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 99b8f038ddf211..6abc66bfb8d815 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -833,9 +833,19 @@ class WorkerPool extends EventEmitter { this.numThreads = numThreads; this.workers = []; this.freeWorkers = []; + this.tasks = []; for (let i = 0; i < numThreads; i++) this.addNewWorker(); + + // Any time the kWorkerFreedEvent is emitted, dispatch + // the next task pending in the queue, if any. + this.on(kWorkerFreedEvent, () => { + if (this.tasks.length > 0) { + const { task, callback } = this.tasks.shift(); + this.runTask(task, callback); + } + }); } addNewWorker() { @@ -869,7 +879,7 @@ class WorkerPool extends EventEmitter { runTask(task, callback) { if (this.freeWorkers.length === 0) { // No free threads, wait until a worker thread becomes free. - this.once(kWorkerFreedEvent, () => this.runTask(task, callback)); + this.tasks.push({ task, callback }); return; }