From 50b8daf9390268cf2d94a897baf47681e31d314e Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 22 Aug 2018 14:07:52 +0900 Subject: [PATCH] Fix threads being cross-disposed from DatabaseContextFactory --- osu.Game/Database/DatabaseContextFactory.cs | 5 +++-- osu.Game/Database/OsuDbContext.cs | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/osu.Game/Database/DatabaseContextFactory.cs b/osu.Game/Database/DatabaseContextFactory.cs index e70d753114..2037612a09 100644 --- a/osu.Game/Database/DatabaseContextFactory.cs +++ b/osu.Game/Database/DatabaseContextFactory.cs @@ -5,7 +5,6 @@ using System; using System.Linq; using System.Threading; using Microsoft.EntityFrameworkCore.Storage; -using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Platform; namespace osu.Game.Database @@ -118,7 +117,9 @@ namespace osu.Game.Database private void recycleThreadContexts() { - threadContexts?.Values.ForEach(c => c.Dispose()); + // Contexts for other threads are not disposed as they may be in use elsewhere. Instead, fresh contexts are exposed + // for other threads to use, and we rely on the finalizer inside OsuDbContext to handle their previous contexts + threadContexts?.Value.Dispose(); threadContexts = new ThreadLocal(CreateContext, true); } diff --git a/osu.Game/Database/OsuDbContext.cs b/osu.Game/Database/OsuDbContext.cs index bf57644caf..20e144c033 100644 --- a/osu.Game/Database/OsuDbContext.cs +++ b/osu.Game/Database/OsuDbContext.cs @@ -75,6 +75,13 @@ namespace osu.Game.Database } } + ~OsuDbContext() + { + // DbContext does not contain a finalizer (https://github.com/aspnet/EntityFrameworkCore/issues/8872) + // This is used to clean up previous contexts when fresh contexts are exposed via DatabaseContextFactory + Dispose(); + } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder);