Skip to content

NullReferenceException in MySqlConnector.MySqlDataReader.ActivateResultSet #1459

@feiazifeiazi

Description

@feiazifeiazi

An optimization introduced in v2.3.0 started recycling MySqlDataReader instances: #1277.

If client code held onto the MySqlDataReader and disposed it after the associated MySqlConnection had been returned to the connection pool, this could cause that connection to be disposed while it was being used by another thread, leading to unpredictable NullReferenceExceptions.

While many access patterns didn't trigger this bug (e.g., no problems have been reported by users of Pomelo.EntityFrameworkCore.MySql), using CommandBehavior.CloseConnection or Dapper's QueryAsync<T> made it more likely to happen.

Original bug report below.


Software versions
MySqlConnector version: 2.3.5
Server type (MySQL, MariaDB, Aurora, etc.) and version: 8.0.25
.NET version: net7.0
Dapper version: 2.1.28

Describe the bug
NullReferenceException

Exception

[Error Message]=Object reference not set to an instance of an object.
[Exception Type]=System.NullReferenceException
[Source]=MySqlConnector
[TargetSite]=Void ActivateResultSet(System.Threading.CancellationToken)
[Stack Trace]=   at MySqlConnector.MySqlDataReader.ActivateResultSet(CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 133
   at MySqlConnector.MySqlDataReader.InitAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary`2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 505
   at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 78
   at MySqlConnector.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 317
   at MySqlConnector.MySqlCommand.ExecuteNonQuery() in /_/src/MySqlConnector/MySqlCommand.cs:line 108
   at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) in /_/Dapper/SqlMapper.cs:line 2975
   at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in /_/Dapper/SqlMapper.cs:line 656
   at Wwtfly.DBMonitor.DAL_Dapper.DBLinkConfigDAL.UpdateExecCount()

Code sample

-

Expected behavior

Additional context
This method is an update SQL . Its execution frequency is approximately 50 times per second.

The error randomly occurs 20 times per day.

Link to the exception line

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions