Skip to content

Commit

Permalink
native: Workaround for MonitorEnter with pending exception
Browse files Browse the repository at this point in the history
Android doesn't like it when we call MonitorEnter with an exception
pending.
  • Loading branch information
kohlschuetter committed Jul 8, 2024
1 parent 7e833f7 commit d8c8be4
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions junixsocket-native/src/main/c/filedescriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ int _closeFd(JNIEnv * env, jobject fd, int handle)
return ret;
}

// Android doesn't like it when we call MonitorEnter on a pending exception.
// Temporarily hold on to that exception, and throw again afterwards.
jthrowable throwable = (*env)->ExceptionOccurred(env);
if(throwable != NULL) {
(*env)->ExceptionClear(env);
}

(*env)->MonitorEnter(env, fd);
int fdHandle = _getFD(env, fd);
_initFD(env, fd, -1);
Expand All @@ -243,6 +250,10 @@ int _closeFd(JNIEnv * env, jobject fd, int handle)
#endif
(*env)->MonitorExit(env, fd);

if(throwable != NULL) {
(*env)->Throw(env, throwable);
}

#if defined(_WIN32)
jboolean isSocket;
if(handleWin > 0) {
Expand Down

0 comments on commit d8c8be4

Please # to comment.