System.InvalidOperationException: Can't replace active reader.

See original GitHub issue

Software versions MySqlConnector version: 2.1.8 Server type (MySQL, MariaDB, Aurora, etc.) and version: Aurora (MySQL 5.7) .NET version: .NET 6 (Optional) ORM NuGet packages and versions: Pomelo 6.0.1

Describe the bug After some usage my application, I start getting pseudo-random exceptions. It doesn’t happen on each call but it happens around 50% of web calls after some time. Restarting the application seems to reset things but they again start to go wrong after a while.

This only happened after the latest update. I changed from version 2.1.6 to 2.1.8. I updated from .NET and EFCore 6.0.2 to 6.0.3. Pomelo remained the same.

Admittingly there are other code changes but nothing big nor that should do this as I don’t use Readers directly. Only with EFCore. I don’t see connection exhaustion or anything looking at metrics in AWS.

I’m not sure if I’m doing something wrong or where to look for to how to solve this problem. Can’t find anything similar. I’m going to downgrade to 2.1.6 to see if the issue reoccurs. I’m hoping to get some pointers about how to figure out what’s going on.

Exception

System.InvalidOperationException: Can't replace active reader.
   at MySqlConnector.MySqlConnection.SetActiveReader(MySqlDataReader dataReader) in /_/src/MySqlConnector/MySqlConnection.cs:line 874
   at MySqlConnector.MySqlDataReader.CreateAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary`2 cachedProcedures, IMySqlCommand command, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlDataReader.cs:line 460
   at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(IReadOnlyList`1 commands, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, Activity activity, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 56
   at MySqlConnector.MySqlCommand.ExecuteReaderAsync(CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 344
   at MySqlConnector.MySqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 337
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass33_0`2.<<ExecuteAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:30 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jcfore21commented, Aug 25, 2022

@bgrainger After deploying that fix, we have gone over an hour without issue. We even had the exception occur which normally seemed to be the start of the “Can’t replace active reader” issues. However, it didn’t appear to start those messages anymore. I wasn’t able to see any warnings logged either.

I think the issue is may be resolved but going to keep monitoring.

1reaction
adamhathcockcommented, Mar 25, 2022

Seemingly, reverting to 2.1.6 stops the problem.

It only happens in one of the services after some period of use. It’s like I’m leaking something is one specific piece of code but I don’t know where to begin as I don’t believe I’m doing anything different in one area versus another.

I’ve had several hours with no errors now when it only took 1-2 at most (this service is relatively low usage).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't replace active reader in c# .net
System.InvalidOperationException: Can't replace active reader. I have investigated a lot on this issue , but could not find a solution.
Read more >
'there is already an open datareader associated with this ...
System.InvalidOperationException: 'There is already an open DataReader associated with this Command which must be closed first.' command and ...
Read more >
Error: There is already an open DataReader associated with ...
After downloading the project and database from Azure, when trying to start the project an error appears: "Unable to merge MetadataSource ...
Read more >
InvalidOperationException Class (System)
The exception that is thrown when a method call is invalid for the object's current state.
Read more >
Timeout expired messages when connecting to SQL Server
System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found