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

Change-Id: Id5010c945118920f54086d1a58ba2a671fe6b06f