Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: propagate sync errors from worker #101

Merged
merged 3 commits into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
})()
},
)
},
)
}
}