-
Notifications
You must be signed in to change notification settings - Fork 72
fix(runtime): use Weak to break reference cycle
#490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| } | ||
Berrysoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| let (runnable, task) = async_task::spawn_unchecked(future, schedule); | ||
| runnable.schedule(); | ||
| task | ||
|
|
@@ -418,21 +426,6 @@ impl Runtime { | |
| } | ||
| } | ||
|
|
||
| impl Drop for Runtime { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I recently discovered that if a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.