MQTT.js icon indicating copy to clipboard operation
MQTT.js copied to clipboard

[Bug]: wrong connection string with a port puts mqtt client into an endless loop

Open maximivanov opened this issue 11 months ago • 12 comments

MQTTjs Version

5.4.0

Broker

Doesn't matter (testing connection to a wrong hostname)

Environment

NodeJS

Description

When trying to connect to a wrong URL that contains a port, the client never throws an error

Minimal Reproduction

Verify versions:

❯ node --version
v20.9.0

❯ npm --version
10.2.5

❯ npm list | grep mqtt
├── [email protected]

Add a script:

import * as mqtt from 'mqtt';

// this is a non existent hostname and it's part of the test
const url = 'mqtt://a7dc229b79e0425222318eb495f0dddd.s1.eu.hivemq.cloud:8883';

const mqttClient = await mqtt.connectAsync(url, {
  connectTimeout: 5000,
  username: 'does not matter',
  password: 'does not matter',
});

await mqttClient.endAsync();

Run the script:

DEBUG='mqttjs*' node mqtt-connect-issue.mjs

Expected: process errors after 5 seconds because the hostname is not reachable.

Actual: process never exits.

Note: if you remove the port part (:8883) from the connection string, it will correctly time out and throw after 5 seconds.

Debug logs

❯ DEBUG='mqttjs*' node mqtt-connect-issue.mjs
  mqttjs connecting to an MQTT broker... +0ms
  mqttjs:client MqttClient :: version: undefined +0ms
  mqttjs:client MqttClient :: environment node +0ms
  mqttjs:client MqttClient :: options.protocol mqtt +0ms
  mqttjs:client MqttClient :: options.protocolVersion 4 +0ms
  mqttjs:client MqttClient :: options.username does not matter +0ms
  mqttjs:client MqttClient :: options.keepalive 60 +0ms
  mqttjs:client MqttClient :: options.reconnectPeriod 1000 +0ms
  mqttjs:client MqttClient :: options.rejectUnauthorized undefined +0ms
  mqttjs:client MqttClient :: options.properties.topicAliasMaximum undefined +0ms
  mqttjs:client MqttClient :: clientId mqttjs_a4da9d11 +1ms
  mqttjs:client MqttClient :: setting up stream +0ms
  mqttjs:client connect :: calling method to clear reconnect +1ms
  mqttjs:client _clearReconnect : clearing reconnect timer +0ms
  mqttjs:client connect :: using streamBuilder provided to client to create stream +0ms
  mqttjs calling streambuilder for mqtt +5ms
  mqttjs:tcp port 8883 and host a7dc229b79e0425222318eb495f0dddd.s1.eu.hivemq.cloud +0ms
  mqttjs:client connect :: pipe stream to writable stream +7ms
  mqttjs:client connect: sending packet `connect` +3ms
  mqttjs:client _writePacket :: packet: {
  mqttjs:client   cmd: 'connect',
  mqttjs:client   protocolId: 'MQTT',
  mqttjs:client   protocolVersion: 4,
  mqttjs:client   clean: true,
  mqttjs:client   clientId: 'mqttjs_a4da9d11',
  mqttjs:client   keepalive: 60,
  mqttjs:client   username: 'does not matter',
  mqttjs:client   password: 'does not matter',
  mqttjs:client   properties: undefined
  mqttjs:client } +0ms
  mqttjs:client _writePacket :: emitting `packetsend` +2ms
  mqttjs:client _writePacket :: writing to stream +0ms
  mqttjs:client _writePacket :: writeToStream result true +18ms
  mqttjs:client (mqttjs_a4da9d11)stream :: on close +188ms
  mqttjs:client _flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function +1ms
  mqttjs:client stream: emit close to MqttClient +0ms
  mqttjs:client close :: connected set to `false` +0ms
  mqttjs:client close :: clearing connackTimer +0ms
  mqttjs:client close :: clearing ping timer +0ms
  mqttjs:client close :: calling _setupReconnect +0ms
  mqttjs:client _setupReconnect :: emit `offline` state +0ms
  mqttjs:client _setupReconnect :: set `reconnecting` to `true` +0ms
  mqttjs:client _setupReconnect :: setting reconnectTimer for 1000 ms +0ms
  mqttjs:client reconnectTimer :: reconnect triggered! +1s
  mqttjs:client _reconnect: emitting reconnect to client +0ms
  mqttjs:client _reconnect: calling connect +0ms
  mqttjs:client connect :: calling method to clear reconnect +0ms
  mqttjs:client _clearReconnect : clearing reconnect timer +0ms
  mqttjs:client connect :: using streamBuilder provided to client to create stream +0ms
  mqttjs calling streambuilder for mqtt +1s
  mqttjs:tcp port 8883 and host a7dc229b79e0425222318eb495f0dddd.s1.eu.hivemq.cloud +1s
  mqttjs:client connect :: pipe stream to writable stream +1ms
  mqttjs:client connect: sending packet `connect` +0ms
  ...

maximivanov avatar Mar 19 '24 23:03 maximivanov