Simplify ClosableLazy and prevent deadlock

This change is split into two commits so you can see the changes.

* I tidied up some old code of mine which was more complicated than it needed to be.
* Fixed the deadlock reported in #2986.

I also added a test.
I think when shutting down an application, it's acceptable to wait forever for things to terminate, since the OS (or init.d script) will usually use some sort of timeout itself before doing a kill -9.  It doesn't make sense in my opinion to timeout at multiple levels, only the top level should control and perform the timeout.