Skip to content

fix(thread_aware): Move source registration up so we prevent race condition#404

Closed
Darksecond wants to merge 1 commit intomainfrom
u/timmypeters/bug-7312281
Closed

fix(thread_aware): Move source registration up so we prevent race condition#404
Darksecond wants to merge 1 commit intomainfrom
u/timmypeters/bug-7312281

Conversation

@Darksecond
Copy link
Copy Markdown
Contributor

This prevents a race condition when source equals destination.

Copilot AI review requested due to automatic review settings May 6, 2026 13:15
@Darksecond Darksecond changed the title Move source registration up so we prevent race condition bug: Move source registration up so we prevent race condition May 6, 2026
@Darksecond Darksecond changed the title bug: Move source registration up so we prevent race condition fix: Move source registration up so we prevent race condition May 6, 2026
@Darksecond Darksecond changed the title fix: Move source registration up so we prevent race condition fix(thread_aware): Move source registration up so we prevent race condition May 6, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
@Darksecond Darksecond closed this May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants