Skip to content

Commit

Permalink
doc: avoid memory leak warning in async_hooks example
Browse files Browse the repository at this point in the history
Fixes: #35952

PR-URL: #36783
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
jasnell authored and danielleadams committed Jan 12, 2021
1 parent b17130a commit 61b0393
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion doc/api/async_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -839,9 +839,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() {
Expand Down Expand Up @@ -875,7 +885,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;
}

Expand Down

0 comments on commit 61b0393

Please sign in to comment.