Skip to content

Fix lost events in ThreadAwareScheduler's thread queues - #192

Merged
Simn merged 4 commits into
masterfrom
fix-thread-queue-publication
Oct 29, 2026
Merged

Simn merged 4 commits into
masterfrom
fix-thread-queue-publication

Conversation

@Simn

@Simn Simn commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

This fixes lost events in ThreadAwareScheduler, the scheduler of the default thread pool setup. They made runs hang on the JVM. It also fixes a rarer variant of the same bug in WorkStealingQueue, which the thread pool uses.

The bug

Each thread schedules events, such as yield and delay resumptions, into its own queue, which the loop thread drains. This queue is a TLS value, so only its owner thread writes to it. But the loop thread reads from it, so the queue still has to publish its events to another thread. Its read and write indices were plain fields:

  • Unordered write: the loop thread could see the new write index before the event in the slot. It then read a null or stale slot, still advanced the read index, and the event was gone.
  • Stale storage: after a resize, the loop thread could keep reading the old storage array.

The coroutine waiting for the lost event never resumed. So the run hung, with the loop thread and all pool workers idle and waiting.

The review flagged this as a risk on weak memory models, but we couldn't reproduce it then. While I was benchmarking the scheduler heap (#193), it turned out to be easy on the JVM. A burst of yields from 32 tasks on a thread pool hung within a few rounds:

run 1: HANG after 57 completed rounds
run 2: HANG after 4 completed rounds
run 3: HANG after 46 completed rounds

With this PR, three runs of 200 rounds finished without a hang, in about 8.5 seconds each.

The fix

Atomic indices. The first commit makes the queue's indices AtomicInts. The producer writes the slot and then stores the write index. The consumer loads the write index before it reads the slot. That orders the slot write before the consumer's read.

One queue implementation. With atomic indices, the queue had become the same data structure as WorkStealingQueue. So the second commit replaces it with WorkStealingQueue<ScheduledEvent>.

  • Single consumer: the loop thread is the only consumer, so steal's compare-and-swap on the read index always succeeds, and null means the queue is empty.
  • Cleanup: the unused CircularQueueData class is gone too.

Atomic storage. WorkStealingQueue now also publishes its storage atomically. When the producer resizes, it copies the live slots into a new array and then stores it in storage, which used to be a plain field.

  • The race: a consumer that loaded the write index before the resize can still read storage after it and get the new array. Nothing guaranteed that it also saw the copied slots, so it could read an empty one and lose that element.
  • The fix: storage is now an AtomicObject. Loading the array then also makes the copies visible.
  • Wrapper: the array sits in a small Slots wrapper, because AtomicObject needs a class instance and the storage is a native array on hl.
  • Scope: this needs a resize to coincide with a steal, so it's much rarer than the hang. It affected the thread pool as well as the scheduler.

Test

schedulers.TestThreadAwareScheduler.testYieldBurstsOnThreadPool runs yield bursts on a thread pool:

  • Bounded: the bursts run on a separate thread with a deadline, so a hang fails the test instead of blocking the run.
  • Sizing: it runs 100 rounds on jvm and cpp, and a small number elsewhere to keep the suite fast.
  • Checked on jvm: it fails without the fix and passes with it.
  • Skipped on neko: this is the first test that runs the thread pool on neko, whose default test setup is the trampoline. It failed there because of a neko limitation that's unrelated to the scheduler. Assigning a declared field for the first time adds it to the object, which isn't safe while another thread reads the object. That's tracked in Reading an object's fields while another thread adds fields can return wrong values neko#335.

The storage race can't be reproduced on demand, so it has no dedicated test. All tests pass on eval and hl, and the jvm test and repro pass with both commits.

🤖 Generated with Claude Code

Each thread schedules events into its own queue, which the loop thread
drains. The queue's read and write indices were plain fields, so the
loop thread could see an advanced write index before the event in the
slot, or keep reading a storage array that a resize had replaced. It
then lost the event, and a coroutine waiting for it never resumed.

On the JVM, bursts of yields on a thread pool hung within a few rounds.
The indices are now atomic, so storing the write index after the slot
publishes both to the loop thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Simn and others added 2 commits October 29, 2026 20:50
With atomic indices, the scheduler's CircularQueue had become the same
data structure as WorkStealingQueue, so the scheduler now uses that. The
loop thread is the only consumer, so steal only returns null when the
queue is empty.

WorkStealingQueue now also publishes its storage atomically. A consumer
that loaded the write index before a resize could still read the new
storage afterwards through a plain field, without seeing the slots that
were copied into it. It would then take an empty slot and lose that
element. This affected both the thread pool and the scheduler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On hxcpp, AtomicObject.load returns an untyped hx::Object, so accessing
a field directly on its result generated a member access on hx::Object,
which doesn't compile. The slots are now loaded through a non-inline
function with a declared return type, which makes hxcpp convert the
object first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On neko, assigning a declared field for the first time adds it to the
object, which is not safe while another thread reads the object. The
thread pool shares continuations between threads like that, so the test
failed there for reasons unrelated to the scheduler. This is tracked in
HaxeFoundation/neko#335.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment on lines +53 to +60
/**
On hxcpp, `AtomicObject.load` returns an untyped `hx::Object`, so a field access directly
on its result fails to compile. The declared return type makes hxcpp convert it. This is
not inline, so the conversion can't be optimized away.
**/
function loadHolder():Slots<T> {
return storage.load();
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll review this again once we've dealt with HaxeFoundation/haxe#13095 properly.

@Simn
Simn merged commit 9857567 into master Oct 29, 2026
54 checks passed
@Simn
Simn deleted the fix-thread-queue-publication branch October 29, 2026 21:18
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.

1 participant