Skip to content

Commit

Permalink
fix: close pool with minThreads=0 (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup authored Jun 4, 2024
1 parent 285aa82 commit 776bacb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,12 @@ class ThreadPool {

const onPoolFlushed = () => new Promise<void>((resolve) => {
const numberOfWorkers = this.workers.size;

if (numberOfWorkers === 0) {
resolve();
return;
}

let numberOfWorkersDone = 0;

const checkIfWorkerIsDone = (workerInfo: WorkerInfo) => {
Expand Down
6 changes: 6 additions & 0 deletions test/pool-close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ test('close()', async (t) => {
t.pass('pool closed successfully');
});

t.test('no pending tasks (with minThreads=0)', async (t) => {
const pool = new Piscina({ filename: resolve(__dirname, 'fixtures/sleep.js'), minThreads: 0 });
await pool.close();
t.pass('pool closed successfully');
});

t.test('queued tasks waits for all tasks to complete', async (t) => {
const pool = new Piscina({ filename: resolve(__dirname, 'fixtures/sleep.js'), maxThreads: 1 });

Expand Down

0 comments on commit 776bacb

Please sign in to comment.