Resource Leaks cleanup

Our Jenkins installation still suffers from resource leaks on big uptimes.
This fix targets high-impact resource leaks discovered by [Coverity Scan][1]

[1]: https://scan.coverity.com/projects/94

I've moved the stuff to <code>try</code> section.