Skip to content

Runtime: fix lost wakeup from late-arriving atomicWaitBroken wait notify#107

Merged
andreaTP merged 2 commits into
bytecodealliance:mainfrom
andreas-karlsson:wait-notify-order
Jul 20, 2026
Merged

Runtime: fix lost wakeup from late-arriving atomicWaitBroken wait notify#107
andreaTP merged 2 commits into
bytecodealliance:mainfrom
andreas-karlsson:wait-notify-order

Conversation

@andreas-karlsson

Copy link
Copy Markdown
Contributor

Fixes #106

A pending wakeup left behind by atomicNotify could be stolen by an atomicWait call that arrives after the notify, letting it return without ever waiting — while the thread the wakeup was actually meant for gets no signal and can sleep forever (lost wakeup / deadlock).

Fixes ByteArrayMemory/ByteBufferMemory by tagging each WaitState with a generation counter, bumped on every notify. A waiter may only consume a pending wakeup minted since it registered, so late arrivals wait for the next notify instead of stealing the current one.

Split into two commits for review: a regression test that reproduces the steal (fails on main), then the fix. Also adds a stress test that drives a rw-lock built on atomicWait/atomicNotify under contention, to catch this class of bug more broadly.

Comment on lines +119 to +122
for (Thread t : threads) {
final long remaining = Long.max(10, joinDeadline - System.currentTimeMillis());
t.join(remaining);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's actually no point in waiting for these threads to join. I could replace this loop with just a sleep until deadline + some leeway. We still expect no thread to be alive.

@andreaTP andreaTP left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR as usual @andreas-karlsson , thanks for the help!

@andreaTP
andreaTP merged commit b6f29f9 into bytecodealliance:main Jul 20, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

atomicWait can steal a wakeup meant for an already-parked thread

2 participants