Fix race and leak

Sorry for submitting two commits in this pull request, but the second builds on the first.
why do we need that ```finally``` block? as I see you've invented ```trap``` for that