socketio / socket.io-postgres-adapter

The Socket.IO Postgres adapter, allowing to broadcast events between several Socket.IO servers
https://socket.io
MIT License
24 stars 8 forks source link

Socket.IO Crash on server leaving pool #8

Closed yuvalsus-esggo closed 1 year ago

yuvalsus-esggo commented 2 years ago

Hey there,

We are currently facing an issue with fetchSockets() in a Nest.js backend using @socket.io/postgres-adapter. When one server connected to the database disconnects it throws an error and crashes the server. Error: timeout reached: only 0 responses received out of 1

We are running on AWS rds-postgres. When two se

We are pretty lost currently when it comes to ideas where to debug and how to fix this issue, therefore we're asking here. We believe this is either a problem with the postgres adapter or with socket.io itself (thus my next question: does this issue belong in this repository or should we open another issue in the socket.io repo?).

Thanks a lot for your help!

Relevant versions in use @nestjs/platform-socket.io@8.4.4 socket.io@4.5.2 @socket.io/postgres-adapter@0.3.0

darrachequesne commented 2 years ago

Hi! The error is thrown there: https://github.com/socketio/socket.io-postgres-adapter/blob/4190f4b571cfe29c3db70c68a8250817251effd0/lib/index.ts#L711-L715

You should be able to catch it though:

try {
  const sockets = await io.fetchSockets();
} catch (e) {
  // something went wrong
}

Isn't that the case?

yuvalsus-esggo commented 2 years ago

Thanks for the fast response! I've added a try-catch and it still crashes the server

`const sockets = await this.server.fetchSockets();

sockets.forEach(socket => {
  if (
    socket?.data?.registrations &&
    _.find(
      socket.data.registrations,
      (registration: string) => registration === generateIndicatorId(indicatorId, relevanceTime, organizationId)
    )
  ) {
    socket.emit("update", indicator);
  }
});`
darrachequesne commented 2 years ago

Hmm, I'm not able to reproduce: https://github.com/socketio/socket.io-fiddle/tree/postgres-adapter

https://github.com/socketio/socket.io-fiddle/blob/de5c4ef4a89fe80d8505fe8c2a3414d47ae9737e/server.js#L44-L51

The exception is properly caught and printed to the console:

  try {
    const sockets = await io.fetchSockets();

    socket.emit("count", sockets.length);
  } catch (e) {
    console.log(e); // prints "Error: timeout reached: only 0 responses received out of 1 [...]"
  }
});

Could you please check?

darrachequesne commented 1 year ago

Closed due to inactivity, please reopen if needed.