Skip to content

Commit

Permalink
Fix resource leak at socket close
Browse files Browse the repository at this point in the history
  • Loading branch information
miniway committed Jul 14, 2015
1 parent 82bbdf0 commit 02d3fa1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/main/java/zmq/Signaler.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public class Signaler
implements Closeable
{
// Underlying write & read file descriptor.
private Pipe.SinkChannel w;
private Pipe.SourceChannel r;
private Selector selector;
private final Pipe.SinkChannel w;
private final Pipe.SourceChannel r;
private final Selector selector;

// Selector.selectNow at every sending message doesn't show enough performance
private final AtomicInteger wcursor = new AtomicInteger(0);
Expand All @@ -48,7 +48,16 @@ public class Signaler
public Signaler()
{
// Create the socketpair for signaling.
makeFdPair();
Pipe pipe;

try {
pipe = Pipe.open();
}
catch (IOException e) {
throw new ZError.IOException(e);
}
r = pipe.source();
w = pipe.sink();

// Set both fds to non-blocking mode.
try {
Expand All @@ -72,25 +81,28 @@ public Signaler()
@Override
public void close() throws IOException
{
r.close();
w.close();
selector.close();
}

// Creates a pair of filedescriptors that will be used
// to pass the signals.
private void makeFdPair()
{
Pipe pipe;

IOException exception = null;
try {
pipe = Pipe.open();
r.close();
}
catch (IOException e) {
throw new ZError.IOException(e);
exception = e;
}
try {
w.close();
}
catch (IOException e) {
exception = e;
}
try {
selector.close();
}
catch (IOException e) {
exception = e;
}
if (exception != null) {
throw exception;
}
r = pipe.source();
w = pipe.sink();
}

public SelectableChannel getFd()
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/zmq/SocketBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package zmq;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.SelectableChannel;
Expand Down Expand Up @@ -159,6 +160,12 @@ public static SocketBase create(int type, Ctx parent, int tid, int sid)

public void destroy()
{
try {
mailbox.close();
}
catch (IOException ignore) {
}

stopMonitor();
assert (destroyed);
}
Expand Down

0 comments on commit 02d3fa1

Please sign in to comment.