Skip to content

Commit

Permalink
Fix not connected/bound error handling for Channels
Browse files Browse the repository at this point in the history
On Linux, we may need to cast InvalidArgumentSocketException to
NotYetBoundException/NotYetConnectedException.
  • Loading branch information
kohlschuetter committed Jul 8, 2024
1 parent 44acf55 commit 8125664
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*/
package org.newsclub.net.unix;

import java.io.Closeable;
import java.io.IOException;
import java.nio.channels.DatagramChannel;
import java.nio.channels.InterruptibleChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
Expand All @@ -33,7 +33,8 @@
* @see AFServerSocketChannel
* @see AFDatagramChannel
*/
public interface AFSomeSocketChannel extends Closeable, FileDescriptorAccess, AFSomeSocketThing {
public interface AFSomeSocketChannel extends InterruptibleChannel, FileDescriptorAccess,
AFSomeSocketThing {
/**
* Checks if the channel is configured blocking. The result may be cached, and therefore not
* invoke native code to check if the underlying socket is actually configured that way.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import java.nio.channels.NotYetBoundException;
import java.nio.channels.NotYetConnectedException;
import java.nio.channels.spi.AbstractInterruptibleChannel;

import org.eclipse.jdt.annotation.NonNull;
import java.util.Objects;

/**
* Helper methods when working with {@link AbstractInterruptibleChannel} subclasses.
Expand Down Expand Up @@ -53,8 +52,8 @@ interface EndMethod {
* @param exception An optional exception that was caught in the try-catch-finally block.
* @throws AsynchronousCloseException on error.
*/
static void endInterruptable(AbstractInterruptibleChannel channel, EndMethod end,
boolean complete, Exception exception) throws AsynchronousCloseException {
static void endInterruptable(AFSomeSocketChannel channel, EndMethod end, boolean complete,
Exception exception) throws AsynchronousCloseException {
if (!complete) {
if (exception instanceof ClosedChannelException) {
// we already have caught a valid exception; we don't need to throw one from within "end"
Expand All @@ -68,8 +67,8 @@ static void endInterruptable(AbstractInterruptibleChannel channel, EndMethod end
}
}

private static <T extends Exception> T closeAndThrow(AbstractInterruptibleChannel channel,
@NonNull T exc) {
private static <T extends Exception> T closeAndThrow(AFSomeSocketChannel channel, T exc) {
Objects.requireNonNull(exc);
if (channel.isOpen()) {
try {
channel.close();
Expand Down Expand Up @@ -99,14 +98,27 @@ static IOException ioExceptionOrThrowRuntimeException(Exception exception) {
* @param e The exception
* @return The exception.
*/
@SuppressWarnings("null")
static Exception handleException(AbstractInterruptibleChannel channel, IOException e) {
@SuppressWarnings("PMD.CognitiveComplexity")
static Exception handleException(AFSomeSocketChannel channel, IOException e) {
if (e instanceof NotConnectedSocketException) {
return (NotYetConnectedException) new NotYetConnectedException().initCause(e);
} else if (e instanceof NotBoundSocketException) {
return (NotYetBoundException) new NotYetBoundException().initCause(e);
}

if (e instanceof InvalidArgumentSocketException) {
if (channel instanceof AFServerSocketChannel<?>) {
AFServerSocketChannel<?> sc = (AFServerSocketChannel<?>) channel;
if (!sc.socket().isBound()) {
return (NotYetBoundException) new NotYetBoundException().initCause(e);
}
} else if (channel instanceof AFSocketChannel<?>) {
if (!((AFSocketChannel<?>) channel).socket().isConnected()) {
return (NotYetConnectedException) new NotYetConnectedException().initCause(e);
}
}
}

if (e instanceof SocketClosedException || e instanceof ClosedChannelException
|| e instanceof BrokenPipeSocketException) {
Thread t = Thread.currentThread();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,4 @@ protected void cleanupTestBindNull(ServerSocketChannel sc, SocketAddress addr)
Path p = Paths.get(addr.toString());
Files.delete(p);
}

}

0 comments on commit 8125664

Please sign in to comment.