Fix race condition on shutdown
Merge pull request #738 from ricnewton/master

Fix race condition on shutdown