[FixRM #8312] - Fix file handle leaks

Fix file handle leaks for [SeeRM #8312]
What Luke said.

"You cannot, in general, place a variable declaration in a begin
scope and use it in the ensure scope unless you use nil?. It is
better to swap line 35 and line 34."
Make sure we close hashlist
Properly close hashlist
If exception hits, make sure it's closed.
Use the correct variable
Use the correct variable
Land @2243, @wchen-r7's patch for [SeeRM #8312]