title: CloseHandle method for serial port blocking indefinitely when unplugging device
body: <p>I am currently facing issues with the <code>CloseHandle</code> method, trying to close a virtual serial port COM in C++. I use the methods <code>CreateFile</code> and <code>WriteFile</code> to send messages to my device. When my application closes, I call the <code>CloseHandle</code> method in my class destructor. Everything works fine when I close properly my application.</p>
  
  <p>My device is plugged in the PC using a RJ45 to USB adaptor, which is plugged into the USB port of my computer. Then, I use a FTDI driver to communicate on a virtual serial COM port.</p>
  
  <p>The issue occurs when I unplug the RJ45 cable from the adaptor: in this case, the <code>ReadFile</code> function correctly returns that it cannot communicate with the device; then, I want to close my application, calling the <code>CloseHandle</code> method. In this case, the <code>CloseHandle</code> method is blocking indefinitely and my application do not close. However, when I re-plugg the RJ45 cable, the <code>CloseHandle</code> method finishes and my application is able to close.</p>
  
  <p>Something interesting is that I do not have the same issue when I unplug directly the USB port: in this case, the <code>CloseHandle</code> method works fine and my application closes.</p>
  
  <p>Is there something in the communication buffer to clear in that case? (I have heard about the <code>EscapeCommFunction</code> method with the DTR signal or RTS signal but I tried this without success!) Why is <code>CloseHandle</code> blocking in this case? And why don't I have the same issue when I unplugg the USB cable?</p>
  
  <p>I am not an expert in USB/serial communications, so please excuse the vocabulary.</p>