Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/internal/repl/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ class ReplHistory {

await this[kFlushHistory]();
} catch (err) {
await this[kCloseHandle]();
return this[kHandleHistoryInitError](err, onReadyCallback);
}
}
Expand Down
56 changes: 56 additions & 0 deletions test/parallel/test-repl-history-init-fail-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
const fs = require('fs');
const path = require('path');
const { ReplHistory } = require('internal/repl/history');
const assert = require('assert');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const historyPath = path.join(tmpdir.path, '.node_repl_history');

fs.writeFileSync(historyPath, 'dummy\n');

const originalOpen = fs.promises.open;
let closeCalled = false;

fs.promises.open = async (filepath, flags, mode) => {
const handle = await originalOpen(filepath, flags, mode);

if (flags === 'r+' && filepath === historyPath) {
handle.truncate = async (len) => {
throw new Error('Mock truncate error');
};

const originalClose = handle.close;
handle.close = async () => {
closeCalled = true;
return originalClose.call(handle);
};
}

return handle;
};

const context = {
historySize: 30,
on: () => {},
once: () => {},
emit: () => {},
pause: () => {},
resume: () => {},
off: () => {},
line: '',
_historyPrev: () => {},
_writeToOutput: () => {}
};

const history = new ReplHistory(context, { filePath: historyPath });

history.initialize(common.mustCall((err) => {
assert.strictEqual(err, null);
assert.strictEqual(closeCalled, true);
}));
Loading