Skip to content

Commit 61b0393

Browse files
jasnelldanielleadams
authored andcommitted
doc: avoid memory leak warning in async_hooks example
Fixes: #35952 PR-URL: #36783 Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent b17130a commit 61b0393

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
@@ -839,9 +839,19 @@ class WorkerPool extends EventEmitter {
839839
this.numThreads = numThreads;
840840
this.workers = [];
841841
this.freeWorkers = [];
842+
this.tasks = [];
842843

843844
for (let i = 0; i < numThreads; i++)
844845
this.addNewWorker();
846+
847+
// Any time the kWorkerFreedEvent is emitted, dispatch
848+
// the next task pending in the queue, if any.
849+
this.on(kWorkerFreedEvent, () => {
850+
if (this.tasks.length > 0) {
851+
const { task, callback } = this.tasks.shift();
852+
this.runTask(task, callback);
853+
}
854+
});
845855
}
846856

847857
addNewWorker() {
@@ -875,7 +885,7 @@ class WorkerPool extends EventEmitter {
875885
runTask(task, callback) {
876886
if (this.freeWorkers.length === 0) {
877887
// No free threads, wait until a worker thread becomes free.
878-
this.once(kWorkerFreedEvent, () => this.runTask(task, callback));
888+
this.tasks.push({ task, callback });
879889
return;
880890
}
881891

0 commit comments

Comments
 (0)