Skip to content

Commit

Permalink
Use common event loop interrupt on hardware error.
Browse files Browse the repository at this point in the history
Just cleaning up the event info struct isn't enough: one some platforms,
there's a “drain loop” thread which runs underneath the monitor thread
to watch for an empty output buffer. This thread needs to be stopped,
too, or it'll segfault and bring down the whole JVM after the event info
struct is freed.
  • Loading branch information
MrDOS authored and Claudia Pellegrino committed Feb 8, 2024
1 parent a7510de commit 4caae7b
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/main/c/src/SerialImp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4247,10 +4247,23 @@ JNIEXPORT void JNICALL RXTXPort(eventLoop)( JNIEnv *env, jobject jobj )
do {
if(RXTXPort(nativeavailable)( env, jobj )<0){
report("eventLoop: Hardware Missing\n");
finalize_threads( &eis );
finalize_event_info_struct( &eis );
LEAVE("eventLoop:error");
return;
/* The hardware is gone, so we need to stop the monitor thread.
* Conveniently, this function is supposed to be an infinite
* loop, so once we return from it to Java-land, the thread will
* close up shop. However, that also means that we need to make
* sure any native cleanup happens before we return, or else it
* will never run. We'll trigger that by the usual method to
* ensure platform-specific cleanup happens (e.g., killing the
* drain loop). That will also set `eis.closing`, so the
* function will return as usual in the next block.
*
* Note that `nativeavailable()` has thrown an exception at this
* point, so we need to be careful about calling further JNI
* functions. Conventional wisdom states that JNI functions
* called when an exception is pending “may lead to unexpected
* results”. Empirically, this seems to work okay; I suspect, at
* worst, the functions might return an error. */
RXTXPort(interruptEventLoop)(env, jobj);
}
/* nothing goes between this call and select */
if( eis.closing )
Expand Down

0 comments on commit 4caae7b

Please sign in to comment.