Skip to content

Commit

Permalink
feat: propagate sync errors from worker (#101)
Browse files Browse the repository at this point in the history
close #44
close #94
  • Loading branch information
JounQin committed Aug 10, 2022
1 parent 4fe6aef commit 34e44ae
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-mice-peel.md
@@ -0,0 +1,5 @@
---
"synckit": patch
---

feat: propagate sync errors from worker
59 changes: 40 additions & 19 deletions src/index.ts
Expand Up @@ -352,26 +352,47 @@ export function runAsWorker<
}

const { workerPort } = workerData as WorkerData
parentPort!.on(
'message',
({ sharedBuffer, id, args }: MainToWorkerMessage<Parameters<T>>) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
const sharedBufferView = new Int32Array(sharedBuffer)
let msg: WorkerToMainMessage<R>
try {
msg = { id, result: await fn(...args) }
} catch (error: unknown) {
msg = {
id,
error,
properties: extractProperties(error),

try {
parentPort!.on(
'message',
({ sharedBuffer, id, args }: MainToWorkerMessage<Parameters<T>>) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
const sharedBufferView = new Int32Array(sharedBuffer)
let msg: WorkerToMainMessage<R>
try {
msg = { id, result: await fn(...args) }
} catch (error: unknown) {
msg = { id, error, properties: extractProperties(error) }
}
}
workerPort.postMessage(msg)
workerPort.postMessage(msg)
Atomics.add(sharedBufferView, 0, 1)
Atomics.notify(sharedBufferView, 0)
})()
},
)

/**
* @see https://github.com/un-ts/synckit/issues/94
*
* Starting the worker can fail, due to syntax error, for example. In that case
* we just fail all incoming messages with whatever error message we got.
* Otherwise incoming messages will hang forever waiting for a reply.
*/
} catch (error) {
parentPort!.on(
'message',
({ sharedBuffer, id }: MainToWorkerMessage<Parameters<T>>) => {
const sharedBufferView = new Int32Array(sharedBuffer)
workerPort.postMessage({
id,
error,
properties: extractProperties(error),
})
Atomics.add(sharedBufferView, 0, 1)
Atomics.notify(sharedBufferView, 0)
})()
},
)
},
)
}
}

0 comments on commit 34e44ae

Please sign in to comment.