Merge pull request #1820, improve graceful shutdown.