move close into non error block and defer it