Skip to content

Instantly share code, notes, and snippets.

@KarthikNedunchezhiyan
Created August 29, 2020 06:38
Show Gist options
  • Save KarthikNedunchezhiyan/39fbdd3eee4b97ab540bf3cb16b21313 to your computer and use it in GitHub Desktop.
Save KarthikNedunchezhiyan/39fbdd3eee4b97ab540bf3cb16b21313 to your computer and use it in GitHub Desktop.
sample code to reproduce race condition that triggers false callback
use event_manager::{EventManager, EventOps, EventSet, Events, MutEventSubscriber, SubscriberOps};
use std::os::unix::io::AsRawFd;
use vmm_sys_util::eventfd::EventFd;
static mut event_loop_manager: Option<EventManager<EventProcessor>> = None;
pub struct EventProcessor {
fd1: EventFd,
fd2: EventFd,
subscriber_uuid: u32,
}
impl MutEventSubscriber for EventProcessor {
fn process(&mut self, events: Events, ops: &mut EventOps) {
println!("event received for subscriber uuid {}", self.subscriber_uuid);
self.fd1.read().unwrap();
self.fd2.read().unwrap();
ops.remove(Events::with_data(&self.fd1, 1, EventSet::IN)).unwrap();
ops.remove(Events::with_data(&self.fd2, 1, EventSet::IN)).unwrap();
nix::unistd::close(self.fd1.as_raw_fd()).unwrap();
nix::unistd::close(self.fd2.as_raw_fd()).unwrap();
unsafe {
event_loop_manager.as_mut().unwrap().add_subscriber(EventProcessor {
fd1: EventFd::new(0).unwrap(),
fd2: EventFd::new(0).unwrap(),
subscriber_uuid: 2,
});
}
}
fn init(&mut self, ops: &mut EventOps) {c
ops.add(Events::with_data(&self.fd1, 1u32, EventSet::IN)).unwrap();
ops.add(Events::with_data(&self.fd2, 1u32, EventSet::IN)).unwrap();
}
}
fn main() {
let mut manager: EventManager<EventProcessor> = event_manager::EventManager::<EventProcessor>::new().unwrap();
let fd1 = EventFd::new(0).unwrap();
let fd2 = EventFd::new(0).unwrap();
fd1.write(1).unwrap();
fd2.write(1).unwrap();
manager.add_subscriber(EventProcessor { fd1, fd2, subscriber_uuid: 1, });
unsafe { event_loop_manager = Some(manager); }
loop { unsafe { event_loop_manager.as_mut().unwrap().run().unwrap(); } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment