Fix graceful shutdown handling