How to trigger shutdown_hook when an error occurs

This question originally came up in our Slack community and the thread has been consolidated here for long-term reference.

Our use case requires that if any error occurs (from client code or the inference API), we shutdown and notify another component of our system.

How can we trigger the shutdown_hook method when an error occurs (both recoverable and non-recoverable)?

You can call shutdown on the JobContext to explicitly trigger the shutdown_hook.

See the documentation: