fastify-socket.io icon indicating copy to clipboard operation
fastify-socket.io copied to clipboard

Unable to close fastify server with open connections

Open giovanni-bertoncelli opened this issue 5 months ago • 5 comments

I'm reporting a bug discussed here: https://github.com/fastify/help/issues/988. It seems that fastify-socket.io is missing some logic like the one illustrated here with fastify-websocket: https://github.com/fastify/fastify-websocket/blob/431cba461f0bdd133d607deee6cfc56b69ea379a/index.js#L155-L172.

Description

When a fastify instance gets closed the fastify-socket.io plugin never closes its underlying websocket connections so the server hangs indefinitely. There should be some logic that forces the plugin to close at least the local sockets in order to close correctly the underlying HTTP server.

Reproduction

In order to reproduce the issue you can use this example https://codesandbox.io/p/devbox/socketio-close-stuck-jlcglp, and following these steps:

  • Start the server on a terminal with npm start
  • Start another server and run node client.js
  • Try close with CTRL+C the first terminal: the shutdown process will time out.

Proposal

I'd like to have some forceClose parameter in order to close the local websockets by using socket.io API:

fastify.io.local.disconnectSockets(true);

Or, since fastify has already a parameter for this check whenever forcecloseconnections is true and force the disconnection.

Another option can be some preClose parameter with a design similar to the mentioned fastify-websocket.

giovanni-bertoncelli avatar Jan 30 '24 14:01 giovanni-bertoncelli