Fix crash: ConcurrentQueue mutates without a barrier - #38
Open
Kevin-4Spaces wants to merge 1 commit into
Open
Conversation
`ConcurrentQueue` runs on a concurrent DispatchQueue, so a plain `sync`
grants shared (reader) access, not exclusive access. `enqueue` correctly
uses `async(flags: .barrier)`, but both `dequeue()` and `dequeue(count:)`
MUTATE `elements` inside a plain `sync` — so two callers can each read a
non-empty array and then `removeFirst()` past each other's end.
In production this crashes as:
Swift/Array.swift:1845: Fatal error: Array replace: subrange extends past the end
reached from `EventDispatcher.flush()` → `ConcurrentQueue.dequeue(count:)`.
`flush()` is driven by a repeating timer and also by app lifecycle events,
so overlapping flushes happen on real devices.
Fix: give both dequeues `sync(flags: .barrier)`. The read-only `isEmpty`
and `count` keep the plain `sync` — concurrent reads are the point of the
concurrent queue.
Adds two regression tests that drain the queue from 8 concurrent workers
and assert every element comes out exactly once. Both crash with SIGSEGV
against the unpatched implementation and pass with the barrier.
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.
The crash
reached from
AptabaseClienttimer flush →EventDispatcher.flush()→ConcurrentQueue.dequeue(count:).Cause
ConcurrentQueue'squeueis created withattributes: .concurrent, so a plainsyncgrants shared access — it does not serialize.enqueue(_:)andenqueue(contentsOf:)correctly useasync(flags: .barrier), butdequeue()anddequeue(count:)mutateelementsinside a plainsync:Two concurrent callers each evaluate
self.elements.count, then bothremoveFirst(), and the second removes past the end.This is reachable in normal use:
flush()runs from the repeating timer and from app lifecycle handlers, so two flushes can overlap on a real device.Fix
Give both dequeues
sync(flags: .barrier).isEmptyandcountkeep the plainsync— concurrent reads are the reason for the concurrent queue.Tests
Two regression tests drain a 2,000-element queue from 8 concurrent workers and assert every element is dequeued exactly once, covering both
dequeue()anddequeue(count:).Against the unpatched implementation both crash:
With the barrier, the full suite passes (11 tests, 0 failures).