Skip to content

Interrupt read/receive when UnixDatagramChannel is closed #107

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package jnr.unixsocket.impl;

import jnr.constants.platform.Shutdown;
import jnr.enxio.channels.Native;
import jnr.enxio.channels.NativeSelectableChannel;
import jnr.enxio.channels.NativeSelectorProvider;
Expand Down Expand Up @@ -52,6 +53,8 @@ public final int getFD() {

@Override
protected void implCloseSelectableChannel() throws IOException {
// Shutdown to interrupt any potentially blocked threads. This is necessary on Linux.
Native.shutdown(getFD(), SHUT_RD);
Native.close(common.getFD());
}

Expand Down Expand Up @@ -80,4 +83,5 @@ public long write(ByteBuffer[] srcs, int offset,
return common.write(srcs, offset, length);
}

private static final int SHUT_RD = Shutdown.SHUT_RD.intValue();
}
48 changes: 48 additions & 0 deletions src/test/java/jnr/unixsocket/UnixDatagramChannelTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package jnr.unixsocket;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;

import org.junit.Test;
Expand Down Expand Up @@ -66,4 +70,48 @@ public void testAbstractNamespace() throws Exception {
assertEquals("local socket path", ABSTRACT, ch.getLocalSocketAddress().path());
}

@Test
public void testInterruptRead() throws Exception {
int readTimeoutInMilliseconds = 5000;

UnixDatagramChannel ch = UnixDatagramChannel.open();
ch.bind(null);

CountDownLatch readStartLatch = new CountDownLatch(1);
AtomicReference<IOException> thrownOnThread = new AtomicReference<IOException>();

Runnable runnable = new Runnable() {
@Override
public void run() {
try {
readStartLatch.countDown();
ByteBuffer buffer = ByteBuffer.allocate(1 << 16);
ch.receive(buffer);
} catch (IOException e) {
if (!e.getMessage().equals("Bad file descriptor")) {
thrownOnThread.set(e);
}
}
}
};

Thread readThread = new Thread(runnable);

readThread.setDaemon(true);

long startTime = System.nanoTime();
readThread.start();
readStartLatch.await();
Thread.sleep(100); // Wait for the thread to call receive()
ch.close();
readThread.join();
long stopTime = System.nanoTime();

long duration = stopTime - startTime;
long durationInMilliseconds = duration / 1_000_000;

assertTrue("read() was not interrupted by close() before read() timed out", durationInMilliseconds < readTimeoutInMilliseconds);
assertEquals("read() threw an exception", null, thrownOnThread.get());
}

}