Skip to content

fix(server): register @fastify/websocket once at the root (#545) - #620

Merged
melvincarvalho merged 1 commit into
gh-pagesfrom
fix/websocket-single-registration
Oct 8, 2026
Merged

melvincarvalho merged 1 commit into
gh-pagesfrom
fix/websocket-single-registration

Conversation

@melvincarvalho

Copy link
Copy Markdown
Contributor

Fixes the websocket upgrade failed / ERR_HTTP_SOCKET_ASSIGNED warnings from #545 (diagnosis in this comment).

Problem

Each WebSocket-bearing feature registered @fastify/websocket in its own encapsulated context. Every registration adds an upgrade listener to the shared HTTP server, but the websocketServer decorator it sets is scoped to the registering context, so the per-plugin if (!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 threw ERR_HTTP_SOCKET_ASSIGNED, logged by @fastify/websocket as websocket 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/websocket once at the root, before the feature plugins, when any of notifications / live reload / nostr / webrtc / terminal / tunnel / app plugins is enabled. Root decorators and the onRoute hook 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: asserts server.listenerCount('upgrade') is 0 with no WebSocket feature and 1 with one or all of them. On current gh-pages the all-features case fails with actual: 5.

No code reads fastify.websocketServer or .clients, so a single shared instance changes no behaviour. This is the registration pattern the @fastify/websocket README recommends.

Testing

  • New test fails on gh-pages (actual 5, expected 1), passes with this change.
  • npm test (full suite) green locally.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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/websocket once at the root in createServer, 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 upgrade listener 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.

@melvincarvalho
melvincarvalho merged commit ca88517 into gh-pages Oct 8, 2026
2 checks passed
@melvincarvalho
melvincarvalho deleted the fix/websocket-single-registration branch October 8, 2026 08:15
melvincarvalho added a commit that referenced this pull request Oct 18, 2026
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants