Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deno TLS Websocket Connect issue since 2.1.5 (e.g. Mqtt.js) #27694

Closed
mariusheil opened this issue Jan 16, 2025 · 3 comments · Fixed by #27792
Closed

Deno TLS Websocket Connect issue since 2.1.5 (e.g. Mqtt.js) #27694

mariusheil opened this issue Jan 16, 2025 · 3 comments · Fixed by #27792
Assignees

Comments

@mariusheil
Copy link

mariusheil commented Jan 16, 2025

Summary

To Reproduce

Run this minimal script with Deno 2.1.4 and with Deno 2.1.5:

deno upgrade 2.1.4
deno run --allow-all Test.ts
deno upgrade 2.1.5
deno run --allow-all Test.ts
import * as mqtt from "npm:mqtt";

console.log("Current Deno version", Deno.version.deno);
console.log("MQTT Connecting....");
let mqttJsClient = mqtt.connect({
    protocol:"wss",
    hostname:"test.mosquitto.org",
    port:8091,
    path:"/",
    clientId:"test" + Math.random(),
    username:"rw",
    password:"readwrite",
    rejectUnauthorized:true,
    clean:true,
    reconnectPeriod:10 * 1000
});

mqttJsClient.on('connect', () => {
    console.log("Successfully Connected!");
    Deno.exit(0);
});

mqttJsClient.on('reconnect', () => {
    console.log("Reconnecting");
});

mqttJsClient.on('close', () => {
    console.log("Closed");
});

mqttJsClient.on('error', () => {
    console.log("Error");
});

Output

$ deno upgrade 2.1.4
Upgraded successfully to Deno v2.1.4 (stable)
$ deno run --allow-all Test.ts
Current Deno version 2.1.4
MQTT Connecting....
Warning: Not implemented: ClientRequest.options.createConnection
Successfully Connected!
$ deno upgrade 2.1.5
Upgraded successfully to Deno v2.1.5 (stable)
$ deno run --allow-all Test.ts
Current Deno version 2.1.5
MQTT Connecting....
Closed
Reconnecting
Closed
Reconnecting
Closed
[.....]

Further Info:

  • I was testing this on Windows 11, if it should matter
  • Deno version 2.1.4 did not have the clientRequestOptions.createConnection implemented correctly, so the Websocket connection might have used some fallback method which it is not using right now, which has never worked as intended.
  • Trying to connect to the unencrypted websocket works as expected, but the encrypted one fails:
//Works for Deno 2.1.5
let mqttJsClient = mqtt.connect({
    protocol:"ws",
    hostname:"test.mosquitto.org",
    port:8090,
    path:"/",
    clientId:"test" + Math.random(),
    username:"rw",
    password:"readwrite",
    clean:true,
    reconnectPeriod:10 * 1000
});
//Fails for Deno 2.1.5
let mqttJsClient = mqtt.connect({
    protocol:"wss",
    hostname:"test.mosquitto.org",
    port:8091,
    path:"/",
    clientId:"test" + Math.random(),
    username:"rw",
    password:"readwrite",
    clean:true,
    reconnectPeriod:10 * 1000
});

Some logs when enabling the DEBUG env for MQTT.js:

MqttService                          INFO  2025-01-15T12:14:38.820Z: MQTT Connecting....
  mqttjs connecting to an MQTT broker... +0ms
  mqttjs:client MqttClient :: version: 5.10.1 +0ms
  mqttjs:client MqttClient :: environment node +0ms
  mqttjs:client MqttClient :: options.protocol wss +1ms
  mqttjs:client MqttClient :: options.protocolVersion 4 +0ms
  mqttjs:client MqttClient :: options.username Token-MYTOKEN +0ms
  mqttjs:client MqttClient :: options.keepalive 60 +0ms
  mqttjs:client MqttClient :: options.reconnectPeriod 10000 +0ms
  mqttjs:client MqttClient :: options.rejectUnauthorized false +1ms
  mqttjs:client MqttClient :: options.properties.topicAliasMaximum undefined +0ms
  mqttjs:client MqttClient :: clientId Token-MYTOKEN__1736943278820 +0ms
  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 wss +21ms
  mqttjs:ws streamBuilder +0ms
  mqttjs:ws createWebSocket +1ms
  mqttjs:ws protocol: MQTT 4 +0ms
  mqttjs:ws creating new Websocket for url: wss://myexampleserver:443/ws and protocol: mqtt +0ms
Warning: Not implemented: ClientRequest.options.createConnection
  mqttjs:client connect :: pipe stream to writable stream +3ms
  mqttjs:client connect: sending packet `connect` +0ms
  mqttjs:client _writePacket :: packet: {
  mqttjs:client   cmd: 'connect',
  mqttjs:client   protocolId: 'MQTT',
  mqttjs:client   protocolVersion: 4,
  mqttjs:client   clean: true,
  mqttjs:client   clientId: 'Token-MYTOKEN__1736943278820',
  mqttjs:client   keepalive: 60,
  mqttjs:client   username: 'Token-MYTOKEN',
  mqttjs:client   password: 'myPass',
  mqttjs:client   properties: undefined
  mqttjs:client } +1ms
  mqttjs:client _writePacket :: emitting `packetsend` +1ms
  mqttjs:client _writePacket :: writing to stream +0ms
  mqttjs:client _writePacket :: writeToStream result true +28ms
MetadataService                      INFO  2025-01-15T12:14:39.086Z: Orga Name: Marius
  mqttjs:client writable stream :: parsing buffer +671ms
  mqttjs:client parser :: on packet push to packets array. +2ms
  mqttjs:client work :: getting next packet in queue +1ms
  mqttjs:client work :: packet pulled from queue +0ms
  mqttjs:client _handlePacket :: emitting packetreceive +0ms
  mqttjs:client _handleConnack +1ms
  mqttjs:client _setupKeepaliveManager :: keepalive 60 (seconds) +0ms
  mqttjs:client KeepaliveManager: set keepalive to 60000ms +0ms
  mqttjs:client connect :: sending queued packets +2ms
  mqttjs:client deliver :: entry undefined +0ms
  mqttjs:client _resubscribe +0ms
MqttService                          INFO  2025-01-15T12:14:39.552Z: MQTT Connected

I am now getting a disconnect after a very short time:

Broken in Deno 2.1.5:

MqttService                          INFO  2025-01-15T12:11:46.451Z: MQTT Connecting....
  mqttjs connecting to an MQTT broker... +0ms
  mqttjs:client MqttClient :: version: 5.10.1 +0ms
  mqttjs:client MqttClient :: environment node +0ms
  mqttjs:client MqttClient :: options.protocol wss +0ms
  mqttjs:client MqttClient :: options.protocolVersion 4 +1ms
  mqttjs:client MqttClient :: options.username Token-MYTOKEN +0ms
  mqttjs:client MqttClient :: options.keepalive 60 +0ms
  mqttjs:client MqttClient :: options.reconnectPeriod 10000 +0ms
  mqttjs:client MqttClient :: options.rejectUnauthorized false +0ms
  mqttjs:client MqttClient :: options.properties.topicAliasMaximum undefined +0ms
  mqttjs:client MqttClient :: clientId Token-MYTOKEN__1736943106451 +0ms
  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 wss +12ms
  mqttjs:ws streamBuilder +0ms
  mqttjs:ws createWebSocket +0ms
  mqttjs:ws protocol: MQTT 4 +0ms
  mqttjs:ws creating new Websocket for url: wss://myexampleserver.io:443/ws and protocol: mqtt +0ms
  mqttjs:client connect :: pipe stream to writable stream +16ms
  mqttjs:client connect: sending packet `connect` +1ms
  mqttjs:client _writePacket :: packet: {
  mqttjs:client   cmd: 'connect',
  mqttjs:client   protocolId: 'MQTT',
  mqttjs:client   protocolVersion: 4,
  mqttjs:client   clean: true,
  mqttjs:client   clientId: 'Token-MYTOKEN__1736943106451',
  mqttjs:client   keepalive: 60,
  mqttjs:client   username: 'Token-MYTOKEN',
  mqttjs:client   password: 'myPass',
  mqttjs:client   properties: undefined
  mqttjs:client } +0ms
  mqttjs:client _writePacket :: emitting `packetsend` +1ms
  mqttjs:client _writePacket :: writing to stream +0ms
  mqttjs:client _writePacket :: writeToStream result true +28ms
MetadataService                      INFO  2025-01-15T12:11:46.733Z: Orga Name: Marius
  mqttjs:client streamErrorHandler :: error Bad resource ID +317ms
  mqttjs:client noop :: BadResource: Bad resource ID
    at node:http:306:27
    at HttpsClientRequest._writeHeader (node:http:398:7)
    at HttpsClientRequest._flushHeaders (node:_http_outgoing:382:12)
    at TLSSocket.onConnect (node:http:444:16)
    at TLSSocket.emit (ext:deno_node/_events.mjs:405:35)
    at _afterConnect (node:net:159:12)
    at _afterConnectMultiple (node:net:214:3)
    at TCP.afterConnect (ext:deno_node/internal_binding/connection_wrap.ts:43:11)
    at TCP.handle.afterConnect (node:_tls_wrap:157:29)
    at eventLoopTick (ext:core/01_core.js:175:7) {
  name: 'BadResource'
} +0ms
  mqttjs:client (Token-MYTOKEN__1736943106451)stream :: on close +1ms
  mqttjs:client _flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function +0ms
  mqttjs:client stream: emit close to MqttClient +0ms
  mqttjs:client close :: connected set to `false` +1ms
  mqttjs:client close :: clearing connackTimer +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 10000 ms +0ms
MqttService                          WARN  2025-01-15T12:11:46.829Z: MQTT Disconnected
@mariusheil mariusheil changed the title Deno Websocket Connect issue since 2.1.5 (e.g. Mqtt.js) Deno TLS Websocket Connect issue since 2.1.5 (e.g. Mqtt.js) Jan 16, 2025
@kt3k kt3k self-assigned this Jan 16, 2025
@brettchalupa
Copy link

Seeing a similar issue where Deno 2.1.5 introduced a "Bad resource ID" error when using npm:puppeteer@^23.11 to make a websocket connection. Here's the full error when running:

await puppeteer.connect({ browserWSEndpoint: `${browserWSEndpoint}` })

Error:

ErrorEvent {
  [Symbol(kTarget)]: WebSocket {
    _events: [Object: null prototype] {
      open: [Function: onOpen] {
        [Symbol(kIsForOnEventAttribute)]: false,
        [Symbol(kListener)]: [Function (anonymous)]
      },
      error: [Function: onError] {
        [Symbol(kIsForOnEventAttribute)]: false,
        [Symbol(kListener)]: [Function (anonymous)]
      }
    },
    _eventsCount: 2,
    _maxListeners: undefined,
    _binaryType: "nodebuffer",
    _closeCode: 1006,
    _closeFrameReceived: false,
    _closeFrameSent: false,
    _closeMessage: <Buffer >,
    _closeTimer: null,
    _errorEmitted: true,
    _extensions: {},
    _paused: false,
    _protocol: "",
    _readyState: 3,
    _receiver: null,
    _sender: null,
    _socket: null,
    _bufferedAmount: 0,
    _isServer: false,
    _redirects: 0,
    _autoPong: true,
    _url: "wss://production-sfo.browserless.io/e/SOMELONGID/devtools/browser/SOME-UUID",
    _originalIpc: false,
    _originalSecure: true,
    _originalHostOrSocketPath: "production-sfo.browserless.io",
    _req: null,
    [Symbol(kCapture)]: false
  },
  [Symbol(kType)]: "error",
  [Symbol(kError)]: BadResource: Bad resource ID
    at node:http:306:27
    at HttpsClientRequest._writeHeader (node:http:398:7)
    at HttpsClientRequest._flushHeaders (node:_http_outgoing:382:12)
    at TLSSocket.onConnect (node:http:444:16)
    at TLSSocket.emit (ext:deno_node/_events.mjs:405:35)
    at TCPConnectWrap._afterConnect [as oncomplete] (node:net:159:12)
    at TCP.afterConnect (ext:deno_node/internal_binding/connection_wrap.ts:43:11)
    at TCP.handle.afterConnect (node:_tls_wrap:157:29)
    at eventLoopTick (ext:core/01_core.js:175:7) {
    name: "BadResource"
  },
  [Symbol(kMessage)]: "Bad resource ID"
}

I can confirm that downgrading to Deno 2.1.4 stops that error from occurring.

@pr0pz
Copy link

pr0pz commented Jan 21, 2025

I have the same issue can can also confirm that downgrading to 2.1.4 stops the error.

I use twurple/eventsub-ws and also the deno built-in websocket connection.
npm:@twurple/eventsub-ws@^7.2.0

error: Uncaught Error: Client network socket disconnected before secure TLS connection was established
    at connResetException (ext:deno_node/internal/errors.ts:1912:14)
    at TLSSocket.onConnectEnd (node:_tls_wrap:32:19)
    at TLSSocket.emit (ext:deno_node/_events.mjs:405:35)
    at endReadableNT (ext:deno_node/_stream.mjs:3210:16)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:36:15)
    at runNextTicks (ext:deno_node/_next_tick.ts:76:3)
    at eventLoopTick (ext:core/01_core.js:182:21)

@mariusheil
Copy link
Author

Just an update: still happens in Deno 2.1.7, Frix from PR #27707 that sounded related did not fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants