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

Compatibility with signal-hook #528

Closed
bytefall opened this issue Nov 25, 2024 · 6 comments
Closed

Compatibility with signal-hook #528

bytefall opened this issue Nov 25, 2024 · 6 comments

Comments

@bytefall
Copy link

First of all, thank you for your library!

There is a quite popular signal-hook crate. Basic service that uses subscriber looks looks like this:

use anyhow::Result;
use core::time::Duration;
use iceoryx2::prelude::*;
use std::{
    io::Write,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
};

const CYCLE_TIME: Duration = Duration::from_secs(1);

fn main() -> Result<()> {
    let stop = Arc::new(AtomicBool::new(false));
    signal_hook::flag::register(signal_hook::consts::SIGINT, Arc::clone(&stop))?;
    signal_hook::flag::register(signal_hook::consts::SIGTERM, Arc::clone(&stop))?;

    let node = NodeBuilder::new().create::<ipc::Service>()?;
    let service = node
        .service_builder(&"My/Funk/ServiceName".try_into()?)
        .publish_subscribe::<u64>()
        .open_or_create()?;
    let subscriber = service.subscriber_builder().create()?;

    while !stop.load(Ordering::Relaxed) {
        // while let NodeEvent::Tick = dbg!(node.wait(CYCLE_TIME)) {
        //     while let Some(sample) = subscriber.receive()? {
        //         println!("received: {:?}", *sample);
        //     }
        // }

        print!(".");
        std::io::stdout().flush()?;
        std::thread::sleep(Duration::from_secs(1));
    }

    println!("Bye");

    Ok(())
}

Pressing Ctrl+C stops the program (you will see "Bye"). Uncommented node.wait code prevents signal-hook to handle Ctrl+C so that the program keeps running.

I couldn't find a crate feature to disable signal handling (using only logger_log feature, which is very helpful).

@elfenpiff
Copy link
Contributor

@bytefall I already have this on my radar. I will add a builder parameter to the node and the waitset to explicitly disable signal handler registration.

The PR should be out in the next week or so.

By the way, we created our own signal handler since we must be able to certify the code according to ISO26262 for ASIL D so that iceoryx2 can also run in mission-critical systems like a car's emergency break.

@elfenpiff
Copy link
Contributor

elfenpiff commented Nov 26, 2024

@bytefall btw. if you do not need all the signal_hook features you can also simply just use Node::wait(). It will reporte to you if a signal was received via its return result value NodeWaitFailure.

If you just want to terminate on SIGINT (CTRL+c) or SIGTERM (kill $PID$) you can write

while node.wait(CYCLE_TIME).is_ok() {
  // ...
}

or if you want to handle it a bit more explicitly

while !stop.load(Ordering::Relaxed) {
  match node.wait(CYCLE_TIME) {
    Ok(()) => my_algorithm(),
    Err(NodeWaitFailure::TerminationRequest) => {
      // someone called for instance kill PID
      stop.store(true, Ordering::Relaxed);
    }
    Err(NodeWaitFailure::Interrupt) => {
      // someone pressed CTRL+c
      stop.store(true, Ordering::Relaxed);
    }
  }
}

elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
@bytefall
Copy link
Author

Thanks @elfenpiff for addressing this requirement.

I'm not developing any mission-critical systems so the certification doesn't make any difference to me. Talking from the perspective of a general developer on the Internet, who is looking for d-bus alternatives.

@elfenpiff
Copy link
Contributor

@bytefall you said the required keyword "d-bus alternatives". We wanted to promote iceoryx2 as d-bus alternative in the near future when some other features like request response are implemented and the documentation is a bit mature.

Could you share some of your d-bus use cases with us?
What features did you use?
Why are you looking for d-bus alternatives?
Are their features on the iceoryx2 side missing which d-bus provides and you would see as important?

elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
@bytefall
Copy link
Author

Could you share some of your d-bus use cases with us?

Sure, I have a patch for dosbox that adds ability to communicate via d-bus. Also there is a TUI front-end to control dosbox.

Why are you looking for d-bus alternatives?

Out of curiosity.

Are their features on the iceoryx2 side missing which d-bus provides and you would see as important

Basically, as you specified in the docs, "Request-Response" is missing.

elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
elfenpiff added a commit to elfenpiff/iceoryx2 that referenced this issue Nov 26, 2024
elfenpiff added a commit that referenced this issue Nov 27, 2024
…ion-optional

[#528] make signal registration optional
@elfenpiff
Copy link
Contributor

@bytefall I added the ability to disable the automatic signal handling on the Node and the WaitSet via the corresponding builder.

When you write:

let node = NodeBuilder::new()
                .name(&"my_little_node".try_into()?)
                .signal_handling_mode(SignalHandlingMode::Disabled)
                .create::<ipc::Service>()?;

/// this will no longer fetch SIGINT or SIGTERM
while node.wait(CYCLE_TIME).is_ok() {
    
}

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