diff --git a/lib/internal/repl/history.js b/lib/internal/repl/history.js index d8c36170639fc9..19ecee3d86b9f2 100644 --- a/lib/internal/repl/history.js +++ b/lib/internal/repl/history.js @@ -136,6 +136,11 @@ function setupHistory(repl, historyPath, ready) { function flushHistory() { timer = null; + + if (repl.closed) { + return; + } + if (writing) { pending = true; return; diff --git a/test/parallel/test-repl-close-before-setupHistory.js b/test/parallel/test-repl-close-before-setupHistory.js new file mode 100644 index 00000000000000..59c5c5e0c3bb4a --- /dev/null +++ b/test/parallel/test-repl-close-before-setupHistory.js @@ -0,0 +1,13 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const tmpdir = require('../common/tmpdir'); +const repl = require('repl'); + +tmpdir.refresh(); +const r = repl.start({}); +const historyPath = tmpdir.resolve('.history'); + +r.setupHistory(historyPath, () => { assert.strictEqual(r.closed, true); }); +r.close();