Bug #46308 LoadPacket stuck in loop
Submitted: 20 Jul 2009 13:36 Modified: 5 Aug 2009 12:25
Reporter: Mark Logan Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.0.4 OS:Microsoft Windows (XP)
Assigned to: CPU Architecture:Any
Tags: deadlock, infinite loop, Load Packet

[20 Jul 2009 13:36] Mark Logan
Description:
Running .NET webservice using connector.net on Windows XP to connect to MySQL(version 5.0.45) on Linux process sometimes deadlocks. No exceptions are thrown.

Debugging into source code of MySqlStream.cs line 171:

   while (leftToRead > 0)
   {
     int read = inStream.Read(packet.Buffer, offset, leftToRead);
     leftToRead -= read;
     offset += read;
   }

inStream.Read() always returns 0 and leftToRead is > 0 so results in infinte loop. .Net documentation indicates that 0 will be returned if the end of the stream has been reached.  Somehow there are still bytes to be read but the Read method indicates end of stream - indicating stream is broken.  In this instance infinte loop results.

This is similar to: Bug #43678 Reading from a CompressedStream may result in deadlock 

How to repeat:
Error recurrs intermittently. In our testing, web service is only called by 4 other processes (concurrently).  Connection to mysql server seems to be breaking somehow. 

Suggested fix:
   while (leftToRead > 0)
   {
      int read = baseStream.Read(inBuffer, numRead, numToRead);
      numRead += read;
      numToRead -= read;
      if (read == 0 && numToRead > 0)
          throw new MySqlException("The end of the stream was unexpectedly
reached.");
    }
[21 Jul 2009 6:34] Tonci Grgin
Hi Mark and thanks for your report.

I'll leave it open until Wlad's patch for Bug#43678 is released so you can retest.
[29 Jul 2009 11:07] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/79516

731 Vladislav Vaintroub	2009-07-29
      fixed potential infinite loop in LoadPacket(), when read from a stream returns 0. (bug#46308)
[5 Aug 2009 12:25] Tony Bedford
An entry was added to the 6.0.5 and 6.1.1 changelogs:

MySQL Connector/NET sometimes hung, without generating an exception. This happened if a read from a stream failed returning a 0, causing the code in LoadPacket() to enter an infinite loop.