← Back to Pull requests

fix(mqtt): give each sender its own client ID by default #2214

Open
unknown opened this pull request with 420 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 15, 2026 01:50 UTC
Author

This PR gives every MQTT service its own client ID unless the URL sets one. Two senders on one broker no longer take over each other's session.

Problem

Every MQTT service connected with the client ID shoutrrr. A broker keeps one session per client ID and disconnects a connected client when another connects with the same ID (MQTT 5, [MQTT-3.1.4-3]). Two long-lived senders on one broker, such as two programs that each notify through Shoutrrr, kept disconnecting each other, and autopaho reconnected each one. Against a local EMQX broker, two services logged 7063 "server disconnected code=142" warnings in three seconds, and some sends failed. A client ID that is not valid UTF-8, holds a null character, or is longer than 65535 bytes could not be encoded, so every connection attempt failed until the send's time ran out. The package also exported ErrPublishTimeout and ErrConnectionNotInitialized, which nothing returns.

Solution

When the URL sets no clientid, each service now chooses shoutrrr followed by 12 random base32 characters. That is 20 letters and digits, which every MQTT 5 broker must accept ([MQTT-3.1.3-5]). The service keeps that ID for every connection it opens, and Initialize chooses a new one. The connection's debug log names the ID. An explicit clientid is used as given, so a broker that accepts only known client IDs needs one set. Leaving the ID empty for the broker to assign was rejected, because brokers may refuse it and autopaho would get a new ID on every reconnect.

With a clean session, the default, no later connection resumes the session, so a graceful disconnect now asks the broker to discard it at once. Without that, each one-shot send would leave a session on the broker for the 60 seconds Shoutrrr asks for. Against EMQX, 20 one-shot sends left 20 sessions before this change and none after it. A topic or client ID that MQTT cannot encode fails when the URL is read and on each send, with ErrInvalidTopic or the new ErrInvalidClientID. The two unused errors are removed. No code outside this repository uses them. The docs say the broker must support MQTT 5, since paho.golang speaks only that version, and explain how long the broker keeps a session.

Changes

  • Choose a client ID per service when the URL sets none

  • End a clean session when its connection closes

  • Reject a client ID with a null character, invalid UTF-8, or more than 65535 bytes

  • Remove the unused ErrPublishTimeout and ErrConnectionNotInitialized

  • Document the client ID, session expiry, and the MQTT 5 requirement
  • <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    New Features
    MQTT connections now generate a unique client ID when none is configured. The ID is reused on reconnects and can be overridden.
    Connection logs now include the client ID.

    Bug Fixes
    Invalid client IDs are rejected, including IDs with null bytes, invalid UTF-8, or lengths exceeding MQTT’s limit.
    Clean sessions now signal that session state should expire on disconnect.

    Documentation
    MQTT setup guidance now explains MQTT 5 broker requirements, client IDs, session behavior, and connection failures with MQTT 3.1.1-only brokers.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 420 lines (+315 / -105) across 5 files Reviewer: coderabbitai[bot] Review duration: 9m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.45) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 420 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 93% · Reviewer familiarity: 35%
    41%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.362
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #2214"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 41.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45% (MEDIUM) · Higher = larger blast radius
    45%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 41.3% · Lower = rushed or shallow review
    41.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 26.4% · Safe to merge
    26.4%