Skip to content

Fix ASP connection handler startup race - #2255

Open
suhashollakc wants to merge 1 commit into
dotnet:masterfrom
suhashollakc:suhas/fix-asp-connection-handler-race
Open

Fix ASP connection handler startup race#2255
suhashollakc wants to merge 1 commit into
dotnet:masterfrom
suhashollakc:suhas/fix-asp-connection-handler-race

Conversation

@suhashollakc

Copy link
Copy Markdown
Contributor

Fixes #2173

Summary
This fixes a startup race in the ASP.NET Core connection handler.

If ASP.NET routes a connection to MqttConnectionHandler.OnConnectedAsync before the MQTT server adapter has been started, _serverOptions is still null and the handler throws a NullReferenceException. The connection should be terminated cleanly instead of remaining in an invalid handler state.

Changes

  • Abort incoming ASP.NET Core connections when the MQTT server adapter has not been started or no client handler is wired.
  • Read server options and the client handler into locals before constructing the MQTT connection context.
  • Add a regression test covering the not-started handler path.

Validation

  • dotnet test --project Source\MQTTnet.Tests\MQTTnet.Tests.csproj --framework net10.0 --no-restore --filter ConnectionHandler_Aborts_Connection_When_Server_Not_Started
  • dotnet test --project Source\MQTTnet.Tests\MQTTnet.Tests.csproj --framework net10.0 --no-restore --filter FullyQualifiedName~MQTTnet.Tests.ASP
  • dotnet build MQTTnet.slnx --no-restore

Note: the solution build completes successfully with existing benchmark warnings about obsolete ASP.NET hosting APIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MqttConnectionHandler race condition

2 participants