Skip to content

Add try-with-resources to annotation processor #45193

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

Merged

Conversation

alzimmermsft
Copy link
Member

Description

Changes annotation processor to use a try-with-resources block when the Response<BinaryData> will be closed. This ensures that if any errors occur before the network response is closed that resources won't be leaked.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@azure-sdk
Copy link
Collaborator

API change check

API changes are not detected in this pull request.

Copy link
Member

@vcolin7 vcolin7 left a comment

Choose a reason for hiding this comment

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

General question: is there was a particular reason you chose to use try-with-resources in some tests and to explicitly call close() in others?

@alzimmermsft alzimmermsft linked an issue May 5, 2025 that may be closed by this pull request
@alzimmermsft alzimmermsft requested a review from vcolin7 May 5, 2025 13:49
Copy link
Member

@vcolin7 vcolin7 left a comment

Choose a reason for hiding this comment

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

LGTM. I would still like to know the reason why some changes in tests use try-with-resources and others do not. Is it because of the return types?

@alzimmermsft
Copy link
Member Author

LGTM. I would still like to know the reason why some changes in tests use try-with-resources and others do not. Is it because of the return types?

Yep! It's all about the return type.

If the return is something that fully reads the network response it should use try-with-resource. So that's byte[], String, Void, and a deserialized T type. void we don't as some compilers will complain about either an empty try-with-resource block or unused try-with-resource value. InputStream and BinaryData are left to those types to be closed themselves to close the network response.

@alzimmermsft alzimmermsft merged commit 80e328f into Azure:main May 5, 2025
57 checks passed
@alzimmermsft alzimmermsft deleted the AzCC_AnnotationProcessorTryWithResources branch May 5, 2025 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use try-finally for cleaning up resources
3 participants