Skip to content

http2: fix FileHandle leak in respondWithFile#61707

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

http2: fix FileHandle leak in respondWithFile#61707
Han5991 wants to merge 2 commits intonodejs:mainfrom
Han5991:fix/http2-filehandle-leak

Conversation

@Han5991
Copy link
Contributor

@Han5991 Han5991 commented Feb 6, 2026

Summary

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

In processRespondWithFD, if buildNgHeaderString throws (e.g., due to invalid headers), the code would catch the error and destroy the stream but fail to close the file handle (fd) if it owned it (self.ownsFd). This leaves the file handle open, eventually triggering the GC error.

This patch adds a tryClose(fd) call in the catch block and error handling path to ensure the handle is properly released.

References

  • Fixes potential ERR_INVALID_STATE in http2 module.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/http2
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http2 Issues or PRs related to the http2 subsystem. needs-ci PRs that need a full CI run. labels Feb 6, 2026
Ensure that the file handle is closed if header validation fails in
respondWithFile. This prevents ERR_INVALID_STATE errors where a
FileHandle object is closed during garbage collection.
@Han5991 Han5991 force-pushed the fix/http2-filehandle-leak branch from 4859ad9 to 5c4dd6b Compare February 6, 2026 13:25
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Thanks for opening a PR! Can you please add a unit test?

@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 (ae2ffce) to head (f6d474a).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #61707   +/-   ##
=======================================
  Coverage   89.75%   89.75%           
=======================================
  Files         674      674           
  Lines      204416   204428   +12     
  Branches    39285    39284    -1     
=======================================
+ Hits       183472   183484   +12     
- Misses      13227    13241   +14     
+ Partials     7717     7703   -14     
Files with missing lines Coverage Δ
lib/internal/http2/core.js 95.34% <100.00%> (+0.11%) ⬆️

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

Verifies that the file descriptor is closed when an error occurs during
header validation in respondWithFile.

Refs: 5c4dd6b
@Han5991 Han5991 requested a review from mcollina February 6, 2026 15:18
@Han5991
Copy link
Contributor Author

Han5991 commented Feb 6, 2026

Thank you for your review. I have added unit tests.

Copy link
Member

@pimterry pimterry left a comment

Choose a reason for hiding this comment

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

LGTM 👍

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

@Han5991
Copy link
Contributor Author

Han5991 commented Feb 6, 2026

@pimterry

I think I got a flaki tI think I got a flaki test. Could you please give me the ci again? Thank you.est. Could you please give me the ci again? Thank you.

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

Labels

http2 Issues or PRs related to the http2 subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants