fix: replace static mut ID_COUNTER with AtomicU64 - #75
Open
Jay-Jay-Tee wants to merge 1 commit into
Open
Conversation
static mut is undefined behavior in Rust regardless of threading. The app also uses Arc<RwLock> and a tokio runtime, so next_id() can be called from multiple threads, making this a data race that could produce duplicate or skipped IDs and silently corrupt clipboard history and vault storage lookups. Replace with AtomicU64::fetch_add which is thread-safe, needs no unsafe block, and has zero runtime overhead. Closes shaaravraghu#62
Contributor
Author
|
@shaaravraghu, do check this out! |
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.
Closes #62
Problem
next_id()insrc/storage/ram.rsuses a rawstatic mutto generate entry IDs. In Rust, any access tostatic mutis undefined behavior per the reference and Miri, even from a single thread. The compiler is free to reorder, cache, or elide reads and writes to it.The app also uses
Arc<RwLock>and a tokio runtime, sonext_id()can be called from multiple threads, making this a data race that could produce duplicate or skipped IDs and silently corrupt clipboard history and vault storage lookups.Fix
Replace with
AtomicU64::fetch_add:No unsafe block needed. Thread-safe. Zero runtime overhead.
Files changed
src/storage/ram.rs