Skip to content

Websocket connections to secondary server prevents server from shutting down #5180

@kingston

Description

@kingston

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.24.3

Plugin version

No response

Node.js version

20.10.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

14.1.1

Description

When a websocket connection is connected to a secondary server (e.g. when we are listening to both 127.0.0.1 (secondary) and ::1 (primary) and a client connects to 127.0.0.1), we don't run the preClose operations prior to closing the secondary server. This causes the server close hook to hang indefinitely since the websocket connection is still live.

I tried debugging the issue and it appears to be related to this line (https://github.com/fastify/fastify/blob/main/lib/server.js#L178) where Fastify attaches a onClose handler that tries to close the secondary server without calling any preClose hooks unlike the primary server (https://github.com/fastify/fastify/blob/main/fastify.js#L433). This means that @fastify/websocket is unable to shutdown the sockets appropriately leading the secondary server to stall the close process indefinitely.

I was wondering if it was on @fastify/websocket side to fix, but I don't see a way they can solve this without a fix on Fastify's side since it appears the Fastify close process is hanging before any plugin can do any clean up.

Steps to Reproduce

I created a minimal reproduction repository here: https://github.com/kingston/fastify-websocket-repro

  1. Run node index.js
  2. Open index.html
  3. Ctrl-C the node process

Expected Behavior

Fastify shuts down cleanly without timing out

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions