Skip to content

Make Channel and Session implement AutoClosable#1052

Closed
chooklii wants to merge 1 commit into
mwiede:masterfrom
chooklii:feat/autoclosable
Closed

Make Channel and Session implement AutoClosable#1052
chooklii wants to merge 1 commit into
mwiede:masterfrom
chooklii:feat/autoclosable

Conversation

@chooklii
Copy link
Copy Markdown

I would argue that both Channel and Session should implement AutoCloseable to enable try-with-resources usage.

However, I'm not entirely sure if my suggested implementation is correct—especially for Channel. I can see a scenario where disconnect() should be the function called during the implicit close.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@norrisjeremy norrisjeremy left a comment

Choose a reason for hiding this comment

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

As outlined in my comments, changing the Session & Channel classes to implement AutoCloseable isn't correct.

*/

void close() {
public void close() {
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.

This isn't correct: the close() method here isn't meant for public consumption, as it does not fully perform all necessary cleanup: it is meant as an internal-use only method.
The correct way to dispose of a Channel object is to call disconnect().

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.

Also, I believe Channel may have similar semantics as Session, in that it may be re-usable after calling disconnect(), which disagrees with the semantics of AutoCloseable.

}

public void close() {
this.disconnect();
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.

I don't think introducing AutoCloseable semantics to a Session object is correct: there is an expectation that Session objects are re-usable after users call the disconnect() method, which doesn't agree with the semantics of AutoCloseable, which implys that after an object is fully disposed after calling the close() method.

@chooklii chooklii closed this May 20, 2026
@chooklii
Copy link
Copy Markdown
Author

I closed this PR. As I found this repo only through an PR of an co-worker. My PR was based on the assumption that a channel should be "closed" when disconnecting.
As this is not the case I can only agree with @norrisjeremy that if disconnecting is not equal to what a try-with would do when closing this should not be implemented this way.

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