Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Update TableMetadataParser to ensure all streams closed #11220

Merged
merged 4 commits into from
Oct 26, 2024

Conversation

erik-grepr
Copy link
Contributor

@erik-grepr erik-grepr commented Sep 27, 2024

Minor try-with-resources tweak to fix an unclosed stream memory leak I encountered.

More specifically, this occurs when the file is Gzipped and file.newStream().read() throws an exception.

The GZIPInputStream constructor calls read on it's input to load the header.

see: https://stackoverflow.com/questions/12552863/correct-idiom-for-managing-multiple-chained-resources-in-try-with-resources-bloc

@github-actions github-actions bot added the core label Sep 27, 2024
@erik-grepr erik-grepr changed the title Update TableMetadataParser to close streams Core: Update TableMetadataParser to close streams Sep 27, 2024
generator.flush();
}
} else {
try (OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
Copy link
Member

Choose a reason for hiding this comment

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

I am not convinced we need the two code branches for this, nor that it's desired.
OutputStream::close must obey general Closeable::close contract which requries the implementation to be idempotent.

Thus, if we had

OutputStream stream = overwrite ? outputFile.createOrOverwrite() : outputFile.create();
    try (OutputStream ou = isGzip ? new GZIPOutputStream(stream) : stream;

we now should be able to:

try (OutputStream stream = overwrite ? outputFile.createOrOverwrite() : outputFile.create();
   OutputStream ou = isGzip ? new GZIPOutputStream(stream) : stream;

guaranteeing all streams to be closed, regardless at which point an exception is thrown.

Can you try that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated I think. Is there a gradle target to auto-format?

Copy link
Member

Choose a reason for hiding this comment

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

thanks!

Is there a gradle target to auto-format?

./gradlew -DallModules=true spotlessApply

( see also https://gist.github.com/findepi/da2ea85d095eaa0f3cfdcbb311ff59ed#reformat-code )

@findepi
Copy link
Member

findepi commented Oct 12, 2024

@erik-grepr can you please review the build results?

Copy link
Contributor

@singhpk234 singhpk234 left a comment

Choose a reason for hiding this comment

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

LGTM too !

@erik-grepr
Copy link
Contributor Author

Should be good to re-run the workflows.

@findepi findepi changed the title Core: Update TableMetadataParser to close streams Core: Update TableMetadataParser to ensure all streams closed Oct 26, 2024
@findepi findepi merged commit 2b55fef into apache:main Oct 26, 2024
49 checks passed
@findepi
Copy link
Member

findepi commented Oct 26, 2024

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants