Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Throw ClosedChannelException when FastJ8SocketChannel.read() fails due to underlying channel being closed #215

Merged
merged 1 commit into from Oct 27, 2022

Conversation

nicktindall
Copy link
Contributor

@nicktindall nicktindall commented Oct 11, 2022

Fixes #214

The ClosedChannelException is handled specially in e.g. TcpEventHandler, whereas the generic IOException ends up being logged.

We could perhaps also fix this by checking before we attempt to read from the channel, but that would leave room for a race condition, and potentially re-introduce some of the slowness this class attempts to avoid.

This approach just changes the exception after the error has occurred.

@nicktindall nicktindall force-pushed the throw_correct_exception_when_channel_closed branch from 9c2aaa7 to 321713f Compare October 27, 2022 00:09
@nicktindall nicktindall force-pushed the throw_correct_exception_when_channel_closed branch from 321713f to 89bb981 Compare October 27, 2022 00:10
Copy link
Contributor

@JerryShea JerryShea left a comment

Choose a reason for hiding this comment

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

beautiful

@@ -47,7 +47,7 @@ public int read(ByteBuffer buf) throws IOException {
if (buf == null)
throw new NullPointerException();

if (isBlocking() || isClosed() || !IOTools.isDirectBuffer(buf))
if (isBlocking() || isClosed() || !IOTools.isDirectBuffer(buf) || !super.isOpen())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just means we don't attempt the "fast read" if we know the channel isn't open. The slow read behaves as you'd expect (throwing the ClosedChannelException when the channel is closed).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fast read throws an IOException from some com.sun package about an invalid file handle under those circumstances

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably still a chance we throw the IOException if the disconnect happens between the check and the read (though not sure if that can even happen), but it's substantially reduced.

@sonarcloud
Copy link

sonarcloud bot commented Oct 27, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

55.6% 55.6% Coverage
0.0% 0.0% Duplication

@nicktindall nicktindall merged commit 6556e4c into develop Oct 27, 2022
@nicktindall nicktindall deleted the throw_correct_exception_when_channel_closed branch October 27, 2022 00:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants