Memory corruption in glommio::channels::spsc_queue under concurrent use #701
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.
Description
I’m seeing a heap corruption crash when using
glommio::channels::spsc_queuefrom multiple producer and consumer threads.The code only uses safe Rust and the public
glommio::channels::spsc_queueAPI, but at runtime it fails with:This looks like memory corruption / unsoundness in safe code.
I realize this is an SPSC queue and that my sample intentionally uses multiple producers and multiple consumers, but since the API is fully safe, it shouldn’t be possible to trigger heap corruption (ideally the type system or runtime checks would prevent this usage, or at worst it should fail cleanly without UB).
Environment
0.9.0(from github)rustc 1.xx.x (stable)cargo run --releaseandcargo runboth affectedMinimal reproducible example
Running this with
cargo runproduces:Expected behavior
Even if using
spsc_queuein a way that violates its logical “single producer / single consumer” design, the use of safe APIs should not result in undefined behavior or heap corruption.Ideally one of the following would happen instead:
Producer/Consumernot beingClone, or being more constrained), orActual behavior
Program prints
Avvio demo Glommio Multi-Thread...Then glibc reports:
This suggests memory corruption happening internally, triggered only through safe
Producer::try_pushandConsumer::try_popcalls.Additional notes
Producer<T>andConsumer<T>areSendbut!Sync, and they areClone. This allows creating multiple cloned producers/consumers that operate concurrently on the same underlying queue from different threads.spsc_queuesuggests single-producer/single-consumer behavior, but the current API surface (especiallyClone+Send) makes it easy to use it in multi-producer/multi-consumer scenarios without any compiler errors or warnings.If this usage is explicitly unsupported and considered undefined behavior, it might be helpful to:
spsc_queue::make,Producer, andConsumer, and/orCloneor add runtime checks) so that this pattern doesn’t compile or at least doesn’t lead to memory corruption.