Skip to content

repl: fix FileHandle leak in history initialization#61706

Merged
nodejs-github-bot merged 2 commits intonodejs:mainfrom
Han5991:fix/repl-history-filehandle-leak
Feb 9, 2026
Merged

repl: fix FileHandle leak in history initialization#61706
nodejs-github-bot merged 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 21 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.

@lpinca lpinca added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 7, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 7, 2026
@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

lgtm

@Han5991
Copy link
Contributor Author

Han5991 commented Feb 8, 2026

I think a flare test occurred. Could you please request the CI again? Thank you.

@nodejs-github-bot
Copy link
Collaborator

@addaleax addaleax added commit-queue Add this label to land a pull request using GitHub Actions. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Feb 9, 2026
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Feb 9, 2026
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/61706
✔  Done loading data for nodejs/node/pull/61706
----------------------------------- PR info ------------------------------------
Title      repl: fix FileHandle leak in history initialization (#61706)
Author     sangwook <rewq5991@gmail.com> (@Han5991)
Branch     Han5991:fix/repl-history-filehandle-leak -> nodejs:main
Labels     repl, author ready, needs-ci
Commits    2
 - repl: fix FileHandle leak in history initialization
 - test: add regression test for repl history leak
Committers 1
 - sangwook <rewq5991@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/61706
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/61706
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Fri, 06 Feb 2026 12:58:24 GMT
   ✔  Approvals: 3
   ✔  - Luigi Pinca (@lpinca): https://github.com/nodejs/node/pull/61706#pullrequestreview-3767812467
   ✔  - Gürgün Dayıoğlu (@gurgunday): https://github.com/nodejs/node/pull/61706#pullrequestreview-3767899580
   ✔  - Anna Henningsen (@addaleax): https://github.com/nodejs/node/pull/61706#pullrequestreview-3772404810
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2026-02-08T16:45:41Z: https://ci.nodejs.org/job/node-test-pull-request/71256/
- Querying data for job/node-test-pull-request/71256/
✔  Build data downloaded
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  No git cherry-pick in progress
   ✔  No git am in progress
   ✔  No git rebase in progress
--------------------------------------------------------------------------------
- Bringing origin/main up to date...
From https://github.com/nodejs/node
 * branch                  main       -> FETCH_HEAD
✔  origin/main is now up-to-date
- Downloading patch for 61706
From https://github.com/nodejs/node
 * branch                  refs/pull/61706/merge -> FETCH_HEAD
✔  Fetched commits as aa3e649ca1ef..e87b6f401794
--------------------------------------------------------------------------------
[main a717e4037d] repl: fix FileHandle leak in history initialization
 Author: sangwook <rewq5991@gmail.com>
 Date: Fri Feb 6 21:57:03 2026 +0900
 1 file changed, 1 insertion(+)
[main 1cd06c80ad] test: add regression test for repl history leak
 Author: sangwook <rewq5991@gmail.com>
 Date: Sat Feb 7 06:47:58 2026 +0900
 1 file changed, 56 insertions(+)
 create mode 100644 test/parallel/test-repl-history-init-fail-leak.js
   ✔  Patches applied
There are 2 commits in the PR. Attempting autorebase.
(node:664) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated.
(Use `node --trace-deprecation ...` to show where the warning was created)
Rebasing (2/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
repl: fix FileHandle leak in history initialization

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.

PR-URL: #61706
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Anna Henningsen <anna@addaleax.net>

[detached HEAD ccedd65226] repl: fix FileHandle leak in history initialization
Author: sangwook <rewq5991@gmail.com>
Date: Fri Feb 6 21:57:03 2026 +0900
1 file changed, 1 insertion(+)
Rebasing (3/4)
Rebasing (4/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
test: add regression test for repl history leak

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

Refs: 52e5e2d0da35a21235ae2b516f605508370e8865
PR-URL: #61706
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: Anna Henningsen <anna@addaleax.net>

[detached HEAD 54f6f74b63] test: add regression test for repl history leak
Author: sangwook <rewq5991@gmail.com>
Date: Sat Feb 7 06:47:58 2026 +0900
1 file changed, 56 insertions(+)
create mode 100644 test/parallel/test-repl-history-init-fail-leak.js
Successfully rebased and updated refs/heads/main.

ℹ Add commit-queue-squash label to land the PR as one commit, or commit-queue-rebase to land as separate commits.

https://github.com/nodejs/node/actions/runs/21821224872

@addaleax addaleax added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Feb 9, 2026
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Feb 9, 2026
@nodejs-github-bot nodejs-github-bot merged commit 71b36ee into nodejs:main Feb 9, 2026
82 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 71b36ee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. 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

5 participants