Skip to content

Commit e973bdc

Browse files
jasnelltargos
authored andcommittedMay 1, 2021
doc: avoid memory leak warning in async_hooks example
Fixes: #35952 PR-URL: #36783 Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent 70cbe4a commit e973bdc

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed
 

‎doc/api/async_hooks.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,19 @@ class WorkerPool extends EventEmitter {
833833
this.numThreads = numThreads;
834834
this.workers = [];
835835
this.freeWorkers = [];
836+
this.tasks = [];
836837

837838
for (let i = 0; i < numThreads; i++)
838839
this.addNewWorker();
840+
841+
// Any time the kWorkerFreedEvent is emitted, dispatch
842+
// the next task pending in the queue, if any.
843+
this.on(kWorkerFreedEvent, () => {
844+
if (this.tasks.length > 0) {
845+
const { task, callback } = this.tasks.shift();
846+
this.runTask(task, callback);
847+
}
848+
});
839849
}
840850

841851
addNewWorker() {
@@ -869,7 +879,7 @@ class WorkerPool extends EventEmitter {
869879
runTask(task, callback) {
870880
if (this.freeWorkers.length === 0) {
871881
// No free threads, wait until a worker thread becomes free.
872-
this.once(kWorkerFreedEvent, () => this.runTask(task, callback));
882+
this.tasks.push({ task, callback });
873883
return;
874884
}
875885

0 commit comments

Comments
 (0)