Repository navigation
fix(server): register @fastify/websocket once at the root (#545) - #620
Conversation
Every WebSocket-bearing feature (notifications, nostr relay, webrtc,
terminal, tunnel, app plugins) registered @fastify/websocket in its own
encapsulated context. Each registration adds an 'upgrade' listener to the
one shared HTTP server, while the websocketServer decorator it sets is only
visible inside the registering context, so the per-plugin
`if (!fastify.websocketServer)` guards never saw each other's registration.
With N features enabled the server had N upgrade listeners. The first one
claims the socket; the other N-1 throw ERR_HTTP_SOCKET_ASSIGNED, which
@fastify/websocket catches and logs as "websocket upgrade failed" — once
per listener, on every connection, including successful ones. A production
instance with four features enabled produced ~790k such lines in nine
months (28% of its log).
Register the plugin once in createServer, before the feature plugins,
whenever any of them is enabled. Root decorators and the onRoute hook are
inherited, so the existing guards now correctly skip and `{ websocket: true }`
routes in child plugins keep working. The notifications plugin gets the
same guard instead of registering unconditionally.
The new test asserts listenerCount('upgrade') is 0 with no WebSocket
feature and 1 with one or all of them; it fails on the previous code with
actual: 5.
There was a problem hiding this comment.
🔵 Needs a closer look
It changes core server WebSocket registration semantics affecting six features and relies on subtle Fastify decorator-inheritance and boot-ordering behavior that warrants human verification of runtime WS routes.
0 open findings
What changed in this PR
This PR fixes the recurring websocket upgrade failed / ERR_HTTP_SOCKET_ASSIGNED warnings described in issue #545. Previously each WebSocket-bearing feature (notifications, nostr, webrtc, terminal, tunnel, app plugins) registered @fastify/websocket in its own encapsulated context. Because @fastify/websocket is wrapped with fastify-plugin, each registration added an upgrade listener to the shared HTTP server while the websocketServer decorator remained scoped per context, so the per-plugin guards couldn't see one another. The fix registers the plugin exactly once at the root (before any feature plugin) so the decorator and onRoute hook are inherited by all children, and the existing if (!fastify.websocketServer) guards now correctly skip.
Changes:
- Register
@fastify/websocketonce at the root increateServer, gated on whether any WebSocket-using feature is enabled. - Update the notifications plugin to guard its registration (
if (!fastify.websocketServer)) instead of registering unconditionally. - Add a regression test asserting the server keeps exactly one
upgradelistener regardless of how many WebSocket features are enabled.
| File | Description |
|---|---|
| src/server.js | Imports @fastify/websocket and registers it once at the root (before feature plugins) when any WS feature is enabled, with an explanatory comment referencing #545. |
| src/notifications/index.js | Guards its own websocket registration so it no longer adds a duplicate upgrade listener when the root already registered the plugin. |
| test/websocket-registration.test.js | New test verifying upgrade listener count is 0 with no WS feature and exactly 1 with one or all features enabled. |
🧠 Review effort: Balanced
Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.
Since 0.0.220: - --git defers to plugin-owned prefixes (#616, #630): core's git preHandler returns early for a URL below an app path, so a plugin serving git smart-HTTP under its mount (the forge at /forge/<owner>/<name>.git) is no longer shadowed by core's 401. Pod git outside plugin prefixes is unchanged. - CORS exposes WWW-Authenticate, Last-Modified and Accept-Query to cross-origin clients (#629). - @fastify/websocket registered once at the root (#545, #620). - Hardening: atomic quota reservation and idempotent deposit credit (#619).
Fixes the
websocket upgrade failed/ERR_HTTP_SOCKET_ASSIGNEDwarnings from #545 (diagnosis in this comment).Problem
Each WebSocket-bearing feature registered
@fastify/websocketin its own encapsulated context. Every registration adds anupgradelistener to the shared HTTP server, but thewebsocketServerdecorator it sets is scoped to the registering context, so the per-pluginif (!fastify.websocketServer)guards could not see each other's registration. With N features enabled the server had N listeners; on every upgrade the first claimed the socket and the other N-1 threwERR_HTTP_SOCKET_ASSIGNED, logged by@fastify/websocketaswebsocket upgrade failed. On an instance with notifications + nostr + webrtc + tunnel that was ~3 warnings per connection, ~790k lines over nine months.Change
src/server.js: register@fastify/websocketonce at the root, before the feature plugins, when any of notifications / live reload / nostr / webrtc / terminal / tunnel / app plugins is enabled. Root decorators and theonRoutehook are inherited, so the existing guards in the relay, webrtc, terminal, tunnel and plugin-API code now skip, and{ websocket: true }routes in child plugins keep working.src/notifications/index.js: use the same guard instead of registering unconditionally.test/websocket-registration.test.js: assertsserver.listenerCount('upgrade')is 0 with no WebSocket feature and 1 with one or all of them. On currentgh-pagesthe all-features case fails withactual: 5.No code reads
fastify.websocketServeror.clients, so a single shared instance changes no behaviour. This is the registration pattern the@fastify/websocketREADME recommends.Testing
gh-pages(actual 5, expected 1), passes with this change.npm test(full suite) green locally.