fix wrong condition

Fix bug of 404472fcca016ae315bf84b67e452d71f7fefd45.
Handles was not properly closed.