Repository: MassTransit/MassTransit

Issue #2645: Endpoint health check fails with NullReferenceException
Created: 2021-06-28T09:11:15Z
Is this a bug report?
Yes
Can you also reproduce the problem with the latest version?
We're running 7.2.0.
Environment

Dotnet version: 5.0.x
Operating environment: Azure App Services (most recent ASP MVC Core)

Steps to Reproduce
In AppInsights, we see somewhat occasional health check errors related to MassTransit. See the stack trace below.
System.NullReferenceException:
   at MassTransit.Transports.ReceiveEndpointCollection+<>c.<CheckHealth>b__11_3 (MassTransit, Version=7.2.0.0, Culture=neutral, PublicKeyToken=b8e0e9f2f1e657fa)
   at System.Linq.Enumerable+SelectArrayIterator`2.ToArray (System.Linq, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   at MassTransit.Transports.ReceiveEndpointCollection.CheckHealth (MassTransit, Version=7.2.0.0, Culture=neutral, PublicKeyToken=b8e0e9f2f1e657fa)
   at MassTransit.Transports.BaseHost.CheckHealth (MassTransit, Version=7.2.0.0, Culture=neutral, PublicKeyToken=b8e0e9f2f1e657fa)
   at MassTransit.MassTransitBus.CheckHealth (MassTransit, Version=7.2.0.0, Culture=neutral, PublicKeyToken=b8e0e9f2f1e657fa)
   at MassTransit.AspNetCoreIntegration.HealthChecks.BusHealthCheck.Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck.CheckHealthAsync (MassTransit.AspNetCoreIntegration, Version=7.2.0.0, Culture=neutral, PublicKeyToken=b8e0e9f2f1e657fa)
   at Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService+<RunCheckAsync>d__5.MoveNext (Microsoft.Extensions.Diagnostics.HealthChecks, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)

This stack trace seems to correlate to the following anonymous method in ReceiveEndpointCollection.CheckHealth, meaning that either .InputAddress or .AbsolutePath is not set:
var names = unhappy.Select(x => x.InputAddress.AbsolutePath.Split('/').LastOrDefault()).ToArray();

We didn't try to reproduce the behaviour locally, but I can tell something about our setup:
We use MassTransit against Azure Service Bus. Most of the consumers are registered explicitly as instances (busConfig.ReceiveEndpoint(..., epConfig => epConfig.Instance(...))) in contrast to registration by convention. With this setup, we already had health check issues with MassTransit 7.1.8 that went away by updating to version 7.2.0.
Well, maybe not fully, but the health check code seems to be under somewhat active development.
Expected Behavior

(Write what you thought would happen.)
Actual Behavior

(Write what happened. Please add screenshots!)
Reproducible Demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

Duplicate issue #2848: NullReferenceException in Health Check
Created: 2021-09-21T19:09:49Z
We have NullReferenceException in our logs when we trigger our pod’s health check, the exception is there even the bus is healthy.
Stack Trace:
fail: Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService[104]
      Health check masstransit-bus threw an unhandled exception after 12.7546ms
      System.NullReferenceException: Object reference not set to an instance of an object.
         at MassTransit.Transports.ReceiveEndpointCollection.<>c.<CheckHealth>b11_3(<>fAnonymousType56`2 x) in MassTransit.dll:token 0x6003457+0x0
         at System.Linq.Enumerable.SelectArrayIterator`2.ToArray() in System.Linq.dll:token 0x60001c3+0x12
         at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) in System.Linq.dll:token 0x600011a+0x1b
         at MassTransit.Transports.ReceiveEndpointCollection.CheckHealth(BusState busState, String healthMessage) in MassTransit.dll:token 0x6000cf7+0xb2
         at MassTransit.Transports.BaseHost.CheckHealth(BusState busState, String healthMessage) in MassTransit.dll:token 0x6000c6c+0x0
         at MassTransit.MassTransitBus.CheckHealth() in MassTransit.dll:token 0x60009de+0x0
         at MassTransit.AspNetCoreIntegration.HealthChecks.BusHealthCheck.Microsoft.Extensions.Diagnostics.HealthChecks.IHealthCheck.CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken) in MassTransit.AspNetCoreIntegration.dll:token 0x6000013+0x0
         at Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService.RunCheckAsync(HealthCheckRegistration registration, CancellationToken cancellationToken) in Microsoft.Extensions.Diagnostics.HealthChecks.dll:token 0x6000024+0x107
Environment
 .NET 6 Preview 7 / MassTransit 7.2.1

Closing comment (2021-09-21T20:16:39Z): Duplicate of #2645
Which was already fixed and released in 7.2.2.