Bug 812608 - Part 2c: Further improvements to shutdown logic; r=rnewman

Now with tests and more logging!