rustiron

panic! does not stop an Iron server


I thought panic! stops everything in Rust, but if I panic! in an Iron route handler function it does not stop the whole server. Instead, it just displays the panic message.

Is it the "normal" behavior of panic!?

I'm not posting my actual code because I don't think it's useful here, but I can add it if required.


Solution

  • I thought panic! stops everything in Rust.

    Not quite: panic! only stops¹ the current thread, which for single threaded programs stops the whole program. If you spawn another thread, the parent thread can detect whether or not the child thread panicked on joining.

    Iron uses many threads to handle multiple requests in parallel. Apparently it just ignores panics in child threads...


    ¹ As DK. and Vladimir Matveev mentioned in the comments, it's not that simple. Multiple things can happen when a panic is "thrown". By default, the application starts unwinding (climbing down the stack) until "the end" of the stack or a catch_unwind is reached. In the former case, the thread is killed; in the latter it's up to the user to decide what happens. It's also possible to configure your build such that abort is called on panic!.