title: CloseHandle((Handle) port) is not returning
body: <p>I am having an issue with windows closeHandle((Handle)port) function in our .dll. it is not returning. The issue happens every few hours.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>The code for the function which calls closeHandle is attached.</p>
  
  <pre><code>{
      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-&gt;GetLongField(jthis, portFID);
      if ( 0 != port ) {
          flog("Java_com_hctsi_comm_Serial_close() port %d\n", port);
          int count = 0;
          while ( !CloseHandle((HANDLE)port) &amp;&amp; count &lt; 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-&gt;SetLongField(jthis, portFID, (jlong)0);
      }
      CloseLog();
  } `
  
  The code to open a port is below
  
  `JNIEXPORT jint JNICALL Java_com_hctsi_comm_Serial_open(JNIEnv *env, jobject jthis, jstring jportName, jint baud, jbyte dataBits, jchar parity, jbyte stop, jchar flowin, jchar flowout, jchar rts, jchar cts, jchar dtr, jchar dsr, jint usTimeout)
  {
      int result;
      jboolean iscopy;
      HANDLE port;
  
     StartLog();
      const char *portName = env-&gt;GetStringUTFChars(jportName, &amp;iscopy);
      flog("serial port open %s baud %d data %d parity %c stop %d\n", portName, baud, dataBits, parity, stop);
  
  
  // ================ DEBUG ======================================================
      /*
      {
          jclass thisClass;
          if (! (thisClass = env-&gt;GetObjectClass(jthis))) {
              flog("GetFieldId thisClass=NULL\n");
              return (0);
          }
          jmethodID getComPort = env-&gt;JNIEnv_::GetMethodID(thisClass, "getComPort", "()Ljava/lang/String;");
          jfieldID  tempFID = GetFieldId(env, jthis, "comPort", "Ljava/lang/String;");
          flog("   getComPort=%d\n",getComPort);
          flog("   comPort=%d\n",tempFID);
      }
      */
  // ================ DEBUG ======================================================
  
      jfieldID portFID = GetFieldId(env, jthis, "portnbr", "J");
      flog("   portFID=%d\n",portFID);
      if (portFID == 0) {
          flog("ERROR: Java_com_hctsi_comm_Serial_open() portFID=0\n");
          return (jint)-1;
      }
      port = (HANDLE)env-&gt;GetLongField(jthis, portFID);
      if (0 != port) {
          flog("ERROR: Java_com_hctsi_comm_Serial_open() port %d\n", port);
          CloseHandle((HANDLE)port);
      }
      port = (HANDLE)serOpen((char *)portName, baud, dataBits, parity, stop, flowin, flowout, rts, cts, dtr, dsr, usTimeout);
      if (port != NULL) {
          result = (jint)1;
      } else {
          errCode = GetLastError();
          flog("Java_com_hctsi_comm_Serial_open() port %d, errorcode=%d\n", port, errCode);
          result = (jint)-1;
      }
      flog("Java_com_hctsi_comm_Serial_open() port %d\n", port);
      env-&gt;SetLongField(jthis, portFID, (jlong)port);
      return(jint)result;
  } `
  
  
  </code></pre>