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.
What about development mode, will it be terminated properly in case of stopping application (i.e.: in sbt/activator console)?