Bug #67543 Object reference not set to an instance of an object in get_ServerThread()
Submitted: 9 Nov 2012 20:00 Modified: 29 Sep 19:58
Reporter: Csaba Skultety Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.5.4 OS:Windows
Assigned to: Assigned Account CPU Architecture:Any

[9 Nov 2012 20:00] Csaba Skultety
Description:
I have an application which periodically polls the database for things to process. Occasionally, sometimes several times in a row or sometimes minutes apart, and sometimes hours or days apart it throws this error:

Object reference not set to an instance of an object.
   at MySql.Data.MySqlClient.MySqlConnection.get_ServerThread()
   at MySql.Data.MySqlClient.MySqlConnection.HandleTimeoutOrThreadAbort(Exception ex)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlHelper.ExecuteReader(MySqlConnection connection, MySqlTransaction transaction, String commandText, MySqlParameter[] commandParameters, Boolean ExternalConn)
   at MySql.Data.MySqlClient.MySqlHelper.ExecuteReader(String connectionString, String commandText, MySqlParameter[] commandParameters)

The call is made using MySqlHelper and passing in a connection string, not an actual connection. Also as I mentioned, it is pretty random and sometimes right after an error on the next iteration it fetches data from the database so it's not an issue with the server or connectivity to the server (at least it doesn't appear so).

How to repeat:
I cannot repeat it on demand, it appears in my logs randomly.
[11 Jan 2013 15:39] NOT_FOUND NOT_FOUND
As an additional note:

I have reason to suspect that the actual, correct exception is being masked by this null reference exception. So while the error is not necessarily "abnormal" from MySqlHelper, the fact that the null reference exception is masking the correct one is a defect.
[30 Oct 2014 10:39] Coleman Corrigan
I've hit the same issue on delete of a copied Connector from MySQL Workbench.
Similarly I think it masks a separate, underlying issue. Following a hard reset (power fault) MySQL Workbench would lock up windows when a connection was opened (consumed all memory and got into a system fatal malloc/paging spiral). 
I used Manage Server Connections window [Duplicate] to copy the connector, then when I used [Delete] to remove the original, I got said error.

In my case I'd two connectors to duplicate, both generated the MySQL Workbench Unexpected Error : Object reference not set to an instance of an object.

In both cases, following the error, [Test Connection] on the duplicated connectors then reported - Failed to Connect to MySQL at 127.0.0.1:3306 with user root
 Can't connect to MySQL server on '27.0.0.1' (10061)

Closing out the Manage Server Connections and opening the connectors via the main page worked fine , and on re-opening the Manage Server Connections window, connector hostnames were restored and [Test Connection] works as before.
[29 Sep 20:00] Jose Ramirez Ruiz
Posted by developer:
 
The exception-masking defect is still reproducible.

Reproduces by executing current timeout-recovery path with an unopened connection and forced its cancellation connection attempt to fail. 

It raised:
System.NullReferenceException:
Object reference not set to an instance of an object.

Cause:
- ServerThread blindly dereferences driver: [MySqlConnection.cs (line 189)].
- Command timeout handling calls HandleTimeoutOrThreadAbort: [MySqlCommand.cs (line 890)].
- If fast query cancellation fails, the recovery handler tries to log with ServerThread: [MySqlConnection.cs (line 1213)].
- When the driver is already absent—for example, after an incomplete open, close, or relevant race—that logging statement throws NullReferenceException, replacing the original failure.

The async handler has the same problem at [MySqlConnection.cs (line 1268)]. Abort also logs through this unsafe property in its error path.

A robust fix would preserve the original exception and make diagnostics null-safe—e.g., snapshot the thread ID only when driver exists, use a sentinel ID for logging otherwise, and avoid attempting cancellation/abort work when there is no active driver.
There are tests for ServerThread only on active connections; none cover timeout recovery after the driver has become unavailable.