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

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

Open
JoelBrenstrum opened this issue Mar 28, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@JoelBrenstrum
Copy link

JoelBrenstrum commented Mar 28, 2024

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

private ondata(data): void {

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)
@JoelBrenstrum JoelBrenstrum added the to triage Waiting to be triaged by a member of the team label Mar 28, 2024
@darrachequesne
Copy link
Member

I was indeed able to reproduce the issue, thanks. I'm digging into this.

@darrachequesne darrachequesne added bug Something isn't working and removed to triage Waiting to be triaged by a member of the team labels Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants