dotnet / dotnet/MQTTnet

The server shuts down too slowly.

Open
#1,916 5 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
5.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

### 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:
```csharp
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().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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the .NET 8 startup example using MQTTnet.AspNetCore 4.3.3.952 and reproduce the delay by connecting with an MQTT client or telnet before stopping the server. Read the MQTT server shutdown path and compare it with the shown ApplicationStopping StopAsync registration; done means an active connection no longer causes the roughly 30-second shutdown delay and existing behavior remains covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.