fix stackoverflow on shutdown