Skip to content

The Free Software Media System - Server Backend & API

Backport pull request #18269 from jellyfin/release-12.z

martimarkova6cc1c196c08
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);
+ }
}
/// <inheritdoc />
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<JellyfinDbContext>();
+ var factory = new Mock<IDbContextFactory<JellyfinDbContext>>();
+ factory.Setup(f => f.CreateDbContextAsync(It.IsAny<CancellationToken>()))
+ .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<TrickplayManager>.Instance,
+ null!,
+ null!,
+ null!,
+ null!,
+ null!,
+ factory.Object,
+ null!,
+ null!);
+
+ await trickplayManager.DeleteTrickplayDataAsync(Guid.NewGuid(), CancellationToken.None);
+
+ var context = Assert.Single(contexts);
+ Assert.Throws<ObjectDisposedException>(() => context.Model);
+ }
+}