From 006f6156bd2880e7f798097b2ed9db3b17616f87 Mon Sep 17 00:00:00 2001 From: Lei Shi Date: Fri, 19 Apr 2024 14:34:07 +0800 Subject: [PATCH 1/4] lib: avoid repl.setupHistory() blocks node exit after repl closed Fixes: https://github.com/nodejs/node/issues/52386 --- lib/internal/repl/history.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/internal/repl/history.js b/lib/internal/repl/history.js index d8c36170639fc9..ac37d264f50d3d 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.close) { + return; + } + if (writing) { pending = true; return; From fb1871a7608aa0fa9068c452db4753e368675edf Mon Sep 17 00:00:00 2001 From: Lei Shi Date: Fri, 19 Apr 2024 14:54:41 +0800 Subject: [PATCH 2/4] lib: avoid repl.setupHistory() blocks node exit after repl closed Fixes: https://github.com/nodejs/node/issues/52386 --- lib/internal/repl/history.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/repl/history.js b/lib/internal/repl/history.js index ac37d264f50d3d..19ecee3d86b9f2 100644 --- a/lib/internal/repl/history.js +++ b/lib/internal/repl/history.js @@ -137,7 +137,7 @@ function setupHistory(repl, historyPath, ready) { function flushHistory() { timer = null; - if (repl.close) { + if (repl.closed) { return; } From d81a580afc1f8662c85e20e0268feafad5271a51 Mon Sep 17 00:00:00 2001 From: Lei Shi Date: Mon, 22 Apr 2024 11:09:11 +0800 Subject: [PATCH 3/4] lib: add test --- test/parallel/test-repl-close-before-setupHistory.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 test/parallel/test-repl-close-before-setupHistory.js 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..de676d20837a6e --- /dev/null +++ b/test/parallel/test-repl-close-before-setupHistory.js @@ -0,0 +1,7 @@ +'use strict'; +const common = require('../common'); +const repl = require('repl'); + +var r = repl.start({}); +r.setupHistory(__dirname + '/history', () => { }); // Different results will occur here +r.close(); \ No newline at end of file From 7f46d82367cb38be9ec3c086b833cdbc395043dc Mon Sep 17 00:00:00 2001 From: Lei Shi Date: Wed, 24 Apr 2024 16:37:10 +0800 Subject: [PATCH 4/4] fixed lint --- .../test-repl-close-before-setupHistory.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-repl-close-before-setupHistory.js b/test/parallel/test-repl-close-before-setupHistory.js index de676d20837a6e..59c5c5e0c3bb4a 100644 --- a/test/parallel/test-repl-close-before-setupHistory.js +++ b/test/parallel/test-repl-close-before-setupHistory.js @@ -1,7 +1,13 @@ 'use strict'; -const common = require('../common'); + +require('../common'); +const assert = require('assert'); +const tmpdir = require('../common/tmpdir'); const repl = require('repl'); -var r = repl.start({}); -r.setupHistory(__dirname + '/history', () => { }); // Different results will occur here -r.close(); \ No newline at end of file +tmpdir.refresh(); +const r = repl.start({}); +const historyPath = tmpdir.resolve('.history'); + +r.setupHistory(historyPath, () => { assert.strictEqual(r.closed, true); }); +r.close();