From 2c73430b74dae44ccd544e3e24af2e76f744fa9d Mon Sep 17 00:00:00 2001 From: Marti Markov Date: Thu, 1 Oct 2026 19:05:20 +0100 Subject: [PATCH] Dispose the database context in DeleteTrickplayDataAsync --- .../Trickplay/TrickplayManager.cs | 5 +- .../Trickplay/TrickplayManagerTests.cs | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/Jellyfin.Server.Implementations.Tests/Trickplay/TrickplayManagerTests.cs diff --git a/Jellyfin.Server.Implementations/Trickplay/TrickplayManager.cs b/Jellyfin.Server.Implementations/Trickplay/TrickplayManager.cs index 40a153c8ed..3c1164bffe 100644 --- a/Jellyfin.Server.Implementations/Trickplay/TrickplayManager.cs +++ b/Jellyfin.Server.Implementations/Trickplay/TrickplayManager.cs @@ -731,7 +731,10 @@ public partial class TrickplayManager : ITrickplayManager public async Task DeleteTrickplayDataAsync(Guid itemId, CancellationToken cancellationToken) { var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); - await dbContext.TrickplayInfos.Where(i => i.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false); + await using (dbContext.ConfigureAwait(false)) + { + await dbContext.TrickplayInfos.Where(i => i.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false); + } } /// diff --git a/tests/Jellyfin.Server.Implementations.Tests/Trickplay/TrickplayManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Trickplay/TrickplayManagerTests.cs new file mode 100644 index 0000000000..fa33806dcc --- /dev/null +++ b/tests/Jellyfin.Server.Implementations.Tests/Trickplay/TrickplayManagerTests.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Jellyfin.Database.Implementations; +using Jellyfin.Server.Implementations.Tests.Item; +using Jellyfin.Server.Implementations.Trickplay; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging.Abstractions; +using Moq; +using Xunit; + +namespace Jellyfin.Server.Implementations.Tests.Trickplay; + +public sealed class TrickplayManagerTests : SqliteDbTestFixture +{ + [Fact] + public async Task DeleteTrickplayDataAsync_DisposesDbContext() + { + var contexts = new List(); + var factory = new Mock>(); + factory.Setup(f => f.CreateDbContextAsync(It.IsAny())) + .ReturnsAsync(() => + { + var context = CreateDbContext(); + contexts.Add(context); + return context; + }); + + // Only the database context factory is used when deleting trickplay data. + var trickplayManager = new TrickplayManager( + NullLogger.Instance, + null!, + null!, + null!, + null!, + null!, + factory.Object, + null!, + null!); + + await trickplayManager.DeleteTrickplayDataAsync(Guid.NewGuid(), CancellationToken.None); + + var context = Assert.Single(contexts); + Assert.Throws(() => context.Model); + } +}