Improvements to container exit reliability

Improve a few problems with container exit which can lead to resource leaks.