I am having an issue with windows closeHandle((Handle)port) function in our .dll. it is not returning. The issue happens every few hours.
The java application using the .dll connects, reads and writes data. The last method call java application does is to write the data and release method is called IMMEDIATELY which calls closeHandle function in the JNI.
Is CloseHandle not returning because write is still happening? Is there a way to gracefully return to normal when the closeHandle hangs/ not return. Thanks in advance.
The code for the function which calls closeHandle is below.
{
HANDLE port;
jfieldID portFID = GetFieldId(env, jthis, "portnbr", "J");
if ( portFID == 0 ){
flog("ERROR: Java_com_hctsi_comm_Serial_close() portFID=0\n");
return;
}
port = (HANDLE)env->GetLongField(jthis, portFID);
if ( 0 != port ) {
flog("Java_com_hctsi_comm_Serial_close() port %d\n", port);
int count = 0;
while ( !CloseHandle((HANDLE)port) && count < 10 ) {
flog("ERROR Java_com_hctsi_comm_Serial_close() closeHandle failed with an error %d\n", count, port, GetLastError);
flog("ERROR Java_com_hctsi_comm_Serial_close() failed count %d port %d\n", count, port);
count++;
Sleep(1000);
}
env->SetLongField(jthis, portFID, (jlong)0);
}
CloseLog();
}
What I have tried:
I haven't tried anything, because it is hard to replicate on my local, but It Does happen in production intermittently.