Repository navigation
Fix lost events in ThreadAwareScheduler's thread queues - #192
Merged
Merged
Conversation
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>
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>
This was referenced Oct 29, 2026
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>
Simn
commented
Oct 29, 2026
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(); | ||
| } |
Member
Author
There was a problem hiding this comment.
I'll review this again once we've dealt with HaxeFoundation/haxe#13095 properly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 inWorkStealingQueue, which the thread pool uses.The bug
Each thread schedules events, such as
yieldanddelayresumptions, 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: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: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 withWorkStealingQueue<ScheduledEvent>.steal's compare-and-swap on the read index always succeeds, andnullmeans the queue is empty.CircularQueueDataclass is gone too.Atomic storage.
WorkStealingQueuenow also publishes its storage atomically. When the producer resizes, it copies the live slots into a new array and then stores it instorage, which used to be a plain field.storageafter 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.storageis now anAtomicObject. Loading the array then also makes the copies visible.Slotswrapper, becauseAtomicObjectneeds a class instance and the storage is a native array on hl.Test
schedulers.TestThreadAwareScheduler.testYieldBurstsOnThreadPoolrunsyieldbursts on a thread pool: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