Skip to content

A parser error causes the socket connection to reconnect but the old connection is not closed #5128

@JoelBrenstrum

Description

@JoelBrenstrum

If a parser error is encountered the socket connection is restarted but the old one is not closed

https://github.com/socketio/socket.io-client/blob/4f6030f2c4394bc51d63fe27ed3310d95b9e0a74/lib/manager.ts#L425
image

Socket.IO server version: 4.1.2

Server

import { Server } from "socket.io";

const io = new Server(3000, {});

io.on("connection", (socket) => {
  console.log(`connect ${socket.id}`);

  socket.on("disconnect", () => {
    console.log(`disconnect ${socket.id}`);
  });

  socket.on('notify', (msg: { channel: string; data: any }, callback) => {
    const { channel, data } = msg;
    socket.broadcast.emit(channel, data);
  });
});

Socket.IO client version: 4.7.2

Client

import { io } from "socket.io-client";

const socket = io("ws://localhost:3000/", {});

socket.on("connect", () => {
  console.log(`connect ${socket.id}`);
});

socket.on("disconnect", () => {
  console.log("disconnect");
});

//invalid emit, correct shape would be {channel: 'channel name': data: {records: []}}
socket.emit('notify', {records:[]})

We had an invalid emit in our code that resulted in us broadcasting (null, null) from the server. This was fine until a semi recent update to the socket.io-parser (socketio/socket.io-parser@3b78117). It causes the client to throw a parser error and to create a second connection to the hub, and slowly over time, this leaks a lot of connections.

I would expect that if the reconnection code is called on the client there is an attempt to safely close the old connection or to check that it is dead before attempting a reconnection. or to skip this behaviour for a parser error

Platform:

  • Device: M1 MacBook pro
  • OS: 14.1.1 (23B81)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage:socket.io-clientThis concerns the "socket.io-client" package

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions