altmp / altv-issues

Issues and roadmap for alt:V project
93 stars 16 forks source link

Using HostBuilder and a HostedService, server stop gets stuck #707

Closed StiviiK closed 4 years ago

StiviiK commented 4 years ago

Client/server version Build #1301, branch release

Current behavior When using the HostBuilder and a HostedService the Server Stop get stuck. Consider the following code:

namespace Test
{
    public class Program : Resource
    {
        private IHost _host;

        public Program() 
            => _host = CreateHostBuilder()
                .Build();

        public static IHostBuilder CreateHostBuilder() 
            => Host.CreateDefaultBuilder()
                .ConfigureHostConfiguration((builder) => {
                    builder
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", optional: true)
                        .AddUserSecrets<Core>();
                })
                .ConfigureLogging((context, builder) => {
                    builder
                        .AddConfiguration(context.Configuration)
                        .AddSentry();
                })
                .ConfigureServices(ConfigureServices)
                .UseConsoleLifetime();

        public async static void ConfigureServices(HostBuilderContext context, IServiceCollection collection)
        {
            [...]

            // Add Core
            collection.AddHostedService<Core>();
        }

        public async override void OnStart()
        {
            await _host.StartAsync();
        }

        public async override void OnStop()
        {
            await _host.StopAsync();
        }
    }

    public class Core : IHostedService
    {
        public async Task StartAsync(CancellationToken cancellationToken)
        {
             Console.WriteLine("Start");
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine("Stop");
        }
    }
}

Produces the following output:

[21:55:03] alt:V Server, build #1301, branch release
[21:55:03] Starting alt:V Server on 0.0.0.0:7788
[21:55:03] coreclr-module: version found: 3.1.9
[21:55:03] coreclr-module: greatest version: 3.1.9
[21:55:04] Loading resource test
Start
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /opt/altv
[21:55:04] Loaded resource test
[21:55:04] Starting HTTP server on 0.0.0.0:7788
[21:55:04] Console thread started (ThreadId: 8)
[21:55:04] Main thread started (ThreadId: 6)
[21:55:04] VoicePacketProcess thread started (ThreadId: 9)
[21:55:04] VoiceStreamer thread started (ThreadId: 10)
[21:55:04] Colshape thread started (ThreadId: 11)
[21:55:04] EntityStreamer thread started (ThreadId: 36)
[21:55:04] Network thread started (ThreadId: 38)
[21:55:04] Server started
^Cinfo: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...

Context (environment) Currently only tested within a Docker Container, but this should not really matter.

Unnvaldr commented 4 years ago

If it'll be confirmed that the issue is related to the server itself, then issue will get reopened.