diff --git a/src/job.rs b/src/job.rs index b61309c..e8ce551 100644 --- a/src/job.rs +++ b/src/job.rs @@ -190,11 +190,18 @@ impl Job { /// It should only be called after being popped from a `JobQueue`. pub unsafe fn wait(&self) -> Option> { - 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