Defer closing after err check

Signed-off-by: Maria Shaldibina <mshaldibina@pivotal.io>