-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Fix ignored exceptions in MqttHostedServer startup #2102
base: master
Are you sure you want to change the base?
Conversation
I think the correct implementation should be: await the MqttServer.StartAsync() behavior in BackgroundService so that the exception can be propagated. The specific implementation is similar to https://github.com/xljiulang/MQTTnet/blob/master/Source/MQTTnet.AspnetCore/Internal/AspNetCoreMqttHostedServer.cs#L39 |
This was also my first thought. If i understand correctly this is a sort of chicken-and-egg problem: the |
If you start MqttnetServer in the IHostedService.StartAsync method, there will generally be no problems. The worst case is that you unfortunately use MqttTcpServerAdapter, which increases the service startup time. If it is used as a windows service process, it may time out and fail. |
Currently, it is changed to delayed startup. The logic of the existing code is vulnerable: when Asp.netcore is successfully started and MqttServer is not started, if there is mqtt connecting through asp.netcore, MqttConnectionHandler will be triggered to call OnConnectedAsync instead of StartAsync. |
If you want to delay starting MqttnetServer, you should fix MqttConnectionHandleras follows: public override async Task OnConnectedAsync(ConnectionContext connection)
{
var clientHandler = ClientHandler;
if (clientHandler == null)
{
connection.Abort();
_logger.Publish(MqttNetLogLevel.Warning, nameof(MqttConnectionHandler), $"{nameof(MqttServer)} has not been started yet.", null, null);
return;
}
...
} |
@xljiulang EDIT: sorry, I'm late!!! 😄 I've just took a look at your #2103 and you probably came to the same conclusion... |
The exceptions raised during the startup of
MqttHostedServer
are completely ignored.Nothing on the logs, the application continue to run, and the
MqttServer.StartedAsync
event is never raised leaving the application in an inconsistent state...I found this issue because in my application I'm also using the Tcp endpoint (on the default port 1883), but if the port is already in use obviously the Tcp listener fails with an exception.
This PR fixes the issue shutting down the application in this case.
If the user has a service registration for
Microsoft.Extensions.Logging.ILogger
(quite common in real life scenario) an error is also logged.