Skip to content

DbContexts aren't handled in a thread safe way #36

@gunnbr

Description

@gunnbr

Gthx occasionally crashes with the following stack trace, apparently due to improper handling of DbContexts:

2025-12-08 02:07:06.613 +00:00 [Error] An exception occurred while iterating over the results of a query for context type '"GthxData.GthxDataContext"'."
""System.InvalidOperationException: A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
   at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()"
System.InvalidOperationException: A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
   at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
   at lambda_method57(Closure , QueryContext )
   at Gthx.Data.GthxSqlData.AddYoutubeTitle(String item, String title) in /source/Gthx.Data/Gthx.Data/GthxSqlData.cs:line 155
   at Gthx.Bot.Modules.YoutubeModule.GetAndSaveTitle(String url, String id, String channel, String user, Int32 referenceCount) in /source/Gthx.Bot/Modules/YoutubeModule.cs:line 70
2025-12-08 02:07:06.613 +00:00 [Error] YoutubeModule.GetAndSaveTitle failed!
System.InvalidOperationException: A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
   at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
   at lambda_method57(Closure , QueryContext )
   at Gthx.Data.GthxSqlData.AddYoutubeTitle(String item, String title) in /source/Gthx.Data/Gthx.Data/GthxSqlData.cs:line 155
   at Gthx.Bot.Modules.YoutubeModule.GetAndSaveTitle(String url, String id, String channel, String user, Int32 referenceCount) in /source/Gthx.Bot/Modules/YoutubeModule.cs:line 70

This must be fixed to handle the DbContext properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions