Skip to content

Fix directory stream leak in FileCache.restoreFromDirectory - #22994

Open
magic-peach wants to merge 1 commit into
opensearch-project:mainfrom
magic-peach:fix/filecache-restore-directory-stream-leak
Open

magic-peach wants to merge 1 commit into
opensearch-project:mainfrom
magic-peach:fix/filecache-restore-directory-stream-leak

Conversation

@magic-peach

Copy link
Copy Markdown
Contributor

Description

FileCache.restoreFromDirectory rebuilds 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 with Stream.concat(...).flatMap(dir -> Files.list(dir))...forEach(...), one long expression with no try with resources anywhere in it. Files.list() opens a DirectoryStream under 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.list stream 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 FileCacheTests suite (27 tests, all passing).

Related Issues

Resolves #
No existing issue; found by inspection while reviewing FileCache's startup path.

Check List

  • Functionality includes testing.
  • API changes companion pull request created, if applicable.
  • Public documentation issue/PR created, if applicable.

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.

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>
@github-actions

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🧪 No relevant tests
🔒 No security concerns identified
✅ No TODO sections
🔀 No multiple PR themes
⚡ No major issues detected

@github-actions

Copy link
Copy Markdown
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?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant