Use defer to guarantee that we close resource handles
Created by: olafurpg
Followup of comment https://github.com/sourcegraph/sourcegraph/pull/23297#discussion_r679878277
Created by: olafurpg
Followup of comment https://github.com/sourcegraph/sourcegraph/pull/23297#discussion_r679878277