Fix exception on shutdown