Merge pull request #3761 from cvium/fix_mem_leak

Fix DI memory leak

(cherry picked from commit 9bf6222597)
Signed-off-by: Joshua M. Boniface <joshua@boniface.me>
pull/4001/head
Joshua M. Boniface 4 years ago
parent f88e9b2678
commit 43ade73be4

@ -1,4 +1,6 @@
using System; using System;
using System.IO;
using MediaBrowser.Common.Configuration;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -10,15 +12,20 @@ namespace Jellyfin.Server.Implementations
public class JellyfinDbProvider public class JellyfinDbProvider
{ {
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly IApplicationPaths _appPaths;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="JellyfinDbProvider"/> class. /// Initializes a new instance of the <see cref="JellyfinDbProvider"/> class.
/// </summary> /// </summary>
/// <param name="serviceProvider">The application's service provider.</param> /// <param name="serviceProvider">The application's service provider.</param>
public JellyfinDbProvider(IServiceProvider serviceProvider) /// <param name="appPaths">The application paths.</param>
public JellyfinDbProvider(IServiceProvider serviceProvider, IApplicationPaths appPaths)
{ {
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
serviceProvider.GetRequiredService<JellyfinDb>().Database.Migrate(); _appPaths = appPaths;
using var jellyfinDb = CreateContext();
jellyfinDb.Database.Migrate();
} }
/// <summary> /// <summary>
@ -27,7 +34,8 @@ namespace Jellyfin.Server.Implementations
/// <returns>The newly created context.</returns> /// <returns>The newly created context.</returns>
public JellyfinDb CreateContext() public JellyfinDb CreateContext()
{ {
return _serviceProvider.GetRequiredService<JellyfinDb>(); var contextOptions = new DbContextOptionsBuilder<JellyfinDb>().UseSqlite($"Filename={Path.Combine(_appPaths.DataPath, "jellyfin.db")}");
return ActivatorUtilities.CreateInstance<JellyfinDb>(_serviceProvider, contextOptions.Options);
} }
} }
} }

@ -63,11 +63,12 @@ namespace Jellyfin.Server
Logger.LogWarning($"Skia not available. Will fallback to {nameof(NullImageEncoder)}."); Logger.LogWarning($"Skia not available. Will fallback to {nameof(NullImageEncoder)}.");
} }
// TODO: Set up scoping and use AddDbContextPool // TODO: Set up scoping and use AddDbContextPool,
serviceCollection.AddDbContext<JellyfinDb>( // can't register as Transient since tracking transient in GC is funky
options => options // serviceCollection.AddDbContext<JellyfinDb>(
.UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"), // options => options
ServiceLifetime.Transient); // .UseSqlite($"Filename={Path.Combine(ApplicationPaths.DataPath, "jellyfin.db")}"),
// ServiceLifetime.Transient);
serviceCollection.AddSingleton<JellyfinDbProvider>(); serviceCollection.AddSingleton<JellyfinDbProvider>();

Loading…
Cancel
Save