Fix crash on shutdown.