diff --git a/index.js b/index.js index 3eef78a..64ae987 100644 --- a/index.js +++ b/index.js @@ -224,7 +224,13 @@ function writeFileSync (filename, data, options) { fs.renameSync(tmpfile, filename) removeOnExitHandler() } catch (err) { - if (fd) fs.closeSync(fd) + if (fd) { + try { + fs.closeSync(fd) + } catch (ex) { + // ignore close errors at this stage, error may have closed fd already. + } + } removeOnExitHandler() cleanup() throw err