Invalid attempt to call Read when reader is closed

Vote:
 

Hi,
After upgrade from CMS 11.20.16  to CMS 12.30.0 we ran into a problem, that for some reason, time to time, getting connection exceptions on using IContentLoader
GetChildren (IEnumerable<T> GetChildren<T>(ContentReference contentLink)) and
GetBySegment (IContent GetBySegment(ContentReference parentLink, string urlSegment, CultureInfo language))

ExecuteReader requires an open and available Connection. The connection's current state is open.
Invalid attempt to call Read when reader is closed.
ExecuteReader requires an open and available Connection. The connection's current state is closed.

Exceptions:

System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is open.

   at Microsoft.Data.SqlClient.SqlConnection.GetOpenTdsConnection(String method)

   at Microsoft.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)

   at Microsoft.Data.SqlClient.SqlCommand.ValidateCommand(Boolean isAsync, String method)

   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)

   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)

   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)

   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)

   at EPiServer.DataAccess.Internal.ContentListDB.<>c__DisplayClass11_0.<ListMatchingSegments>b__0()

   at EPiServer.Data.Providers.Internal.SqlDatabaseExecutor.<>c__DisplayClass26_0`1.<Execute>b__0()

   at EPiServer.Data.Providers.SqlTransientErrorsRetryPolicy.Execute[TResult](Func`1 method)

   at EPiServer.Data.Providers.Internal.SqlDatabaseExecutor.Execute[TResult](Func`1 action)

   at EPiServer.DataAccess.Internal.ContentListDB.ListMatchingSegments(ContentReference parentLink, String urlSegment)

   at EPiServer.Core.Internal.DefaultContentProviderDatabase.ListMatchingSegments(ContentReference parentLink, String urlSegment)

   at EPiServer.Core.Internal.DefaultContentProvider.ListMatchingSegments(ContentReference parentLink, String urlSegment)

   at EPiServer.Core.ContentProvider.<>c__DisplayClass79_0.<GetMatchingSegments>b__0()

   at EPiServer.Framework.Cache.ObjectInstanceCacheExtensions.ReadThroughWithWait[T](IObjectInstanceCache cache, String cacheKey, Func`1 readValue, Func`2 evictionPolicy)


Failed to Read cacheKey = 'EPPageData:2870:en'

System.InvalidOperationException: Invalid attempt to call Read when reader is closed.

   at Microsoft.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)

   at Microsoft.Data.SqlClient.SqlDataReader.Read()

   at EPiServer.DataAccess.Internal.ContentLoadDB.LoadContentInternal(ContentReference contentLink, Int32 languageBranchId, DbDataReader reader)

   at EPiServer.DataAccess.Internal.ContentLoadDB.<>c__DisplayClass2_0.<Load>b__0()

   at EPiServer.Data.Providers.Internal.SqlDatabaseExecutor.<>c__DisplayClass26_0`1.<Execute>b__0()

   at EPiServer.Data.Providers.SqlTransientErrorsRetryPolicy.Execute[TResult](Func`1 method)

   at EPiServer.Data.Providers.Internal.SqlDatabaseExecutor.Execute[TResult](Func`1 action)

   at EPiServer.DataAccess.Internal.ContentLoadDB.Load(ContentReference contentLink, Int32 languageBranchID)

   at EPiServer.Core.Internal.DefaultContentProviderDatabase.Load(ContentReference contentLink, Int32 languageBranchID)

   at EPiServer.Core.Internal.DefaultContentProvider.LoadContent(ContentReference contentLink, ILanguageSelector languageSelector)

   at EPiServer.Core.ContentProvider.<>c__DisplayClass123_0.<LoadContentFromCacheOrRepository>b__0()

   at EPiServer.Framework.Cache.ObjectInstanceCacheExtensions.ReadThroughWithWait[T](IObjectInstanceCache cache, String cacheKey, Func`1 readValue, Func`2 evictionPolicy)


System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed.

   at Microsoft.Data.SqlClient.SqlCommand.ValidateCommand(Boolean isAsync, String method)

   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)

   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)

   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)

   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)

   at EPiServer.DataAccess.Internal.ContentLoadDB.<>c__DisplayClass2_0.<Load>b__0()

   at EPiServer.Data.Providers.Internal.SqlDatabaseExecutor.<>c__DisplayClass26_0`1.<Execute>b__0()

   at EPiServer.Data.Providers.SqlTransientErrorsRetryPolicy.Execute[TResult](Func`1 method)

   at EPiServer.Data.Providers.Internal.SqlDatabaseExecutor.Execute[TResult](Func`1 action)

   at EPiServer.DataAccess.Internal.ContentLoadDB.Load(ContentReference contentLink, Int32 languageBranchID)

   at EPiServer.Core.Internal.DefaultContentProviderDatabase.Load(ContentReference contentLink, Int32 languageBranchID)

   at EPiServer.Core.Internal.DefaultContentProvider.LoadContent(ContentReference contentLink, ILanguageSelector languageSelector)

   at EPiServer.Core.ContentProvider.<>c__DisplayClass123_0.<LoadContentFromCacheOrRepository>b__0()

   at EPiServer.Framework.Cache.ObjectInstanceCacheExtensions.ReadThroughWithWait[T](IObjectInstanceCache cache, String cacheKey, Func`1 readValue, Func`2 evictionPolicy)

System.InvalidOperationException:

   at Microsoft.Data.SqlClient.SqlConnection.GetOpenTdsConnection (Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)

   at Microsoft.Data.SqlClient.SqlConnection.ValidateConnectionForExecute (Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)

   at Microsoft.Data.SqlClient.SqlCommand.ValidateCommand (Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)

   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader (Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)

   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader (Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)

   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader (Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)

   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader (Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)

   at EPiServer.DataAccess.Internal.ContentListDB+<>c__DisplayClass11_0.<ListMatchingSegments>b__0 (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

   at EPiServer.Data.Providers.Internal.SqlDatabaseExecutor+<>c__DisplayClass26_0`1.<Execute>b__0 (EPiServer.Data, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

   at EPiServer.Data.Providers.SqlTransientErrorsRetryPolicy.Execute (EPiServer.Data, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

   at EPiServer.Data.Providers.Internal.SqlDatabaseExecutor.Execute (EPiServer.Data, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

   at EPiServer.DataAccess.Internal.ContentListDB.ListMatchingSegments (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

   at EPiServer.Core.Internal.DefaultContentProviderDatabase.ListMatchingSegments (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

   at EPiServer.Core.Internal.DefaultContentProvider.ListMatchingSegments (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

   at EPiServer.Core.ContentProvider+<>c__DisplayClass79_0.<GetMatchingSegments>b__0 (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

   at EPiServer.Framework.Cache.ObjectInstanceCacheExtensions.ReadThroughWithWait (EPiServer.Framework, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

#325903
Jul 23, 2024 8:09
Vote:
 

Hi Vladislavs,

I am getting the same error on contentrepository.save after upgrading to 12.31. Did you able resolve this?

#326381
Aug 01, 2024 14:13
Vote:
 

Hi,
As a workaround have added catching InvalidOperationException and retry if it fails, as then app does not crash(success on retry), but it still can throw exception even on retry and now getting also NullReferenceException (related to connection) for GetChildren:
System.NullReferenceException:
   at EPiServer.Data.Providers.Internal.ConnectionContext.CloseConnection (EPiServer.Data, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Data.Providers.Internal.ConnectionScope.Complete (EPiServer.Data, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Data.Providers.Internal.SqlDatabaseExecutor+<>c__DisplayClass26_0`1.<Execute>b__0 (EPiServer.Data, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Data.Providers.SqlTransientErrorsRetryPolicy.Execute (EPiServer.Data, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.DataAccess.Internal.ContentListDB.LoadSpecificContentInstances (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Core.Internal.DefaultContentProvider.BatchLoad (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Core.Internal.DefaultContentProvider.LoadContents (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Core.ContentProvider.GetContentBatch (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Core.ContentProvider.GetScatteredContents (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Core.Internal.ProviderPipelineImplementation.GetItems (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Core.Internal.DefaultContentLoader.GetChildren (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)
   at EPiServer.Core.Internal.DefaultContentLoader.GetChildren (EPiServer, Version=12.21.4.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7)

#326610
Aug 06, 2024 9:51
Vote:
 

@iliyas shaikh
Did you manage to solve this in any other way?

#328131
Aug 27, 2024 9:52
Vote:
 

Hi Vladislavs,

I got some help in this https://world.optimizely.com/blogs/Johan-Bjornfot/Dates1/2023/8/parallel-tasks-and-backgroundcontext/

Resolve it by keeping contentrepository.save inside using statement. Hope this might helps you.

#328132
Aug 27, 2024 10:12
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.