Skip to content

tedge mqtt commands don't timeout on connection errors #3828

@reubenmiller

Description

@reubenmiller

Describe the bug

tedge mqtt sub does not timeout when trying to subscribe to the broker that is currently unavailable.

$ TEDGE_MQTT_CLIENT_HOST=foo tedge mqtt sub '#' --duration 1s
2025-10-21T08:23:48.957289Z ERROR MQTT: Failed to connect to broker at 'foo:2883': I/O: failed to lookup address information: nodename nor servname provided, or not known
2025-10-21T08:23:49.961252Z ERROR MQTT: Failed to connect to broker at 'foo:2883': I/O: failed to lookup address information: nodename nor servname provided, or not known
2025-10-21T08:23:50.967627Z ERROR MQTT: Failed to connect to broker at 'foo:2883': I/O: failed to lookup address information: nodename nor servname provided, or not known
2025-10-21T08:23:51.974332Z ERROR MQTT: Failed to connect to broker at 'foo:2883': I/O: failed to lookup address information: nodename nor servname provided, or not known

Similar behaviour occurs when using tedge mqtt pub:

$ TEDGE_MQTT_CLIENT_HOST=foo tedge mqtt pub test message
2025-10-21T08:22:37.939864Z ERROR MQTT: Failed to connect to broker at 'foo:1883': I/O: failed to lookup address information: nodename nor servname provided, or not known
2025-10-21T08:22:38.942901Z ERROR MQTT: Failed to connect to broker at 'foo:1883': I/O: failed to lookup address information: nodename nor servname provided, or not known
2025-10-21T08:22:39.947183Z ERROR MQTT: Failed to connect to broker at 'foo:1883': I/O: failed to lookup address information: nodename nor servname provided, or not known
2025-10-21T08:22:40.954063Z ERROR MQTT: Failed to connect to broker at 'foo:1883': I/O: failed to lookup address information: nodename nor servname provided, or not known

The fact that the command's don't timeout causes the tedge diag collect command to assert its 60s timeout per plugin which adds unnecessary time to the collection of the diagnostic information for instances where the MQTT broker is not running.

tedge diag collect

To Reproduce

The behaviour can be reproduced be setting the mqtt client host to a dummy address.

# mqtt sub
TEDGE_MQTT_CLIENT_HOST=foo tedge mqtt sub '#' --duration 1s

# mqtt pub
TEDGE_MQTT_CLIENT_HOST=foo tedge mqtt pub test message

Expected behavior

The tedge mqtt sub and tedge mqtt pub should support a --connection-timeout <duration> flag where the command gives up after a specified timeout. Below shows an example of the flag in use.

tedge mqtt sub '#' --connection-timeout 15s

tedge mqtt pub test message --connection-timeout 15s
  • With a default connection timeout set to 10s (note the a human friendly duration, instead of assuming a time unit like seconds).
  • A connection timeout of "0" (e.g. "0s", "0ms" etc.) should be considered as no timeout (resulting infinite connection retries)

Screenshots

Environment (please complete the following information):

  • OS [incl. version]: any
  • Hardware [incl. revision]: any
  • System-Architecture [e.g. result of "uname -a"]: any
  • thin-edge.io version [e.g. 0.1.0]: tedge 1.6.2~243+g61154cd

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtheme:cliTheme: cli related topicstheme: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