diff --git a/lib/internal/repl/history.js b/lib/internal/repl/history.js index dd3ea850954936..5d90b40e0041c7 100644 --- a/lib/internal/repl/history.js +++ b/lib/internal/repl/history.js @@ -93,6 +93,7 @@ function setupHistory(repl, historyPath, ready) { fs.ftruncate(hnd, 0, (err) => { repl._historyHandle = hnd; repl.on('line', online); + repl.once('exit', onexit); // Reading the file data out erases it repl.once('flushHistory', function() { @@ -137,6 +138,15 @@ function setupHistory(repl, historyPath, ready) { } } } + + function onexit() { + if (repl._flushing) { + repl.once('flushHistory', onexit); + return; + } + repl.off('line', online); + fs.close(repl._historyHandle, () => {}); + } } function _replHistoryMessage() {