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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple set_handler calls don't properly overwrite? 馃 #118

Open
slafs opened this issue Apr 8, 2024 · 2 comments
Open

Multiple set_handler calls don't properly overwrite? 馃 #118

slafs opened this issue Apr 8, 2024 · 2 comments

Comments

@slafs
Copy link

slafs commented Apr 8, 2024

Hello!

Not sure if I'm holding it wrong or... 馃

The docs for set_handler say:

Will return an error if a system error occurred while setting the handler.

and for try_set_handler say:

Will return an error if another handler exists or if a system error occurred while setting the handler.

But a modified snippet from the README like this:

use ctrlc;
use std::sync::mpsc::channel;

fn main() {
    let (tx, rx) = channel();

    ctrlc::set_handler(move || {}).expect("Error setting Ctrl-C handler");

    ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
        .expect("Error setting Ctrl-C handler for the second time");

    println!("Waiting for Ctrl-C...");
    rx.recv().expect("Could not receive from channel.");
    println!("Got it! Exiting...");
}

panics with:

thread 'main' panicked at src/main.rs:10:10:
Error setting Ctrl-C handler for the second time: MultipleHandlers

So it seems set_handler can return MultipleHandlers? 馃

I'm on OSX 14.4.1

@Detegr
Copy link
Owner

Detegr commented Apr 8, 2024

Hi.

You are correct, this is a bug. It has been there since the introduction of try_set_handler. Quite an obvious bug as well, what have I been thinking :)

@Detegr
Copy link
Owner

Detegr commented Apr 13, 2024

Actually, I'm taking it back. set_handler will overwrite whatever handler there was set by something else than this crate, but it will do it just once. try_set_handler will return MultipleHandlers if something else than this crate had set a handler for the signal.

The wording for the logic is quite poor as my initial thoughts were too that it's not working properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants