Skip to content

repl: keep entries added while history file is loading#64513

Open
mhayk wants to merge 1 commit into
nodejs:mainfrom
mhayk:fix-repl-history-init-race
Open

repl: keep entries added while history file is loading#64513
mhayk wants to merge 1 commit into
nodejs:mainfrom
mhayk:fix-repl-history-init-race

Conversation

@mhayk

@mhayk mhayk commented Jul 15, 2026

Copy link
Copy Markdown

Lines can be evaluated while the history file is still being read asynchronously by setupHistory(), e.g. when the input stream does not support pausing (programmatic REPLs driven by mock streams, sockets or pipes). The entries added to the in-memory history in the meantime were discarded once the file load completed, because the loaded entries overwrote the in-memory history, leaving the history file without those entries.

This change merges the persisted entries with the in-memory ones instead (newest first, capped at the configured history size).

Reproduction on main before this change:

import repl from 'node:repl';
import fs from 'node:fs';
import { Stream, Writable } from 'node:stream';

class In extends Stream { resume() {} pause() {} }
In.prototype.readable = true;

const file = '/tmp/history.txt';
fs.writeFileSync(file, 'persisted entry');

const r = repl.start({
  input: new In(),
  output: new Writable({ write(c, e, n) { n(); } }),
  terminal: true,
});
r.setupHistory(file, () => {
  console.log(r.history); // ['persisted entry'] — evaluated lines lost
});
r.input.emit('data', 'const a = 1\nconst b = 2\n');

After this change, r.history is ['const b = 2', 'const a = 1', 'persisted entry'] and the file is flushed accordingly.

Refs: #64508

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. repl Issues and PRs related to the REPL subsystem. labels Jul 15, 2026
Lines can be evaluated while the history file is still being read
asynchronously by setupHistory(), e.g. when the input stream does not
support pausing. The entries added to the in-memory history in the
meantime were discarded once the file load completed, because the
loaded entries overwrote the in-memory history. Merge the persisted
entries with the in-memory ones instead.

Refs: nodejs#64508
Signed-off-by: Mhayk Whandson <[email protected]>
@mhayk mhayk force-pushed the fix-repl-history-init-race branch from f9f897d to 76d0f74 Compare July 15, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. repl Issues and PRs related to the REPL subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants