Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class WorkerInfo extends AsynchronouslyCreatedResource {
// This would mostly happen if e.g. message contains unserializable data
// or transferList is invalid.
taskInfo.done(err)
return
return false
}

taskInfo.workerInfo = this
Expand All @@ -606,6 +606,7 @@ class WorkerInfo extends AsynchronouslyCreatedResource {
// if it is waiting for one.
Atomics.add(this.sharedBuffer, kRequestCountField, 1)
Atomics.notify(this.sharedBuffer, kRequestCountField, 1)
return true
}

processPendingMessages() {
Expand Down Expand Up @@ -908,9 +909,9 @@ class ThreadPool {
}
const now = performance.now()
taskInfo.started = now
workerInfo.postTask(taskInfo)
const posted = workerInfo.postTask(taskInfo)
this._maybeDrain()
return
if (posted) return
}

if (
Expand Down
26 changes: 26 additions & 0 deletions test/task-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ test('will put items into a task queue until they can run', async () => {
await Promise.all(results)
})

test('continues draining after a queued task cannot be cloned', async () => {
const pool = new Tinypool({
filename: resolve(__dirname, 'fixtures/wait-for-notify.js'),
minThreads: 1,
maxThreads: 1,
})
const firstBuffer = new Int32Array(new SharedArrayBuffer(4))
const lastBuffer = new Int32Array(new SharedArrayBuffer(4))

const firstTask = pool.run(firstBuffer)
const uncloneableTask = pool.run({ uncloneable: () => {} })
const lastTask = pool.run(lastBuffer)

for (const buffer of [firstBuffer, lastBuffer]) {
Atomics.store(buffer, 0, 1)
Atomics.notify(buffer, 0, 1)
}

await expect(firstTask).resolves.toBeUndefined()
await expect(uncloneableTask).rejects.toMatchObject({
name: 'DataCloneError',
})
await expect(lastTask).resolves.toBeUndefined()
expect(pool.queueSize).toBe(0)
})

test('will reject items over task queue limit', async () => {
const pool = new Tinypool({
filename: resolve(__dirname, 'fixtures/eval.js'),
Expand Down