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.