dotnet / dotnet/MQTTnet

MqttConnectionHandler race condition

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

Description

The initial state of `MqttConnectionHandler` can result in a `NullReferenceException` in the time between generic host starup and "full" startup. In my test environment this occurs consistently. For a workaround, I explicitly pull and inject the dependencies by calling `StartAsync` after building and prior to starting the host.

As I understand it, the connection should be terminated rather than remain unhandled in an invalid state.

```diff
public override async Task OnConnectedAsync(ConnectionContext connection)
{
...
var clientHandler = ClientHandler;
if (clientHandler != null)
{
await clientHandler(adapter).ConfigureAwait(false);
}
+ else
+ {
+ connection.Abort();
+ }
}
```

_Initially called out by @xljiulang in https://github.com/dotnet/MQTTnet/issues/2102#issuecomment-2475868014_

Edited for clarity and to sync up to current master

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.