Pārlūkot izejas kodu

Pls fix race condition

Bond_009 4 gadi atpakaļ
vecāks
revīzija
032d72a8a7

+ 6 - 4
Emby.Server.Implementations/ApplicationHost.cs

@@ -126,7 +126,6 @@ namespace Emby.Server.Implementations
         private IMediaEncoder _mediaEncoder;
         private ISessionManager _sessionManager;
         private string[] _urlPrefixes;
-        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.GetOptions();
 
         /// <summary>
         /// Gets a value indicating whether this instance can self restart.
@@ -484,8 +483,9 @@ namespace Emby.Server.Implementations
         /// Runs the startup tasks.
         /// </summary>
         /// <returns><see cref="Task" />.</returns>
-        public async Task RunStartupTasksAsync()
+        public async Task RunStartupTasksAsync(CancellationToken cancellationToken)
         {
+            cancellationToken.ThrowIfCancellationRequested();
             Logger.LogInformation("Running startup tasks");
 
             Resolve<ITaskManager>().AddTasks(GetExports<IScheduledTask>(false));
@@ -501,13 +501,15 @@ namespace Emby.Server.Implementations
 
             var stopWatch = new Stopwatch();
             stopWatch.Start();
-            await Task.WhenAll(StartEntryPoints(entryPoints, true)).ConfigureAwait(false);
+            cancellationToken.ThrowIfCancellationRequested();
+            await Task.WhenAny(Task.WhenAll(StartEntryPoints(entryPoints, true)), Task.Delay(-1, cancellationToken)).ConfigureAwait(false);
             Logger.LogInformation("Executed all pre-startup entry points in {Elapsed:g}", stopWatch.Elapsed);
 
             Logger.LogInformation("Core startup complete");
             CoreStartupHasCompleted = true;
             stopWatch.Restart();
-            await Task.WhenAll(StartEntryPoints(entryPoints, false)).ConfigureAwait(false);
+            cancellationToken.ThrowIfCancellationRequested();
+            await Task.WhenAny(Task.WhenAll(StartEntryPoints(entryPoints, false)), Task.Delay(-1, cancellationToken)).ConfigureAwait(false);
             Logger.LogInformation("Executed all post-startup entry points in {Elapsed:g}", stopWatch.Elapsed);
             stopWatch.Stop();
         }

+ 1 - 1
Jellyfin.Server/Program.cs

@@ -202,7 +202,7 @@ namespace Jellyfin.Server
                     throw;
                 }
 
-                await appHost.RunStartupTasksAsync().ConfigureAwait(false);
+                await appHost.RunStartupTasksAsync(_tokenSource.Token).ConfigureAwait(false);
 
                 stopWatch.Stop();
 

+ 3 - 2
tests/Jellyfin.Api.Tests/JellyfinApplicationFactory.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Concurrent;
 using System.IO;
+using System.Threading;
 using Emby.Server.Implementations;
 using Emby.Server.Implementations.IO;
 using Jellyfin.Server;
@@ -21,7 +22,7 @@ namespace Jellyfin.Api.Tests
     public class JellyfinApplicationFactory : WebApplicationFactory<Startup>
     {
         private static readonly string _testPathRoot = Path.Combine(Path.GetTempPath(), "jellyfin-test-data");
-        private static readonly ConcurrentBag<IDisposable> _disposableComponents = new ConcurrentBag<IDisposable>();
+        private readonly ConcurrentBag<IDisposable> _disposableComponents = new ConcurrentBag<IDisposable>();
 
         /// <summary>
         /// Initializes a new instance of the <see cref="JellyfinApplicationFactory"/> class.
@@ -96,7 +97,7 @@ namespace Jellyfin.Api.Tests
             var appHost = (TestAppHost)testServer.Services.GetRequiredService<IApplicationHost>();
             appHost.ServiceProvider = testServer.Services;
             appHost.InitializeServices().GetAwaiter().GetResult();
-            appHost.RunStartupTasksAsync().GetAwaiter().GetResult();
+            appHost.RunStartupTasksAsync(CancellationToken.None).GetAwaiter().GetResult();
 
             return testServer;
         }