8000 fix: unexpected null pointer exception due to race condition by JounQin · Pull Request #245 · un-ts/synckit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: unexpected null pointer exception due to race condition #245

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

Merged
merged 1 commit into from
May 30, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-kids-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"synckit": patch
---

fix: unexpected null pointer exception due to race condition
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"detail": true,
"ignoreAsAssertion": true,
"ignoreFiles": [
"**/*.d.ts"
"lib"
],
"ignoreNonNullAssertion": true,
"showRelativePath": true,
Expand Down
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,13 @@ function startWorkerThread<T extends AnyFn, R = Awaited<ReturnType<T>>>(
throw new Error('Internal error: Atomics.wait() failed: ' + status)
}

const { id, ...message } = (
receiveMessageOnPort(mainPort) as { message: WorkerToMainMessage<R> }
).message
const result = receiveMessageOnPort(mainPort) as
| { message: WorkerToMainMessage<R> }
| undefined

if (id < expectedId) {
const msg = result?.message

if (msg?.id == null || msg.id < expectedId) {
const waitingTime = Date.now() - start
return receiveMessageWithId(
port,
Expand All @@ -670,6 +672,8 @@ function startWorkerThread<T extends AnyFn, R = Awaited<ReturnType<T>>>(
)
}

const { id, ...message } = msg

if (expectedId !== id) {
throw new Error(
`Internal error: Expected id ${expectedId} but got id ${id}`,
Expand Down
22 changes: 22 additions & 0 deletions test/reliability.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import path from 'node:path'

import { _dirname, testIf } from './helpers.js'

const times = 1e6

const test = testIf(!!process.env.CI)

test(`Reliability (${times} runs)`, async () => {
const { createSyncFn } = await import('synckit')
const identity = createSyncFn(path.resolve(_dirname, `./worker-identity.js`))

for (let index = 0; index < times; index++) {
try {
// eslint-disable-next-line jest/no-standalone-expect
expect(identity(index)).toBe(index)
} catch (error) {
console.error(`Failed on ${index + 1}/${times} run.`)
throw error
}
}
})
3 changes: 3 additions & 0 deletions test/worker-identity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { runAsWorker } from 'synckit'

runAsWorker(value => value)
Loading
0