Fix graceful shutdown logic

Resolve bug #1081367