Clean up resources on worker error and termination