[misc] Fix possible resource leak

(cherry picked from commit 99cbca426ba70255191a62a5dd372181c49bcd09)