Skip to content

Commit

Permalink
Fix racy drop.
Browse files Browse the repository at this point in the history
  • Loading branch information
dragostis committed Sep 13, 2024
1 parent 2544d73 commit ad55276
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,18 @@ impl<T> Job<T> {

/// It should only be called after being popped from a `JobQueue`.
pub unsafe fn wait(&self) -> Option<thread::Result<T>> {
self.fut_or_next
.get()
// Before being popped, the `JobQueue` allocates and store a
self.fut_or_next.get().and_then(|fut| {
// Before being popped, the `JobQueue` allocates and stores a
// `Future` in `self.fur_or_next` that should get passed here.
.and_then(|fut| unsafe { Box::from_raw(fut.as_ptr()).wait() })
let result = unsafe { (*fut.as_ptr()).wait() };
// We only can drop the `Box` *after* waiting on the `Future`
// in order to ensure unique access.
unsafe {
drop(Box::from_raw(fut.as_ptr()));
}

result
})
}

/// It should only be called in the case where the job has been popped
Expand Down

0 comments on commit ad55276

Please sign in to comment.