i3ipc-rs icon indicating copy to clipboard operation
i3ipc-rs copied to clipboard

How to stop event listening?

Open hasufell opened this issue 4 years ago • 2 comments

I have this running:

        let mut event_listener = i3ipc::I3EventListener::connect().unwrap();

        for event in event_listener.listen() {
            match event.as_ref() {
                ...
            }
       }

Then I have a signal handler:

    let signals = Signals::new(&[SIGINT, SIGTERM]).unwrap();
    let signal_handler = thread::spawn(move || {
        for sig in signals.forever() {
            match sig {
                signal_hook::SIGINT => on_exit(&con_thread),
                signal_hook::SIGTERM => on_exit(&con_thread),
                _ => unreachable!(),
            }
        }
    });

There will be race-conditions, since on_exit will trigger i3 events. How can I stop the event listener or unsubscribe from events? Running it in a separate thread isn't an option, because I can't kill that thread, since event listening is a blocking operation.

hasufell avatar Mar 31 '20 22:03 hasufell

because I can't kill that thread, since event listening is a blocking operation

Do you mean that you're unable to kill the thread, or that there's no way to communicate to the thread that it should exit?

It's not clear from your example above whether this would work, but could you use some sort of thread-safe semaphore variable that the event listener checks before processing an event? I guess the question is whether receiving the event in the first place is the problem, or whether the actions that the listener takes after receiving an event are the problem.

brownjohnf avatar Jun 30 '20 17:06 brownjohnf

Do you mean that you're unable to kill the thread, or that there's no way to communicate to the thread that it should exit?

A thread that is blocked is usually non-interruptable and cannot be killed right away.

hasufell avatar Jun 30 '20 17:06 hasufell