Skip to content

Use try-with-resources in AbstractTransporter#1947

Open
Aayush10016 wants to merge 1 commit into
apache:masterfrom
Aayush10016:use-try-with-resources-abstract-transporter
Open

Use try-with-resources in AbstractTransporter#1947
Aayush10016 wants to merge 1 commit into
apache:masterfrom
Aayush10016:use-try-with-resources-abstract-transporter

Conversation

@Aayush10016

Copy link
Copy Markdown

Summary

  • Refactor AbstractTransporter stream handling to use try-with-resources for the close-on-completion paths.
  • Preserve the existing close=false behavior by leaving download input streams open and flushing upload output streams.
  • Add focused tests for the utilGet and utilPut close/flush contracts.

Closes #1521.

Verification

  • mvn -pl maven-resolver-spi -am test with Temurin JDK 21.0.11

Refactor stream handling in utilGet and utilPut while preserving the existing close and flush contracts. Add tests for the close=false and close=true paths.

Closes apache#1521

Generated-by: Codex
@cstamas

cstamas commented Jul 6, 2026

Copy link
Copy Markdown
Member

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review Summary

Nice cleanup, @Aayush10016! The try-with-resources conversion in AbstractTransporter is correct and actually improves exception handling over the original code.

What's good

In the original code, is.close() (in utilGet) and os.close() (in utilPut) were called in plain finally blocks, meaning their exceptions could replace the primary exception from the try block. The try-with-resources approach properly adds close exceptions as suppressed exceptions instead — this is the correct Java behavior.

The tests are well-focused and verify the close/flush contract for both close=true and close=false branches.

Minor observations (non-blocking)

  • Code duplication: The transportStarted + copy logic is duplicated in both branches of the if/else for each method. An alternative is to extract the common body into a private helper method, though the current form with 2-line duplication is clear and defensible.

  • Exception-path tests: The tests cover the happy path well, but don't test the exception path — e.g., verifying streams are still properly closed when copy() throws. Since improved exception handling is a key benefit of this refactor, an exception-path test would strengthen confidence. This is a nice-to-have, not a blocker.

  • Pre-existing: In the utilPut close=false branch, os.flush() remains in a plain finally block, meaning if copy() throws and then flush() also throws, the flush exception replaces the original. This is unchanged by this PR and could be addressed in a follow-up.

Overall this is a solid improvement. The contributor checklist items noted in the maintainer comment should still be addressed.


This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of Guillaume Nodet

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.

Use try-with-resources

3 participants