Fix directory stream leak in FileCache.restoreFromDirectory - #22994
Open
magic-peach wants to merge 1 commit into
Open
magic-peach wants to merge 1 commit into
magic-peach wants to merge 1 commit into
Conversation
Files.list returns a Stream backed by a DirectoryStream that has to be closed explicitly to release its file descriptor. The flatMap chain here opened one of these streams per data path (local store location and indices folder) but never closed any of them, since the whole pipeline was a single expression with no try with resources block anywhere in it. Restructured the scan to open and close one Files.list stream per directory instead of flattening them into the outer stream, so each one is released as soon as its directory has been read. Behavior and exception messages are unchanged, verified against the existing FileCacheTests suite (27 tests, all passing). Signed-off-by: Akanksha Trehun <akankshatrehun@gmail.com>
magic-peach
requested review from
a team,
Bukhtawar,
alchemist51,
andrross,
ashking94,
cwperks,
dbwiddis,
gbbafna,
jed326,
kotwanikunal,
mch2,
msfroh,
owaiskazi19,
reta,
sachinpkale,
saratvemulapalli,
shwetathareja and
sohami
as code owners
September 10, 2026 04:59
Contributor
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
Contributor
|
❌ Gradle check result for 2a95cb9: QUEUE_TIMEOUT Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
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.
Description
FileCache.restoreFromDirectoryrebuilds the file cache from disk at node startup by scanning the local store location and indices folder under each file cache data path. It did this withStream.concat(...).flatMap(dir -> Files.list(dir))...forEach(...), one long expression with no try with resources anywhere in it.Files.list()opens aDirectoryStreamunder the hood that has to be closed explicitly to release its file descriptor, and flatMap does not close the inner streams it consumes, so every directory scanned during startup leaked one.This restructures the scan to iterate the candidate directories with a plain forEach and open one
Files.liststream per directory inside a try with resources block, so each directory's handle is released as soon as its contents have been read. Exception messages, put/decRef behavior, and the set of files processed are all unchanged.Verified against the existing
FileCacheTestssuite (27 tests, all passing).Related Issues
Resolves #
No existing issue; found by inspection while reviewing FileCache's startup path.
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.