Bug #101295 ObjectDisposedException In MySqlConnection.Finalize
Submitted: 23 Oct 2020 17:29 Modified: 24 Nov 2020 17:02
Reporter: Nate Smith Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:8.0.22 OS:Windows (Windows 10)
Assigned to: CPU Architecture:x86
Tags: .net, connector, objectdisposedexception

[23 Oct 2020 17:29] Nate Smith
Description:
We are encountering a semi-random error (always in the same part of our code, but can't determine the exact source).  The logged exception does not provide any point of reference in our code, which is why we believe it to be a bug in the MySQL connector.  This error was not getting caught by any of our Try/Catch blocks (vb.net code base), it was not until we started catching Thread exceptions as well that we were able to log the actual exception.  The exception dump is below:

System.ObjectDisposedException(0x80131622): Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns)
   at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns)
   at MySql.Data.MySqlClient.Driver.SkipDataRow()
   at MySql.Data.MySqlClient.ResultSet.Close()
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlDataReader.Close()
   at MySql.Data.MySqlClient.MySqlConnection.Close()
   at MySql.Data.MySqlClient.MySqlConnection.Dispose(Boolean disposing)
   at MySql.Data.MySqlClient.MySqlConnection.Finalize()

------------------
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
   at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns)
   at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns)
   at MySql.Data.MySqlClient.Driver.SkipDataRow()
   at MySql.Data.MySqlClient.ResultSet.Close()
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlDataReader.Close()
   at MySql.Data.MySqlClient.MySqlConnection.Close()
   at MySql.Data.MySqlClient.MySqlConnection.Dispose(Boolean disposing)
   at MySql.Data.MySqlClient.MySqlConnection.Finalize()

Configuration:
Client:
 - Windows 10
 - Connector/.NET 8.0.22 (latest available)
 - VB.NET app
 - .NET Framework 4.7.2

Server:
 - ProxySQL server managing connections to two Percona XtraDB Cluster nodes running latest MySQL 5.6 release.  

How to repeat:
Unsure (see description above)
[23 Oct 2020 23:06] Bradley Grainger
If "MySqlConnection.Finalize" is being called, then it seems very likely your code is not disposing the various MySql.Data objects that are IDisposable; this includes MySqlConnection, MySqlCommand, and MySqlDataReader.

You should ensure that all MySql.Data objects are disposed, typically by using a using block, e.g., "using (var connection = new MySqlConnection(...))".
[23 Oct 2020 23:07] Bradley Grainger
My previous example was C# syntax, but VB is similar: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-s...
[24 Oct 2020 17:02] MySQL Verification Team
Please check Bradley Grainger comment and Percona XtraDB Cluster aren't supported.
[25 Nov 2020 1: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".