Upgrade listener stays on a custom server after close when `preClose` is set

Chiusa Adatta ai principianti
#388 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@RaphaelFakhri ci sta già lavorando.

Dal 4/11/2026.

  • #389 di @RaphaelFakhri — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js

Direzione di ricerca

Inizia leggendo la configurazione dei listener del plugin e l’implementazione di defaultPreClose, che l’issue identifica come il punto in cui avviene attualmente la rimozione dei listener. Riproduci il caso con un preClose personalizzato e un server fornito, poi verifica che fastify.close() lasci a 0 il numero di listener upgrade con entrambi gli hook di chiusura.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

5.x

Plugin version

11.3.6

Node.js version

22.x

Operating system

Linux

Description

When you pass both options.server and a custom preClose, fastify.close() leaves the plugin's upgrade listener on the supplied server. Upgrade requests that arrive at that server after close are routed through the closed Fastify instance and get a 503 response.

#379 fixed this for the default preClose, but the listener removal lives inside defaultPreClose. A custom preClose can't remove the listener because the listener function is internal to the plugin.

Steps to reproduce
const http = require('node:http')
const Fastify = require('fastify')
const WebSocket = require('ws')

const ext = http.createServer()
ext.listen(0, async () => {
  const app = Fastify()
  await app.register(require('@fastify/websocket'), {
    options: { server: ext },
    preClose: (done) => app.websocketServer.close(done)
  })
  app.get('/', { websocket: true }, (socket) => socket.close())
  await app.ready()
  await app.close()

  console.log(ext.listenerCount('upgrade')) // 1
  const ws = new WebSocket('ws://localhost:' + ext.address().port)
  ws.on('unexpected-response', (_, res) => console.log(res.statusCode)) // 503
})
Expected behavior

fastify.close() removes the plugin's upgrade listener from the supplied server whichever preClose you use, so listenerCount('upgrade') is 0 after close.

Lingua principale
JavaScript
Stelle
467
Fork
88
Merge medio
7h 10m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di fastify/fastify-websocket

Tutte le issue di fastify/fastify-websocket

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.