Skip to content

Improve tedge mqtt sub error handling #1797

@Bravo555

Description

@Bravo555

Describe the bug

Maybe it's not a bug per se, but while working on MQTT client authentication, when the broker refuses the connection, the tedge mqtt sub tedge/measurements command does not exit, but keeps attempting to connect and outputting ERROR: Connection refused, return code: NotAuthorized without exiting. This is different from the case when the broker is not even running, and the process exits with the following message:

Error: failed to subscribe the topic "tedge/measurements" with QoS "AtMostOnce".

Caused by:
    MQTT connection error: I/O: Connection refused (os error 111)
    
    Hint: Is MQTT server running?

The process also correctly exits with a failure when we attempt to publish a message instead of subscribing.

To Reproduce

  1. In /etc/tedge/mosquitto-conf/tedge-mosquitto.conf, change allow_anonymous to false
  2. sudo systemctl restart mosquitto
  3. tedge mqtt sub tedge/measurements
  4. The command should print the NotAuthorized error message every 1s.

Expected behavior
I think this behaviour should be made more consistent and the process should exit first time it receives NotAuthorized message from the broker, as it is unlikely that retrying would help, and so that the command can properly report the failure if it's being called e.g. from a script. There are probably other possible errors where we should just exit and report the failure to the users, and perhaps others where exiting would be unwise, and some other strategy, like exponential backoff, should be used. While fixing the behaviour for NotAuthorized case, we could consider these other cases as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    improvementUser valuetheme:mqttTheme: mqtt and mosquitto related topics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions