MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

Handle all messages before server shutdown

Open AblEdge opened this issue 3 years ago • 2 comments

Describe your question

Is it possible to make sure broker (server) has processed all queued messages before shutdown? To accomplish message safety.

Which project is your question related to?

  • Server

AblEdge avatar May 16 '21 19:05 AblEdge

I find out .Net Core host wait 5 seconds by default to hosted service(MQTT server) stop executing and after that shutdowns the hosted service ungracefully.

the time out is configurable in ConfigureServices():

.ConfigureServices((hostContext, services) =>
{
    services.PostConfigure<HostOptions>(option =>
    {
        option.ShutdownTimeout = Timeout.InfiniteTimeSpan;
    });
});

⚠setting infinite timeout may not be a good idea in all cases.

AblEdge avatar Sep 05 '21 21:09 AblEdge

I don't think we can wait for it because (depending on the use case) a lot of devices is pushing data all the time. So the broker replies with ACK etc. So there will be no time window where it is safe to stop. Espcially QoS 0 is a problem here becuase the TCP stack wil accept new messages but the server ignores them because it is already shutting down. We can also try to send a DISCONNECT (MQTT v5) but this will lead to the problem that sill queued messages cannot be sent over to a client anymore. To me it looks like a chicken egg problem.

chkr1011 avatar Sep 09 '21 13:09 chkr1011