Skip to content

Commit e436d2b

Browse files
committed
Nip some potentially unsound behavior in the bud
1 parent e93561c commit e436d2b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a> Executor<'a> {
231231
let mut rng = fastrand::Rng::new();
232232

233233
// Set the local queue while we're running.
234-
LocalQueue::set(&runner.local, {
234+
LocalQueue::set(self.state(), &runner.local, {
235235
let runner = &runner;
236236
async move {
237237
// A future that runs tasks forever.
@@ -262,6 +262,11 @@ impl<'a> Executor<'a> {
262262

263263
// Try to push into the local queue.
264264
LocalQueue::with(|local_queue| {
265+
// Make sure that we don't accidentally push to an executor that isn't ours.
266+
if !std::ptr::eq(local_queue.state, &*state) {
267+
return;
268+
}
269+
265270
if let Err(e) = local_queue.queue.push(runnable.take().unwrap()) {
266271
runnable = Some(e.into_inner());
267272
return;
@@ -844,6 +849,11 @@ impl Drop for Runner<'_> {
844849

845850
/// The state of the currently running local queue.
846851
struct LocalQueue {
852+
/// The pointer to the state of the executor.
853+
///
854+
/// Used to make sure we don't push runnables to the wrong executor.
855+
state: *const State,
856+
847857
/// The concurrent queue.
848858
queue: Arc<ConcurrentQueue<Runnable>>,
849859

@@ -861,14 +871,19 @@ impl LocalQueue {
861871

862872
impl LocalQueue {
863873
/// Run a function with a set local queue.
864-
async fn set<F>(queue: &Arc<ConcurrentQueue<Runnable>>, fut: F) -> F::Output
874+
async fn set<F>(
875+
state: &State,
876+
queue: &Arc<ConcurrentQueue<Runnable>>,
877+
fut: F,
878+
) -> F::Output
865879
where
866880
F: Future,
867881
{
868882
// Store the local queue and the current waker.
869883
let mut old = with_waker(|waker| {
870884
LOCAL_QUEUE.with(move |slot| {
871885
slot.borrow_mut().replace(LocalQueue {
886+
state: state as *const State,
872887
queue: queue.clone(),
873888
waker: waker.clone(),
874889
})

0 commit comments

Comments
 (0)