Skip to content

repl: fix FileHandle leak in history initialization#61706

Open
Han5991 wants to merge 2 commits intonodejs:mainfrom
Han5991:fix/repl-history-filehandle-leak
Open

repl: fix FileHandle leak in history initialization#61706
Han5991 wants to merge 2 commits intonodejs:mainfrom
Han5991:fix/repl-history-filehandle-leak

Conversation

@Han5991
Copy link
Contributor

@Han5991 Han5991 commented Feb 6, 2026

Summary

This PR fixes a FileHandle leak in lib/internal/repl/history.js that causes ERR_INVALID_STATE when garbage collection occurs.

If an error occurs during kInitializeHistory (specifically within the try block that includes kFlushHistory), the open FileHandle (this[kHistoryHandle]) was not being explicitly closed before delegating to kHandleHistoryInitError. This led to the handle being closed by the garbage collector, triggering the error.

This fixes the regression observed in test-repl-programmatic-history.

References

Fixes #61578

@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 6, 2026
@Han5991 Han5991 force-pushed the fix/repl-history-filehandle-leak branch from a710016 to 9d08876 Compare February 6, 2026 13:00
@codecov
Copy link

codecov bot commented Feb 6, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61706      +/-   ##
==========================================
- Coverage   89.76%   89.75%   -0.01%     
==========================================
  Files         674      674              
  Lines      204424   204425       +1     
  Branches    39289    39287       -2     
==========================================
- Hits       183499   183480      -19     
- Misses      13219    13256      +37     
+ Partials     7706     7689      -17     
Files with missing lines Coverage Δ
lib/internal/repl/history.js 93.62% <100.00%> (+0.01%) ⬆️

... and 35 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.

Ensure that the history file handle is closed if initialization fails or
flushing throws an error. This prevents ERR_INVALID_STATE errors where
a FileHandle object is closed during garbage collection.
@Han5991 Han5991 force-pushed the fix/repl-history-filehandle-leak branch from 9d08876 to 52e5e2d Compare February 6, 2026 15:19
@lpinca
Copy link
Member

lpinca commented Feb 6, 2026

Is it possible to add a test?

This adds a test case to verify that the file handle is properly
closed when REPL history initialization fails, preventing
resource leaks.

Refs: 52e5e2d
@Han5991
Copy link
Contributor Author

Han5991 commented Feb 6, 2026

@lpinca

Thank you for your review, I have finished adding unit tests.

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.

test-repl-programmatic-history is flaky

3 participants