ockam icon indicating copy to clipboard operation
ockam copied to clipboard

Shut down Worker/Processor if initialization failed

Open SanjoDeundiak opened this issue 1 year ago • 4 comments

Observed behavior

Currently, if Worker or Processor return an error from its initialize function, it's not shut down.

Steps to reproduce

Create a dummy Worker that always fails during initialization.

Desired behavior

Worker/Processor should be stopped

Ockam Version

0.116.0

SanjoDeundiak avatar Feb 12 '24 15:02 SanjoDeundiak

Hi, may I take this one?

pradovic avatar Feb 23 '24 18:02 pradovic

@pradovic sure, go ahead

SanjoDeundiak avatar Feb 23 '24 21:02 SanjoDeundiak

@SanjoDeundiak Thanks! Finally got the time to take a look. If I understand correctly the error in processor or worker initialize function error is just logged by WorkerRelay.

#[cfg_attr(not(feature = "std"), allow(unused_mut))]
    #[cfg_attr(not(feature = "std"), allow(unused_variables))]
    async fn run(mut self, mut ctrl_rx: SmallReceiver<CtrlSignal>) {
        match self.worker.initialize(&mut self.ctx).await {
            Ok(()) => {}
            Err(e) => {
                error!(
                    "Failure during '{}' worker initialisation: {}",
                    self.ctx.address(),
                    e
                );
            }
        }

If I understood correctly this does not stop the worker, but just logs the error. The proposed solution would be to move on to the worker shutdown and context stop ack, without starting the loop in between:

 #[cfg_attr(not(feature = "std"), allow(unused_mut))]
    #[cfg_attr(not(feature = "std"), allow(unused_variables))]
    async fn run(mut self, mut ctrl_rx: SmallReceiver<CtrlSignal>) {
        match self.worker.initialize(&mut self.ctx).await {
            Ok(()) => {}
            Err(e) => {
                error!(
                    "Failure during '{}' worker initialisation: {}",
                    self.ctx.address(),
                    e
                );
                
                // call self.shutdown & ctx.send_stop_ack
                shutdown_and_stop_ack(&mut processor, &mut ctx, &ctx_addr).await;
                return;
            }
        }


Processor is behaving in the same fashion, so I posted worker as a reference.

pradovic avatar Feb 26 '24 12:02 pradovic

I have opened the PR with proposed change: https://github.com/build-trust/ockam/pull/7654. Pls correct me if I am wrong and I will be happy to fix it.

pradovic avatar Feb 26 '24 14:02 pradovic