Prevent shutdown hangs.

Copied from Spigot and slightly modified :P