Skip to content

Commit 4a5c750

Browse files
authored
fix(runtime): use Weak to break reference cycle (#490)
1 parent b183c74 commit 4a5c750

File tree

1 file changed

+12
-19
lines changed
  • compio-runtime/src/runtime

1 file changed

+12
-19
lines changed

compio-runtime/src/runtime/mod.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,19 @@ impl Runtime {
202202
///
203203
/// The caller should ensure the captured lifetime long enough.
204204
pub unsafe fn spawn_unchecked<F: Future>(&self, future: F) -> Task<F::Output> {
205-
let runnables = self.runnables.clone();
206-
let handle = self.driver.borrow().handle();
207-
let schedule = move |runnable| {
208-
runnables.schedule(runnable, &handle);
205+
let schedule = {
206+
// Use `Weak` to break reference cycle.
207+
// `RunnableQueue` -> `Runnable` -> `RunnableQueue`
208+
let runnables = Arc::downgrade(&self.runnables);
209+
let handle = self.driver.borrow().handle();
210+
211+
move |runnable| {
212+
if let Some(runnables) = runnables.upgrade() {
213+
runnables.schedule(runnable, &handle);
214+
}
215+
}
209216
};
217+
210218
let (runnable, task) = async_task::spawn_unchecked(future, schedule);
211219
runnable.schedule();
212220
task
@@ -418,21 +426,6 @@ impl Runtime {
418426
}
419427
}
420428

421-
impl Drop for Runtime {
422-
fn drop(&mut self) {
423-
self.enter(|| {
424-
while self.runnables.sync_runnables.pop().is_some() {}
425-
let local_runnables = unsafe { self.runnables.local_runnables.get_unchecked() };
426-
loop {
427-
let runnable = local_runnables.borrow_mut().pop_front();
428-
if runnable.is_none() {
429-
break;
430-
}
431-
}
432-
})
433-
}
434-
}
435-
436429
impl AsRawFd for Runtime {
437430
fn as_raw_fd(&self) -> RawFd {
438431
self.driver.borrow().as_raw_fd()

0 commit comments

Comments
 (0)