Description:
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 );
reader.Stream.Close();
writer.Stream.Close();
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")
Connection.Open()
Me.Trace.Warn("Connection", "Open")
Connection.Ping()
Me.Trace.Warn("Connection", "Ping")
Connection.Close()
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:
try
{
reader.Stream.Close();
writer.Stream.Close();
}
catch
{
//reopen the connection
this.Open();
}
finally
{
base.Close();
}
I looked up the execption on the web and couldn't find much about it.
Reopening the connection seems to work.
Please resolve asap.