Skip to content

Conversation

@mcollina
Copy link
Member

@mcollina mcollina commented Feb 1, 2026

Summary

Fixes flaky test test/parallel/test-repl-programmatic-history.js.

Problem

The test was flaky because the FileHandle for the history file could be garbage collected before being explicitly closed, causing ERR_INVALID_STATE errors when the handle was already closed by the GC finalizer.

Solution

  • Added an explicit closeHandle() method to ReplHistory class
  • Modified REPLServer.close() to wait for the history file handle to be properly closed before completing the close operation
  • This ensures deterministic cleanup regardless of GC timing

Verification

Verified with python3 tools/test.py --repeat 1000 test/parallel/test-repl-programmatic-history.js - all 1000 runs passed with 0 failures.

Refs: nodejs/reliability#1450

@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 Feb 1, 2026
@mcollina mcollina force-pushed the fix-flaky-test-repl-programmatic-history branch from 56947c3 to e2afc62 Compare February 1, 2026 14:50
@codecov
Copy link

codecov bot commented Feb 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.75%. Comparing base (f6464c5) to head (d715296).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61614      +/-   ##
==========================================
- Coverage   89.76%   89.75%   -0.02%     
==========================================
  Files         673      674       +1     
  Lines      203944   204088     +144     
  Branches    39191    39238      +47     
==========================================
+ Hits       183080   183171      +91     
- Misses      13194    13238      +44     
- Partials     7670     7679       +9     
Files with missing lines Coverage Δ
lib/internal/repl/history.js 93.60% <100.00%> (+1.17%) ⬆️
lib/repl.js 94.15% <100.00%> (+0.02%) ⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

debug('Error closing history file:', err);
}
}
this[kContext].emit('historyHandleClosed');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this new event used for?

lib/repl.js Outdated
if (this.terminal && this.historyManager?.closeHandle && !this._historyHandleClosed) {
this._historyHandleClosed = true;
this.historyManager.closeHandle().then(() => {
process.nextTick(() => super.close());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is process.nextTick() needed here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generically prefer this to exit the promise chain. It's probably safe to avoid here

The test was flaky because the FileHandle for the history file could
be garbage collected before being explicitly closed, causing
ERR_INVALID_STATE errors when the handle was already closed by the
GC finalizer.

This fix adds an explicit closeHandle() method to ReplHistory and
ensures that REPLServer.close() waits for the history file handle
to be properly closed before completing the close operation.

Refs: nodejs/reliability#1450
@mcollina mcollina force-pushed the fix-flaky-test-repl-programmatic-history branch from e2afc62 to d715296 Compare February 2, 2026 10:40
@lpinca lpinca added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 2, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 2, 2026
@nodejs-github-bot
Copy link
Collaborator

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