Fix input file descriptor leak across multi-file evaluation - #2804
Open
fzlzjerry wants to merge 1 commit into
Open
Fix input file descriptor leak across multi-file evaluation#2804fzlzjerry wants to merge 1 commit into
fzlzjerry wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a file-descriptor leak when evaluating many input files by ensuring that readers created by readStream are closable and then closed within a per-file scope in both the streaming (eval) and all-at-once (eval-all) evaluators. It also clarifies ownership by keeping ReadDocuments caller-owned (i.e., it no longer closes the provided reader).
Changes:
- Change
readStreamto return an ownedio.ReadCloser(wrapping*os.Filebehind a closable reader) and remove EOF-time closing fromreadDocuments. - Introduce per-file helpers that
defer SafelyCloseReader(...)so each input is closed promptly on both success and error paths. - Add regression coverage: a Go unit test asserting
ReadDocumentsdoes not close caller-owned readers, plus an acceptance test that runs under a lowulimit -nfor bothevalandeval-all.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/yqlib/utils.go | Makes readStream return an owned io.ReadCloser and removes implicit closing from readDocuments to enforce caller ownership. |
| pkg/yqlib/stream_evaluator.go | Closes each per-file reader via a helper that defers SafelyCloseReader, preventing FD accumulation in eval. |
| pkg/yqlib/all_at_once_evaluator.go | Adds a helper that opens+reads+closes per file, preventing FD accumulation in eval-all. |
| pkg/yqlib/file_utils.go | Extends SafelyCloseReader to close any io.Closer, enabling safe closure of wrapped readers. |
| pkg/yqlib/front_matter_test.go | Updates test to explicitly close the reader it opened (since readDocuments no longer closes). |
| pkg/yqlib/all_at_once_evaluator_test.go | Adds a unit test verifying ReadDocuments leaves caller-owned readers open. |
| acceptance_tests/file-descriptors.sh | Adds a low-file-descriptor acceptance test for both eval and eval-all. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
io.ReadCloserfromreadStreamReadDocumentscaller-ownedevalandeval-allRoot cause
readStreamwrapped each opened*os.Filein a*bufio.Reader. The evaluator cleanup type-switch therefore could not reach the underlying file, so descriptors accumulated until garbage collection or process exit.The new per-file helpers defer closing the owned reader, covering both successful evaluation and error returns without retaining descriptors for the full multi-file run.
User impact
Multi-file commands now work under low file-descriptor limits instead of failing partway through with
too many open files.Fixes #2796.
Validation
bash acceptance_tests/file-descriptors.shprlimit --nofile=32:32reproduction for bothevalandeval-allbash scripts/acceptance.shTestTomlScenariosfailure skippedgo test -tags goinstall -run TestGoInstallCompatibility .make test: formatting, spelling, gosec, and golangci-lint passed; the Go test phase hit the same TOML error-message mismatch reproduced on cleanorigin/master