Skip to content

Commit 290368d

Browse files
authored
Merge pull request #63 from eed3si9n/wip/reference-counting
Fix reference counting
2 parents 1401fbb + 4b3c88b commit 290368d

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/main/java/org/scalasbt/ipcsocket/UnixDomainSocket.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,13 @@ public void close() throws IOException {
124124

125125
private class UnixDomainSocketInputStream extends InputStream {
126126
public int available() throws IOException {
127+
int socketFd = fd.acquire();
127128
try {
128-
return provider.available(fd.acquire());
129+
return provider.available(socketFd);
129130
} catch (final NativeErrorException e) {
130131
throw new IOException(e.getMessage(), e);
132+
} finally {
133+
fd.release();
131134
}
132135
}
133136

@@ -147,17 +150,22 @@ public int read(byte[] b, int off, int len) throws IOException {
147150
if (len == 0) {
148151
return 0;
149152
}
150-
int result = doRead(b, off, len);
151-
if (result == 0) {
152-
try {
153-
provider.close(fd.acquire());
154-
} catch (final NativeErrorException e) {
155-
throw new IOException(
156-
"Error closing " + fd.acquire() + (path == null ? "" : " for " + path));
153+
int socketFd = fd.acquire();
154+
try {
155+
int result = doRead(b, off, len);
156+
if (result == 0) {
157+
try {
158+
provider.close(socketFd);
159+
} catch (final NativeErrorException e) {
160+
throw new IOException(
161+
"Error closing " + socketFd + (path == null ? "" : " for " + path));
162+
}
163+
result = -1;
157164
}
158-
result = -1;
165+
return result;
166+
} finally {
167+
fd.release();
159168
}
160-
return result;
161169
}
162170

163171
private int doRead(byte[] buf, int offset, int len) throws IOException {

0 commit comments

Comments
 (0)