Skip to content
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

sources.generic: don't unregister non-registered sources on drop #153

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

#### Bugfixes

- Fix an issue where the `Generic` event source would try to unregister its contents from the event loop
after a failed registration.

## 0.12.1 -- 2023-09-19

#### Bugfixes
Expand Down
57 changes: 53 additions & 4 deletions src/sources/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,7 @@ where
fn register(&mut self, poll: &mut Poll, token_factory: &mut TokenFactory) -> crate::Result<()> {
let token = token_factory.token();

// Make sure we can use the poller to deregister if need be.
self.poller = Some(poll.poller().clone());

// SAFETY: We've now ensured that we have a poller to deregister with.
// SAFETY: We ensure that we have a poller to deregister with (see below).
unsafe {
poll.register(
&self.file.as_ref().unwrap().0,
Expand All @@ -274,7 +271,13 @@ where
)?;
}

// Make sure we can use the poller to deregister if need be.
// But only if registration actually succeeded
// So that we don't try to unregister the FD on drop if it wasn't registered
// in the first place (for example if registration failed because of a duplicate insertion)
self.poller = Some(poll.poller().clone());
self.token = Some(token);

Ok(())
}

Expand Down Expand Up @@ -422,4 +425,50 @@ mod tests {
// the has now been properly dispatched
assert!(dispached);
}

// Duplicate insertion does not fail on all platforms, but does on Linux
#[cfg(target_os = "linux")]
#[test]
fn duplicate_insert() {
use std::os::unix::{
io::{AsFd, BorrowedFd},
net::UnixStream,
};
let event_loop = crate::EventLoop::<()>::try_new().unwrap();

let handle = event_loop.handle();

let (_, rx) = UnixStream::pair().unwrap();

// Rc only implements AsFd since 1.69...
struct RcFd<T> {
rc: std::rc::Rc<T>,
}

impl<T: AsFd> AsFd for RcFd<T> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.rc.as_fd()
}
}

let rx = std::rc::Rc::new(rx);

let token = handle
.insert_source(
Generic::new(RcFd { rc: rx.clone() }, Interest::READ, Mode::Level),
|_, _, _| Ok(PostAction::Continue),
)
.unwrap();

// inserting the same FD a second time should fail
let ret = handle.insert_source(
Generic::new(RcFd { rc: rx.clone() }, Interest::READ, Mode::Level),
|_, _, _| Ok(PostAction::Continue),
);
assert!(ret.is_err());
std::mem::drop(ret);

// but the original token is still registered
handle.update(&token).unwrap();
}
}
Loading