-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
net: Avoid race during io driver shutdown #2997
Closed
zaharidichev
wants to merge
2
commits into
tokio-rs:master
from
zaharidichev:zd/avoid-race-in-driver-shutdown
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -10,8 +10,10 @@ use crate::park::{Park, Unpark}; | |
use crate::util::bit; | ||
use crate::util::slab::{self, Slab}; | ||
|
||
use crate::loom::sync::atomic::AtomicUsize; | ||
use std::fmt; | ||
use std::io; | ||
use std::sync::atomic::Ordering::*; | ||
use std::sync::{Arc, Weak}; | ||
use std::time::Duration; | ||
|
||
|
@@ -32,7 +34,7 @@ pub(crate) struct Driver { | |
poll: mio::Poll, | ||
|
||
/// State shared between the reactor and the handles. | ||
inner: Arc<Inner>, | ||
inner: Option<Arc<Inner>>, | ||
} | ||
|
||
/// A reference to an I/O driver | ||
|
@@ -55,8 +57,19 @@ pub(super) struct Inner { | |
|
||
/// Used to wake up the reactor from a call to `turn` | ||
waker: mio::Waker, | ||
|
||
state: AtomicUsize, | ||
} | ||
|
||
/// Idle state | ||
const WAITING: usize = 0; | ||
|
||
/// An IO resource is being added | ||
const REGISTERING: usize = 1; | ||
|
||
/// Closed state | ||
const CLOSED: usize = 2; | ||
|
||
#[derive(Debug, Eq, PartialEq, Clone, Copy)] | ||
pub(super) enum Direction { | ||
Read, | ||
|
@@ -106,11 +119,12 @@ impl Driver { | |
events: Some(mio::Events::with_capacity(1024)), | ||
resources: slab, | ||
poll, | ||
inner: Arc::new(Inner { | ||
inner: Some(Arc::new(Inner { | ||
registry, | ||
io_dispatch: allocator, | ||
waker, | ||
}), | ||
state: AtomicUsize::new(WAITING), | ||
})), | ||
}) | ||
} | ||
|
||
|
@@ -122,7 +136,7 @@ impl Driver { | |
/// to bind them to this event loop. | ||
pub(crate) fn handle(&self) -> Handle { | ||
Handle { | ||
inner: Arc::downgrade(&self.inner), | ||
inner: Arc::downgrade(self.inner.as_ref().expect("reactor closed")), | ||
} | ||
} | ||
|
||
|
@@ -181,12 +195,24 @@ impl Driver { | |
|
||
impl Drop for Driver { | ||
fn drop(&mut self) { | ||
self.resources.for_each(|io| { | ||
// If a task is waiting on the I/O resource, notify it. The task | ||
// will then attempt to use the I/O resource and fail due to the | ||
// driver being shutdown. | ||
io.wake(Ready::ALL); | ||
}) | ||
let inner = self.inner.take().expect("reactor closed"); | ||
loop { | ||
if inner | ||
.state | ||
.compare_exchange(WAITING, CLOSED, AcqRel, Acquire) | ||
.is_ok() | ||
{ | ||
drop(inner); | ||
// we drop the arc first | ||
self.resources.for_each(|io| { | ||
// If a task is waiting on the I/O resource, notify it. The task | ||
// will then attempt to use the I/O resource and fail due to the | ||
// driver being shutdown. | ||
io.wake(Ready::ALL); | ||
}); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -292,6 +318,17 @@ impl Inner { | |
source: &mut impl mio::event::Source, | ||
interest: mio::Interest, | ||
) -> io::Result<slab::Ref<ScheduledIo>> { | ||
loop { | ||
match self | ||
.state | ||
.compare_exchange(WAITING, REGISTERING, AcqRel, Acquire) | ||
{ | ||
Ok(_) | Err(REGISTERING) => break, | ||
Err(CLOSED) => return Err(io::Error::new(io::ErrorKind::Other, "reactor gone")), | ||
_ => unreachable!(), | ||
} | ||
Comment on lines
+321
to
+329
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. I have an uneasy feeling about this. Is it ok to spin like that here? Is there a better way to do it? |
||
} | ||
|
||
let (address, shared) = self.io_dispatch.allocate().ok_or_else(|| { | ||
io::Error::new( | ||
io::ErrorKind::Other, | ||
|
@@ -303,7 +340,7 @@ impl Inner { | |
|
||
self.registry | ||
.register(source, mio::Token(token), interest)?; | ||
|
||
self.state.store(WAITING, Release); | ||
Ok(shared) | ||
} | ||
|
||
|
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.
Generally spinning is not a great strategy as it will mess w/ schedulers.