treblent
Posts: 2
Joined: Fri Jun 18, 2010 4:14 pm

Write thread lock bug and fix

Thu Jul 08, 2010 4:06 pm

I found that a lock can occur if a Write is requested, and a sudden disconnect occurs the port can lock, and future writes are not possible.

This is because once Write starts, it calls the m_write_func() which then checks m_stop_write to see if the port has been disabled. If it has, it cancels the write and assumes that the port has closed. Unfortunately, if the write has just fired, it will be sitting waiting for m_written_ev, and once m_write_func() has completed, it will never set m_written_ev, and no future calls to Write will get through because the original is still waiting.

In m_write_func() I added a call to m_written.ev.Set() for when m_stop_write is true. This releases any Writes currently waiting. Note it's also important to reset m_written_ev in Write before calling m_write_func().

So...

Code: Select all

if(m_stop_write)
{
   // Thread ended (port closed)
   run_thread = false;
   m_written_ev.Set(); // ADD THE SET HERE
}
and here in Write()

Code: Select all

// Only one thread might be inside this function at a time.
Monitor.Enter(m_write_lock);
_threadEntries++;
int bytes_written = 0;

m_written_ev.Reset(); // ADD THE RESET HERE
m_check_port_enabled(true);
Hope this helps someone else.

WineGuru
Posts: 6
Joined: Tue Dec 19, 2006 3:57 am

RE: Write thread lock bug and fix

Tue Aug 10, 2010 11:51 pm

I've found this true also in VB6 using the ActiveX control. However once it locks I have no recourse but to end task.

SomeName
Posts: 1
Joined: Fri Sep 30, 2011 6:58 am

RE: Write thread lock bug and fix

Fri Sep 30, 2011 7:00 am

Hi treblent! How and where are you making these changes? It almost looks like you are changing the source to the SerialNet.DLL itself? If so, where did you get the source code? I am using this DLL in a VB.NET app and may be having the same problem with a USB-serial adapter is unplugged unexpectedly.

Thanks!

Return to “SerialTools”