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
17 changes: 16 additions & 1 deletion lib/internal/repl/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const kResolveHistoryPath = Symbol('_kResolveHistoryPath');
const kReplHistoryMessage = Symbol('_kReplHistoryMessage');
const kFlushHistory = Symbol('_kFlushHistory');
const kGetHistoryPath = Symbol('_kGetHistoryPath');
const kCloseHandle = Symbol('_kCloseHandle');

class ReplHistory {
constructor(context, options) {
Expand Down Expand Up @@ -393,15 +394,29 @@ class ReplHistory {
}
this[kContext].off('line', this[kOnLine].bind(this));

await this[kCloseHandle]();
}

async [kCloseHandle]() {
if (this[kHistoryHandle] !== null) {
const handle = this[kHistoryHandle];
this[kHistoryHandle] = null;
try {
await this[kHistoryHandle].close();
await handle.close();
} catch (err) {
debug('Error closing history file:', err);
}
}
}

/**
* Closes the history file handle.
* @returns {Promise<void>}
*/
closeHandle() {
return this[kCloseHandle]();
}

[kReplHistoryMessage]() {
if (this[kHistory].length === 0) {
ReplHistory[kWriteToOutput](
Expand Down
10 changes: 8 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,12 +1039,18 @@ class REPLServer extends Interface {
this[kBufferedCommandSymbol] = '';
}
close() {
if (this.terminal && this.historyManager.isFlushing && !this._closingOnFlush) {
if (this.terminal && this.historyManager?.isFlushing && !this._closingOnFlush) {
this._closingOnFlush = true;
this.once('flushHistory', () => super.close());
this.once('flushHistory', () => this.close());

return;
}
// Ensure the history file handle is closed before completing
if (this.terminal && this.historyManager?.closeHandle && !this._historyHandleClosed) {
this._historyHandleClosed = true;
this.historyManager.closeHandle().then(() => super.close());
return;
}
process.nextTick(() => super.close());
}
createContext() {
Expand Down
Loading