Skip to content
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

Issue 21 Can't interrupt blocking accept #25

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
9 changes: 8 additions & 1 deletion src/main/java/jnr/unixsocket/UnixServerSocketChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ public UnixSocketChannel accept() throws IOException {
SockAddrUnix addr = remote.getStruct();
IntByReference len = new IntByReference(addr.getMaximumLength());

int clientfd = Native.accept(getFD(), addr, len);
int clientfd=-1;
try {
begin();
clientfd = Native.accept(getFD(), addr, len);
}
finally {
end(clientfd>=0);
}

if (clientfd < 0) {
if (isBlocking()) {
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/jnr/unixsocket/AcceptInterruptTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package jnr.unixsocket;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import jnr.unixsocket.UnixServerSocketChannel;
import jnr.unixsocket.UnixSocketAddress;
import junit.framework.Assert;

import org.junit.Test;

public class AcceptInterruptTest
{
@Test
public void testAcceptCloseInterrupt() throws Exception
{
File file = File.createTempFile("test", ".sock");
file.delete();
file.deleteOnExit();

final UnixServerSocketChannel channel = UnixServerSocketChannel.open();
channel.socket().bind(new UnixSocketAddress(file));

final AtomicBoolean run = new AtomicBoolean(true);
final CountDownLatch start = new CountDownLatch(1);
final CountDownLatch complete = new CountDownLatch(1);
Thread accept = new Acceptor(complete, start, channel, run);

// Start accepting thread
accept.setDaemon(true);
accept.start();
Assert.assertTrue(start.await(5,TimeUnit.SECONDS));

// Mark as no longer running
run.set(false);

// Close and Interrupt
channel.close();
accept.interrupt();
Assert.assertTrue(complete.await(5,TimeUnit.SECONDS));
}

@Test
public void testAcceptInterrupt() throws Exception
{
File file = File.createTempFile("test", ".sock");
file.delete();
file.deleteOnExit();

final UnixServerSocketChannel channel = UnixServerSocketChannel.open();
channel.socket().bind(new UnixSocketAddress(file));

final AtomicBoolean run = new AtomicBoolean(true);
final CountDownLatch start = new CountDownLatch(1);
final CountDownLatch complete = new CountDownLatch(1);
Thread accept = new Acceptor(complete, start, channel, run);

// Start accepting thread
accept.setDaemon(true);
accept.start();
Assert.assertTrue(start.await(5,TimeUnit.SECONDS));

// Mark as no longer running
run.set(false);

accept.interrupt();
Assert.assertTrue(complete.await(5,TimeUnit.SECONDS));

}


private final class Acceptor extends Thread {
private final CountDownLatch complete;
private final CountDownLatch start;
private final UnixServerSocketChannel channel;
private final AtomicBoolean run;

private Acceptor(CountDownLatch complete, CountDownLatch start, UnixServerSocketChannel channel,
AtomicBoolean run) {
this.complete = complete;
this.start = start;
this.channel = channel;
this.run = run;
}

@Override public void run()
{
try
{
while(run.get())
{
if (start.getCount()>0)
start.countDown();
try
{
channel.accept();
System.err.println("accepted");
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
System.err.println("finally");
}
}
}
finally
{
complete.countDown();
}
}
}
}