MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

The server shuts down too slowly.

Open voids opened this issue 1 year ago • 5 comments

Describe the bug

If there is a connection, shutting down the server will be stuck for 30 seconds.

Which component is your bug related to?

  • Server

To Reproduce

Steps to reproduce the behavior:

  1. .net8 use MQTTnet.AspNetCore 4.3.3.952
  2. Run this code below:
    using MQTTnet.AspNetCore;
    using MQTTnet.Server;
    
    var builder = WebApplication.CreateSlimBuilder(args);
    builder.WebHost.UseKestrel(x => {
        x.ListenLocalhost(1883, l => l.UseMqtt());
        x.ListenAnyIP(5001);
    });
    builder.Services.AddConnections();
    builder.Services.AddMqttConnectionHandler();
    builder.Services.AddHostedMqttServer(x => {
        x.WithDefaultEndpoint();
        x.WithPersistentSessions(false);
    });
    var app = builder.Build();
    // Uncomment the code below, it will shut down quickly.
    // app.Lifetime.ApplicationStopping.Register(() => {
    //     app.Services.GetRequiredService<MqttServer>().StopAsync();
    // });
    app.Run();
    
  3. Connect to the server using any client or even telnet 127.0.0.1 1883
  4. Return to server terminal, ctrl+c , it will shut down after about 30 senconds.

Expected behavior

I'm not sure if it's intentional, but I hope it shuts down gracefully asap.

voids avatar Jan 27 '24 04:01 voids

I am also experiencing this behavior. Any update here? What is the workaround for a graceful shutdown?

kimhegg avatar Feb 21 '24 09:02 kimhegg

This seems to be related to ASP since a server shutdown is pretty fast when using the code from the samples app.

Could you please try without port 5001 and let me know if there is any difference in behavior?

chkr1011 avatar Mar 02 '24 09:03 chkr1011