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
Fix leak in LocalSet #3978
Merged
Merged
Fix leak in LocalSet #3978
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::runtime::tests::loom_oneshot as oneshot; | ||
use crate::runtime::Builder; | ||
use crate::task::LocalSet; | ||
|
||
use std::task::Poll; | ||
|
||
/// Waking a runtime will attempt to push a task into a queue of notifications | ||
/// in the runtime, however the tasks in such a queue usually have a reference | ||
/// to the runtime itself. This means that if they are not properly removed at | ||
/// runtime shutdown, this will cause a memory leak. | ||
/// | ||
/// This test verifies that waking something during shutdown of a LocalSet does | ||
/// not result in tasks lingering in the queue once shutdown is complete. This | ||
/// is verified using loom's leak finder. | ||
#[test] | ||
fn wake_during_shutdown() { | ||
loom::model(|| { | ||
let rt = Builder::new_current_thread().build().unwrap(); | ||
let ls = LocalSet::new(); | ||
|
||
let (send, recv) = oneshot::channel(); | ||
|
||
ls.spawn_local(async move { | ||
let mut send = Some(send); | ||
|
||
let () = futures::future::poll_fn(|cx| { | ||
if let Some(send) = send.take() { | ||
send.send(cx.waker().clone()); | ||
} | ||
|
||
Poll::Pending | ||
}) | ||
.await; | ||
}); | ||
|
||
let handle = loom::thread::spawn(move || { | ||
let waker = recv.recv(); | ||
waker.wake(); | ||
}); | ||
|
||
ls.block_on(&rt, crate::task::yield_now()); | ||
|
||
drop(ls); | ||
handle.join().unwrap(); | ||
drop(rt); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,7 +241,7 @@ struct Tasks { | |
/// LocalSet state shared between threads. | ||
struct Shared { | ||
/// Remote run queue sender | ||
queue: Mutex<VecDeque<task::Notified<Arc<Shared>>>>, | ||
queue: Mutex<Option<VecDeque<task::Notified<Arc<Shared>>>>>, | ||
|
||
/// Wake the `LocalSet` task | ||
waker: AtomicWaker, | ||
|
@@ -339,7 +339,7 @@ impl LocalSet { | |
queue: VecDeque::with_capacity(INITIAL_CAPACITY), | ||
}), | ||
shared: Arc::new(Shared { | ||
queue: Mutex::new(VecDeque::with_capacity(INITIAL_CAPACITY)), | ||
queue: Mutex::new(Some(VecDeque::with_capacity(INITIAL_CAPACITY))), | ||
waker: AtomicWaker::new(), | ||
}), | ||
}, | ||
|
@@ -549,15 +549,23 @@ impl LocalSet { | |
.shared | ||
.queue | ||
.lock() | ||
.pop_front() | ||
.as_mut() | ||
.and_then(|queue| queue.pop_front()) | ||
.or_else(|| self.context.tasks.borrow_mut().queue.pop_front()) | ||
} else { | ||
self.context | ||
.tasks | ||
.borrow_mut() | ||
.queue | ||
.pop_front() | ||
.or_else(|| self.context.shared.queue.lock().pop_front()) | ||
.or_else(|| { | ||
self.context | ||
.shared | ||
.queue | ||
.lock() | ||
.as_mut() | ||
.and_then(|queue| queue.pop_front()) | ||
Comment on lines
+564
to
+567
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. TIOLI: it seems like |
||
}) | ||
} | ||
} | ||
|
||
|
@@ -627,7 +635,10 @@ impl Drop for LocalSet { | |
task.shutdown(); | ||
} | ||
|
||
for task in self.context.shared.queue.lock().drain(..) { | ||
// Take the queue from the Shared object to prevent pushing | ||
// notifications to it in the future. | ||
let queue = self.context.shared.queue.lock().take().unwrap(); | ||
for task in queue { | ||
task.shutdown(); | ||
} | ||
|
||
|
@@ -677,8 +688,16 @@ impl Shared { | |
cx.tasks.borrow_mut().queue.push_back(task); | ||
} | ||
_ => { | ||
self.queue.lock().push_back(task); | ||
self.waker.wake(); | ||
// First check whether the queue is still there (if not, the | ||
// LocalSet is dropped). Then push to it if so, and if not, | ||
// do nothing. | ||
let mut lock = self.queue.lock(); | ||
|
||
if let Some(queue) = lock.as_mut() { | ||
queue.push_back(task); | ||
drop(lock); | ||
self.waker.wake(); | ||
} | ||
} | ||
}); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice to have a comment explaining what the failure modes of this test are, for future reference? not a blocker though.