Bug #8854 Connection sometimes crashes on Close() call
Submitted: 1 Mar 2005 0:34 Modified: 21 Jun 2006 9:20
Reporter: Adam M. Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:1.0.4 OS:Windows (Windows 2003 and Windows XP Pro)
Assigned to: CPU Architecture:Any

[1 Mar 2005 0:34] Adam M.
Description:
The problem is that occasionally, when Dispose() is called, the call will throw an exception with a message saying that the stream is not writable.

I'm sorry that I don't have stack trace output available, but... the path the code takes goes through before throwing the exception is:

MySqlConnection.Dispose()
calls Close()
calls driver.Close()
... in NativeDriver.Close()
calls ExecuteCommand(DBCmd.QUIT, null, 0)
calls writer.StartPacket()
... in PacketWriter.StartPacket()
calls WriteStartBlock()
calls stream.WriteByte()
... stream.WriteByte() throws the exception

I've stepped through the code at one point when I suspected it would happen, and I examined stream.CanWrite just before stream.WriteByte(). It was false.

The connection is made via TCP/IP, so the stream is created by StreamCreator.CreateSocketStream(), so it would seem to be a NetworkStream()

How to repeat:
Unfortunately, I haven't been able to get this to happen consistently. In my specific application, I just set a breakpoint on the call to MySqlConnection.Dispose(). If it gets there, then the crash will sometimes occur and sometimes not.

I wonder if a NetworkStream() becomes unwriteable if the connection is terminated, or the remote side calls Shutdown(), or under some other circumstance? The Microsoft documentation for NetworkStream() says to check the CanWrite property before attempting to write.

I tried for about 20 minutes to get the crash to happen just now, so I could get the full exception information, but I was unable to make it occur.

Suggested fix:
I'm sorry, but I don't have a suggested fix.

I made a change to the code in NativeDriver.Close(), but it's an obvious hack and doesn't fix the root problem.

public override void Close() 
{
  if (isOpen && writer.Stream.CanWrite) // HACK: ADAM: sometimes writer.Stream.CanWrite is false
    ExecuteCommand( DBCmd.QUIT, null, 0 );

  reader.Stream.Close();
  writer.Stream.Close();

  base.Close();
}
[11 Oct 2005 7:54] Vasily Kishkin
Could you please provide some test case ? Thanks
[12 Nov 2005 0:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
[21 May 2006 9:20] Valeriy Kravchuk
Please, try to provide a complete test case, as Vasily asked. Check it on the newer version, 1.0.7.
[21 Jun 2006 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".