Skip to content

Commit

Permalink
native: Fix handling of non-blocking receive
Browse files Browse the repository at this point in the history
Under some circumstances, we would erroneously throw a
ClosedChannelException.

Rework the code to not throw an exception unless necessary, and throw
the proper exception based on the error code (ClosedChannelExceptions
are now handled separately in Java code).

#158
  • Loading branch information
kohlschuetter committed Jul 1, 2024
1 parent 44cdadb commit 3942271
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions junixsocket-native/src/main/c/receive.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,14 @@ JNIEXPORT jint JNICALL Java_org_newsclub_net_unix_NativeUnixSocket_receive
}

if(checkNonBlocking0(handle, theError, opt)) {
theError = errno;
// no data on non-blocking socket, or terminated connection?
if(count == 0 && theError != 0) {
_throwException(env, kExceptionClosedChannelException, NULL);
} else if(theError == 0 || theError == EAGAIN || theError == EWOULDBLOCK || theError == ETIMEDOUT
if(theError == 0 || theError == EAGAIN || theError == EWOULDBLOCK || theError == ETIMEDOUT
#if defined(_WIN32)
|| theError == WSAETIMEDOUT
#endif
|| theError == EINTR) {
|| theError == EINTR) {
// just return 0
} else {
_throwErrnumException(env, errno, fd);
_throwErrnumException(env, theError, fd);
}
return 0;
} else if(theError == EWOULDBLOCK) {
Expand All @@ -395,7 +391,6 @@ JNIEXPORT jint JNICALL Java_org_newsclub_net_unix_NativeUnixSocket_receive
_throwErrnumException(env, theError, fd);
}
}
count = 0;

return (jint)count;
return 0;
}

0 comments on commit 3942271

Please sign in to comment.