This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

cygwin source-patch fixing deadlock while writing to serial port


I found a potential deadlock while writing to a serial port (e.g. /dev/com1) that has been opened as O_RDWR. The deadlock occurs from time to time (not sure about exact conditions) when I write to that port, while there is data coming in (e.g. from an external device) and I do not read away that data fast enough from the port.

I did provide a test case a while ago in http://sources.redhat.com/ml/cygwin/2003-03/msg01529.html. I digged into the issue some more now and found that the executing thread got sometimes deadlocked in fhandler_serial::raw_write(). It basically ends up in a for(;;) loop and just never hits the break;

The applied patch adds a safety exit to that for(;;) loop.
This fixes the testcase referenced above.

This might not be the last problem lingering in the serial access code (there are some FIXME tokens still around ...), but it is definitely an improvement for me. I thought I'd share that with you.

;Henning

--
H. Henning Schmidt
email: henning@hhschmidt.de
phone: +49 (0) 6155 / 899 283
fax:   +49 (0) 6155 / 899 284

*** cygwin-1.5.5-1/winsup/cygwin/fhandler_serial.cc	Sat Jun 21 02:12:35 2003
--- cygwin-1.5.5-1.corrected/winsup/cygwin/fhandler_serial.cc	Mon Jan 19 17:32:04 2004
***************
*** 153,173 ****
  int
  fhandler_serial::raw_write (const void *ptr, size_t len)
  {
!   DWORD bytes_written;
    OVERLAPPED write_status;
  
    memset (&write_status, 0, sizeof (write_status));
    write_status.hEvent = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
    ProtectHandle (write_status.hEvent);
  
!   for (;;)
      {
       if (WriteFile (get_handle (), ptr, len, &bytes_written, &write_status))
 	break;
  
        switch (GetLastError ())
  	{
  	case ERROR_OPERATION_ABORTED:
  	  continue;
  	case ERROR_IO_PENDING:
  	  break;
--- 153,174 ----
  int
  fhandler_serial::raw_write (const void *ptr, size_t len)
  {
!   DWORD bytes_written = 0;
    OVERLAPPED write_status;
  
    memset (&write_status, 0, sizeof (write_status));
    write_status.hEvent = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
    ProtectHandle (write_status.hEvent);
  
!   for (int prevent_deadlock=0 ; prevent_deadlock<10 ; prevent_deadlock++)
      {
       if (WriteFile (get_handle (), ptr, len, &bytes_written, &write_status))
 	break;
  
        switch (GetLastError ())
  	{
  	case ERROR_OPERATION_ABORTED:
+ 	  bytes_written = 0;
  	  continue;
  	case ERROR_IO_PENDING:
  	  break;

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]