FabianTerhorst / coreclr-module

Old alt:V CoreClr (.NET Core Common Language Runtime) community made module. New: https://github.com/altmp/coreclr-module
MIT License
72 stars 67 forks source link

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

Open StiviiK opened 3 years ago

StiviiK commented 3 years ago

Note This is a carry-over from https://github.com/altmp/altv-issues/issues/707.

Client/server version Build #1301, branch release

Current behavior When using the HostBuilder and a HostedService the server stop gets 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.

FabianTerhorst commented 3 years ago

To which value you setted the shutdown timeout in host builder?

StiviiK commented 3 years ago

Currently the default value, but using for example 1s isn't changing the behaviour.

collection.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(1));
FabianTerhorst commented 3 years ago

Can you add a console log after await _host.StartAsync(); in OnStart?

StiviiK commented 3 years ago

Gets called:

[07:19:00] 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
Started
[07:19:00] Loaded resource test
FabianTerhorst commented 3 years ago

Seems good. But i dont know how hostbuilder works internally and i dont know If its possible to stop programmatically with that.

StiviiK commented 3 years ago

Okay, so this seems like a wont fix. I will look into it further and keep the issue updated. But I guess this won't really affect me very much.

ithr0n commented 3 years ago

I also started with HostBuilder and all the stuff, but this is wrong for working with altV. HostBuilder should be used in executing assemblies. Your module gets loaded by altv.exe and therefore should not implement HostBuilder.

Nevertheless, all the advantages can be used as standalone by your own (stuff like Configuration, Dependency Injection, Logging ...).