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

Fixed race condition on Win poll #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
74 changes: 49 additions & 25 deletions src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::ffi::c_void;
use std::io::{Error, ErrorKind, Result};
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use windows::Win32::Foundation::{BOOLEAN, HANDLE};
use windows::Win32::NetworkManagement::IpHelper::{
Expand Down Expand Up @@ -43,24 +42,25 @@ pub struct IfWatcher {
queue: VecDeque<IfEvent>,
#[allow(unused)]
notif: IpChangeNotification,
waker: Arc<AtomicWaker>,
resync: Arc<AtomicBool>,
shared: Pin<Box<IfWatcherShared>>,
}

impl IfWatcher {
/// Create a watcher.
pub fn new() -> Result<Self> {
let resync = Arc::new(AtomicBool::new(true));
let waker = Arc::new(AtomicWaker::new());
let shared = IfWatcherShared {
resync: true.into(),
waker: Default::default(),
};
let shared = Box::pin(shared);
Ok(Self {
addrs: Default::default(),
queue: Default::default(),
waker: waker.clone(),
resync: resync.clone(),
notif: IpChangeNotification::new(Box::new(move |_, _| {
resync.store(true, Ordering::Relaxed);
waker.wake();
}))?,
// Safety:
// Self referential structure, `shared` will be dropped
// after `notif`
notif: unsafe { IpChangeNotification::new(shared.as_ref())? },
shared,
})
}

Expand Down Expand Up @@ -96,10 +96,13 @@ impl IfWatcher {
if let Some(event) = self.queue.pop_front() {
return Poll::Ready(Ok(event));
}
if !self.resync.swap(false, Ordering::Relaxed) {
self.waker.register(cx.waker());

self.shared.waker.register(cx.waker());
if !self.shared.resync.swap(false, Ordering::AcqRel) {
return Poll::Pending;
}
self.shared.waker.take();

if let Err(error) = self.resync() {
return Poll::Ready(Err(error));
}
Expand Down Expand Up @@ -137,10 +140,22 @@ fn ifaddr_to_ipnet(addr: IfAddr) -> IpNet {
}
}

#[derive(Debug)]
struct IfWatcherShared {
waker: AtomicWaker,
resync: AtomicBool,
}

impl IpChangeCallback for IfWatcherShared {
fn callback(&self, _row: &MIB_IPINTERFACE_ROW, _notification_type: MIB_NOTIFICATION_TYPE) {
self.resync.store(true, Ordering::Release);
self.waker.wake();
}
}

/// IP change notifications
struct IpChangeNotification {
handle: HANDLE,
callback: *mut IpChangeCallback,
}

impl std::fmt::Debug for IpChangeNotification {
Expand All @@ -149,31 +164,37 @@ impl std::fmt::Debug for IpChangeNotification {
}
}

type IpChangeCallback = Box<dyn Fn(&MIB_IPINTERFACE_ROW, MIB_NOTIFICATION_TYPE) + Send>;

impl IpChangeNotification {
/// Register for route change notifications
fn new(cb: IpChangeCallback) -> Result<Self> {
unsafe extern "system" fn global_callback(
///
/// Safety: C must outlive the resulting Self
unsafe fn new<C>(cb: Pin<&C>) -> Result<Self>
where
C: IpChangeCallback + Send + Sync,
{
unsafe extern "system" fn global_callback<C>(
caller_context: *const c_void,
row: *const MIB_IPINTERFACE_ROW,
notification_type: MIB_NOTIFICATION_TYPE,
) {
(**(caller_context as *const IpChangeCallback))(&*row, notification_type)
) where
C: IpChangeCallback + Send + Sync,
{
let caller_context = &*(caller_context as *const C);
caller_context.callback(&*row, notification_type)
}
let mut handle = HANDLE::default();
let callback = Box::into_raw(Box::new(cb));
let callback = cb.get_ref() as *const C;
unsafe {
NotifyIpInterfaceChange(
AF_UNSPEC,
Some(global_callback),
Some(callback as _),
Some(global_callback::<C>),
Some(callback as *const c_void),
BOOLEAN(0),
&mut handle as _,
)
.map_err(|err| Error::new(ErrorKind::Other, err.to_string()))?;
}
Ok(Self { callback, handle })
Ok(Self { handle })
}
}

Expand All @@ -183,9 +204,12 @@ impl Drop for IpChangeNotification {
if let Err(err) = CancelMibChangeNotify2(self.handle) {
log::error!("error deregistering notification: {}", err);
}
drop(Box::from_raw(self.callback));
}
}
}

unsafe impl Send for IpChangeNotification {}

trait IpChangeCallback {
fn callback(&self, row: &MIB_IPINTERFACE_ROW, notification_type: MIB_NOTIFICATION_TYPE);
}