fix: close CloseableIterable owners in Iceberg read paths#5149
Merged
Yicong-Huang merged 1 commit intoMay 23, 2026
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5149 +/- ##
============================================
+ Coverage 43.36% 43.39% +0.02%
+ Complexity 2217 2214 -3
============================================
Files 1049 1049
Lines 40560 40569 +9
Branches 4322 4323 +1
============================================
+ Hits 17589 17605 +16
+ Misses 21883 21871 -12
- Partials 1088 1093 +5
*This pull request uses carry forward flags. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
f3e4b9b to
a6911da
Compare
Yicong-Huang
requested changes
May 22, 2026
Contributor
Yicong-Huang
left a comment
There was a problem hiding this comment.
Left comments in line. I think the direction is correct, I like the idea of using ClosableIterator! but the implementation can be improved.
Also please make sure coverage is filled.
3b474c2 to
03612c8
Compare
03612c8 to
8c51c3a
Compare
`IcebergUtil.readDataFileAsIterator` and five sibling sites in `IcebergDocument` shared the same anti-pattern: `closeableIterable.iterator().asScala` returned a bare Scala iterator with no reference to the parent `CloseableIterable`. Under `S3FileIO` each call leaked one `S3InputStream` (kept open until GC) plus one slot of the AWS SDK's default 50-slot Apache HTTP connection pool; after ~50 reads any new S3 read blocked indefinitely on `acquireConnection` until JVM restart. Introduce `CloseableScalaIterator[T]` (`Iterator[T] with AutoCloseable`, idempotent `close()`) that wraps a `CloseableIterable[T]` and propagates `close()` to the parent. Change `readDataFileAsIterator` to return this wrapper. Update the `IcebergDocument` read iterator to track the close handle in a sibling `AutoCloseable` field (needed because `Iterator.drop(n)` returns a bare iterator that loses the wrapper type) and close it on file-switch, on exhaustion, and on caller-imposed `until` cap. Wrap the four eagerly-consumed `planFiles()` call sites (`getCount`, `seekToUsableFile`, `getTableStatistics`, `asInputStream`) in `Using.resource` so the metadata-side `CloseableIterable<FileScanTask>` is released promptly. Known limitation out of scope here: if a caller of `IcebergDocument.get()` / `getRange()` / `getAfter()` stops iterating before `hasNext` returns `false`, the LAST file's wrapper still leaks until GC. Fixing that requires changing the public `Iterator[T]` return type on `VirtualDocument` to `Iterator[T] with AutoCloseable` and updating every caller — best done as a separate refactor. Closes apache#5143. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
8c51c3a to
31cbdff
Compare
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.
What changes were proposed in this PR?
Fixes a resource leak in
IcebergUtil.readDataFileAsIteratorand five sibling sites inIcebergDocumentthat share the same anti-pattern:The bare
Iteratorreturned to callers held no reference to its parentCloseableIterable, so the parent could never be closed. UnderS3FileIO:S3InputStream(kept open until GC because nothing in the call graph could close it).ApacheHttpClientconnection pool (default 50; texera did not override).acquireConnectionuntil JVM restart.This PR:
CloseableScalaIterator[T](Iterator[T] with AutoCloseable, idempotentclose()) inIcebergUtil, which wraps aCloseableIterable[T]and propagatesclose()to the parent.IcebergUtil.readDataFileAsIteratorto returnCloseableScalaIterator[Record]instead of bareIterator[Record]. Callers must now close it (e.g. viaUsing.resource).IcebergDocument's read iterator to track the close handle in a siblingAutoCloseablefield (currentRecordIteratorCloser) and close it on file-switch, on exhaustion, and on caller-imposeduntilcap. The sibling field is necessary becauseIterator.drop(n)returns a bare iterator that loses the wrapper type.planFiles()call sites —getCount,seekToUsableFile,getTableStatistics,asInputStream— inUsing.resourceso the metadata-sideCloseableIterable<FileScanTask>is closed promptly.Known limitation (out of scope here): if a caller of
IcebergDocument.get()/getRange()/getAfter()stops iterating beforehasNextreturnsfalse(e.g. throws mid-loop, or calls.take(n)and then drops the result), the LAST file'sCloseableScalaIteratorwill leak until JVM GC. Fixing this requires changing the publicIterator[T]return type onVirtualDocumenttoIterator[T] with AutoCloseableand updating all callers — best done as a separate refactor.Any related issues, documentation, discussions?
Closes #5143.
How was this PR tested?
IcebergUtilLeakSpec(2 cases): validates thatCloseableScalaIterator(a) closes its parentCloseableIterablewhen used insideUsing.resource, and (b) is idempotent under repeatedclose()calls.IcebergUtilSpec: 14/14IcebergUtilLeakSpec: 2/2 (new)IcebergDocumentSpec: 18/18 (exercises the modified read iterator's close-on-reassign / close-on-exhaustion paths against real Iceberg infrastructure)IcebergTableStatsSpec: 12/12 (exercisesgetTableStatisticswith the newUsing.resourcewrap)IcebergDocumentConsoleMessagesSpec: 1/1Run locally:
Result:
47 succeeded, 0 failed.Was this PR authored or co-authored using generative AI tooling?
Generated-by: Claude Code (claude-opus-4-7)