Skip to content

Commit

Permalink
Merge pull request #17 from vorner/mio-not-blocking
Browse files Browse the repository at this point in the history
Bugfix: Mio not blocking
  • Loading branch information
vorner committed Jul 13, 2019
2 parents efd9dff + b4fd16c commit 5cd4bc0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Expand Up @@ -14,7 +14,7 @@ travis-ci = { repository = "vorner/signal-hook" }
maintenance = { status = "actively-developed" }

[features]
mio-support = ["mio", "mio-uds"]
mio-support = ["mio"]
tokio-support = ["futures", "mio-support", "tokio-reactor"]

[workspace]
Expand All @@ -27,7 +27,6 @@ members = [
libc = "~0.2"
futures = { version = "~0.1", optional = true }
mio = { version = "~0.6", optional = true }
mio-uds = { version = "~0.6", optional = true }
signal-hook-registry = { version = "~1", path = "signal-hook-registry" }
tokio-reactor = { version = "~0.1", optional = true }

Expand All @@ -36,4 +35,4 @@ version-sync = "~0.8"
tokio = "~0.1"

[package.metadata.docs.rs]
features = ["mio-support", "tokio-support"]
all-features = true
11 changes: 5 additions & 6 deletions src/iterator.rs
Expand Up @@ -53,15 +53,12 @@ use std::borrow::Borrow;
use std::io::Error;
use std::iter::Enumerate;
use std::os::unix::io::AsRawFd;
#[cfg(not(feature = "mio-support"))]
use std::os::unix::net::UnixStream;
use std::slice::Iter;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use libc::{self, c_int};
#[cfg(feature = "mio-support")]
use mio_uds::UnixStream;

use pipe;
use SigId;
Expand Down Expand Up @@ -437,8 +434,10 @@ impl<'a> Iterator for Forever<'a> {
#[cfg(feature = "mio-support")]
mod mio_support {
use std::io::Error;
use std::os::unix::io::AsRawFd;

use mio::event::Evented;
use mio::unix::EventedFd;
use mio::{Poll, PollOpt, Ready, Token};

use super::Signals;
Expand All @@ -451,7 +450,7 @@ mod mio_support {
interest: Ready,
opts: PollOpt,
) -> Result<(), Error> {
self.waker.read.register(poll, token, interest, opts)
EventedFd(&self.waker.read.as_raw_fd()).register(poll, token, interest, opts)
}

fn reregister(
Expand All @@ -461,11 +460,11 @@ mod mio_support {
interest: Ready,
opts: PollOpt,
) -> Result<(), Error> {
self.waker.read.reregister(poll, token, interest, opts)
EventedFd(&self.waker.read.as_raw_fd()).reregister(poll, token, interest, opts)
}

fn deregister(&self, poll: &Poll) -> Result<(), Error> {
self.waker.read.deregister(poll)
EventedFd(&self.waker.read.as_raw_fd()).deregister(poll)
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Expand Up @@ -125,8 +125,6 @@ extern crate futures;
extern crate libc;
#[cfg(feature = "mio-support")]
extern crate mio;
#[cfg(feature = "mio-support")]
extern crate mio_uds;
extern crate signal_hook_registry;
#[cfg(feature = "tokio-support")]
extern crate tokio_reactor;
Expand Down
28 changes: 27 additions & 1 deletion tests/iterator.rs
@@ -1,12 +1,13 @@
extern crate signal_hook;

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{self, RecvTimeoutError};
use std::sync::Arc;
use std::thread;
use std::time::Duration;

use signal_hook::iterator::Signals;
use signal_hook::SIGUSR1;
use signal_hook::{SIGUSR1, SIGUSR2};

#[test]
fn signals_close_forever() {
Expand Down Expand Up @@ -39,3 +40,28 @@ fn signals_close_forever() {
thread.join().unwrap();
}
}

// A reproducer for #16: if we had the mio-support enabled (which is enabled also by the
// tokio-support feature), blocking no longer works. The .wait() would return immediately (an empty
// iterator, possibly), .forever() would do a busy loop.
// flag)
#[test]
fn signals_block_wait() {
let signals = Signals::new(&[SIGUSR2]).unwrap();
let (s, r) = mpsc::channel();
thread::spawn(move || {
// Technically, it may spuriously return early. But it shouldn't be doing it too much, so
// we just try to wait multiple times ‒ if they *all* return right away, it is broken.
for _ in 0..10 {
for _ in signals.wait() {
panic!("Someone really did send us SIGUSR2, which breaks the test");
}
}
let _ = s.send(());
});

let err = r
.recv_timeout(Duration::from_millis(100))
.expect_err("Wait didn't wait properly");
assert_eq!(err, RecvTimeoutError::Timeout);
}

0 comments on commit 5cd4bc0

Please sign in to comment.