Avoid double-free/double-fclose on error. am: 332513d0d2 am: 6c5b3c3fed
am: 8d222cd40c

Change-Id: I67608072a9309cbf325071c006a1d517e8583d3b