Shutdown race condition

fixes #1249