Bug #8909 Error with native driver Close() after MySQL unexpected restart
Submitted: 2 Mar 2005 22:27 Modified: 23 Aug 2005 18:29
Reporter: Lynn Eriksen Email Updates:
Status: Closed Impact on me:
Category:Connector / NET Severity:S1 (Critical)
Version:1.0.4 OS:Windows (Windows XP/2003)
Assigned to: Reggie Burnett CPU Architecture:Any

[2 Mar 2005 22:27] Lynn Eriksen
During  a  MySQl sever hard crash and restart by the service manager - the Network stream used by the PacketWritter becomes disposed and consequenty unavailable. When the Close() command is called thse code below is fired:

	if (isOpen)
	ExecuteCommand( DBCmd.QUIT, null, 0 );


The problem in in 'writer.Stream.Close();'. What seems to be happening is that when the server crahses the runtime is disposing the stream, but is still being referenced by the PacketWriter. Calling Close() after is has been disposed raises a System.ObjectDisposedException. This happens repeatedly, essentially crashing the program.

How to repeat:
I created a web page that simply create a connection, opens it, pings, and then closes.

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim DSN As String = "database=testdb;server=localhost;User Id=root;Password=mypassword"

        Dim Connection As New MySql.Data.MySqlClient.MySqlConnection(DSN)

        Me.Trace.IsEnabled = True

        Me.Trace.Warn("Connection", "Closed")


        Me.Trace.Warn("Connection", "Open")


        Me.Trace.Warn("Connection", "Ping")
    End Sub

Normally this makes a nice set of trace statement.

However if I go to the task manager and kill the process (and have the mysql service scheduled to restart automatically), repeatedly I get the error:

[ObjectDisposedException: Cannot access a disposed object named "System.Net.Sockets.NetworkStream".
Object name: "System.Net.Sockets.NetworkStream".]
   System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   System.IO.BufferedStream.FlushWrite() +21
   System.IO.BufferedStream.Flush() +28
   System.IO.BufferedStream.Close() +15
   MySql.Data.MySqlClient.NativeDriver.Close() in c:\program files\mysql\mysql connector net 1.0.4\src\mysqlclient\nativedriver.cs:357
   MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() in C:\Program Files\MySQL\MySQL Connector Net 1.0.4\src\MySqlClient\MySqlPool.cs:107
   MySql.Data.MySqlClient.MySqlPool.GetConnection() in C:\Program Files\MySQL\MySQL Connector Net 1.0.4\src\MySqlClient\MySqlPool.cs:178
   MySql.Data.MySqlClient.MySqlPoolManager.GetConnection(MySqlConnectionString settings) in C:\Program Files\MySQL\MySQL Connector Net 1.0.4\src\MySqlClient\MySqlPoolManager.cs:68
   MySql.Data.MySqlClient.MySqlConnection.Open() in C:\Program Files\MySQL\MySQL Connector Net 1.0.4\src\MySqlClient\Connection.cs:292
   ConnectorNetDebug._Default.Page_Load(Object sender, EventArgs e) in D:\ASPNetProjects\Projects\Experiments\ConnectorNetDebug\Default.aspx.vb:35
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +733

BTW -D:\ASPNetProjects\Projects\Experiments\ConnectorNetDebug\Default.aspx.vb:35

is  the Connection.Open() command above.

Suggested fix:
I tried to trace down a solution for the problem, but an immediate fix seems to be to error wrap the close commands:

		//reopen the connection

I looked up the execption on the web and couldn't find much about it.

Reopening the connection seems to work.

Please resolve asap.
[23 Aug 2005 18:29] Reggie Burnett
Thank you for your bug report. This issue has already been fixed
in the latest released version of that product, which you can download at 

Additional info:

This bug is corrected in 1.0.5 which should be released today.  I have unit tests in the test suite that opens a connection, kills the connection, and then tries to reopen it and fixed this exact problem in 1.0.4.

If a new permutation of this bug appears, please repost a bug for it.