Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions compio-runtime/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,19 @@ impl Runtime {
///
/// The caller should ensure the captured lifetime long enough.
pub unsafe fn spawn_unchecked<F: Future>(&self, future: F) -> Task<F::Output> {
let runnables = self.runnables.clone();
let handle = self.driver.borrow().handle();
let schedule = move |runnable| {
runnables.schedule(runnable, &handle);
let schedule = {
// Use `Weak` to break reference cycle.
// `RunnableQueue` -> `Runnable` -> `RunnableQueue`
let runnables = Arc::downgrade(&self.runnables);
let handle = self.driver.borrow().handle();

move |runnable| {
if let Some(runnables) = runnables.upgrade() {
runnables.schedule(runnable, &handle);
}
}
};

let (runnable, task) = async_task::spawn_unchecked(future, schedule);
runnable.schedule();
task
Expand Down Expand Up @@ -418,21 +426,6 @@ impl Runtime {
}
}

impl Drop for Runtime {
Copy link
Member

Choose a reason for hiding this comment

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

Oh, no. This change may make the runtime panic on drop. The runnables should be dropped in self.enter, or they cannot find the right runtime to cancel the time futures.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, I recently discovered that if a Waker is dropped in another thread and the Weak upgrade fails inside schedule, the Runnable end up being dropped in that other thread. I’m currently looking into how async-executor implements this.

Copy link
Member

Choose a reason for hiding this comment

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

We might want to forget the runnable in that case...

fn drop(&mut self) {
self.enter(|| {
while self.runnables.sync_runnables.pop().is_some() {}
let local_runnables = unsafe { self.runnables.local_runnables.get_unchecked() };
loop {
let runnable = local_runnables.borrow_mut().pop_front();
if runnable.is_none() {
break;
}
}
})
}
}

impl AsRawFd for Runtime {
fn as_raw_fd(&self) -> RawFd {
self.driver.borrow().as_raw_fd()
Expand Down
Loading