Skip to content

Close DFSInputStream on exception in CachableBlockFile.getBCFile#6377

Open
dlmarion wants to merge 3 commits into
apache:2.1from
dlmarion:close-dfs-input-stream
Open

Close DFSInputStream on exception in CachableBlockFile.getBCFile#6377
dlmarion wants to merge 3 commits into
apache:2.1from
dlmarion:close-dfs-input-stream

Conversation

@dlmarion
Copy link
Copy Markdown
Contributor

A RateLimitedInputStream is created from the supplied InputStream, which in most cases is a DFSInputStream. However, when BCFile.Reader throws an exception the RateLimitedInputStream is not closed leaving the related DFSInputStream open.

A RateLimitedInputStream is created from the supplied InputStream,
which in most cases is a DFSInputStream. However, when BCFile.Reader
throws an exception the RateLimitedInputStream is not closed leaving
the related DFSInputStream open.
@dlmarion dlmarion added this to the 2.1.5 milestone May 18, 2026
@dlmarion dlmarion self-assigned this May 18, 2026
@DomGarguilo
Copy link
Copy Markdown
Member

try-with-resources wont work here since we need to return the stream but what if we used a try-finally block instead? And add a new boolean to track whether the stream was successfully returned or not. Then in the finally block if it was not successfully returned that means we need to close it. That way in all failure cases, even any that we might not expect here, we can properly close the stream and dont need to duplicate the close logic.

RateLimitedInputStream fsIn =
new RateLimitedInputStream((InputStream & Seekable) inputSupplier.get(), readLimiter);
BCFile.Reader tmpReader = null;
byte[] serializedMetadata = cachedMetadataSupplier.get();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If cachedMetadataSupplier.get() can throw, that should probably be moved into the try block too. Otherwise this PR LGTM

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I captured that line in the try block in dd8515c

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.

2 participants