fix(mqtt): stale observed refs BadNoCommunication on connection loss so the identical replay recovers (05/STAB-18 shape a)
This commit is contained in:
@@ -349,6 +349,31 @@ public sealed class MqttConnection : IAsyncDisposable, IMqttSubscribeTransport,
|
||||
/// </remarks>
|
||||
public event Func<CancellationToken, Task>? Reconnected;
|
||||
|
||||
/// <summary>
|
||||
/// Raised on the <c>Connected → Reconnecting</c> edge — the session this connection had is
|
||||
/// gone. Ingest paths stale their cached values to <c>BadNoCommunication</c> here (archreview
|
||||
/// 05/STAB-18): without it, MQTT reports Good over a dead broker until the host's 90 s
|
||||
/// comms-loss watchdog notices, and MQTTnet's own supervisor usually reconnects well inside
|
||||
/// that window so the outage is never surfaced at all.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <b>Runs on MQTTnet's dispatcher thread</b> (this is raised from the library's
|
||||
/// <c>DisconnectedAsync</c> callback), so a handler must be non-blocking, must not do I/O
|
||||
/// and must not await — the same constraint <see cref="MessageReceived"/> carries. A
|
||||
/// throwing handler is caught and logged rather than escaping into the pump.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <b>Exactly once per edge, and only for a real outage.</b> It does not fire for a failed
|
||||
/// <i>initial</i> connect (there was no session to lose — the values are still
|
||||
/// <c>BadWaitingForInitialData</c>) nor during <see cref="DisposeAsync"/> (an intentional
|
||||
/// teardown, which every redeploy performs). It is deliberately NOT the mirror of
|
||||
/// <see cref="Reconnected"/>: recovery is a value the broker republishes, never an
|
||||
/// assumption this connection makes on the caller's behalf.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public event Action? ConnectionLost;
|
||||
|
||||
/// <summary>
|
||||
/// Raised for every inbound application message, on MQTTnet's own dispatcher thread. This
|
||||
/// connection does no routing, parsing or typing of its own — that is the subscription
|
||||
@@ -939,6 +964,19 @@ public sealed class MqttConnection : IAsyncDisposable, IMqttSubscribeTransport,
|
||||
if (State == MqttConnectionState.Connected)
|
||||
{
|
||||
SetState(MqttConnectionState.Reconnecting);
|
||||
|
||||
// The quality edge (05/STAB-18): exactly here, once per outage, and never for an initial
|
||||
// connect failure or a dispose — both of which are excluded by the state guard above.
|
||||
// Contained, because this runs on the library's dispatcher: a throwing ingest handler must
|
||||
// not stop the reconnect wake below.
|
||||
try
|
||||
{
|
||||
ConnectionLost?.Invoke();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogError(ex, "MQTT driver '{DriverId}': a connection-lost handler threw.", _driverId);
|
||||
}
|
||||
}
|
||||
|
||||
// Released unconditionally rather than only when the client had been connected: a missed
|
||||
|
||||
Reference in New Issue
Block a user