fix(thread_aware): Move source registration up so we prevent race condition#404
Closed
Darksecond wants to merge 1 commit intomainfrom
Closed
fix(thread_aware): Move source registration up so we prevent race condition#404Darksecond wants to merge 1 commit intomainfrom
Darksecond wants to merge 1 commit intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts thread_aware::cell::Arc::relocated to register the source slot earlier so that relocations where the source and destination resolve to the same storage slot don’t trigger repeated factory execution, and adds a regression test covering that scenario.
Changes:
- Pre-populate the source storage slot (when pinned and currently empty) before checking/creating the destination value in
Arc::relocated. - Remove the previous “restore source slot after destination creation” logic.
- Add a unit test asserting the factory runs only once when relocating with
source == destination.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| crates/thread_aware/src/cell/mod.rs | Moves/changes source-slot registration logic inside Arc::relocated, impacting same-slot relocation behavior. |
| crates/thread_aware/src/cell/tests.rs | Adds a regression test for factory invocation count when source == destination. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+493
to
+497
| if let MemoryAffinity::Pinned(source) = source { | ||
| if guard.get_clone(source).is_none() { | ||
| guard.replace(source, sync::Arc::clone(&self.value)); | ||
| } | ||
| } |
Comment on lines
+494
to
+497
| if guard.get_clone(source).is_none() { | ||
| guard.replace(source, sync::Arc::clone(&self.value)); | ||
| } | ||
| } |
Comment on lines
+492
to
+509
| #[test] | ||
| fn factory_runs_once_per_affinity_when_source_eq_destination() { | ||
| use std::sync::atomic::{AtomicUsize, Ordering}; | ||
|
|
||
| static CALLS: AtomicUsize = AtomicUsize::new(0); | ||
| let arc = PerCore::new(|| { | ||
| CALLS.fetch_add(1, Ordering::Relaxed); | ||
| 0u32 | ||
| }); | ||
| assert_eq!(CALLS.load(Ordering::Relaxed), 1); | ||
|
|
||
| let here = pinned_affinities(&[1])[0]; | ||
| let _ = arc.clone().relocated(here.into(), here); | ||
| let _ = arc.clone().relocated(here.into(), here); | ||
|
|
||
| // Without the fix this is 3 (factory re-runs each time source slot is empty | ||
| // at destination-lookup time). | ||
| assert_eq!(CALLS.load(Ordering::Relaxed), 1); |
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 prevents a race condition when source equals destination.