Skip to content

fix(websockets): resolve global scoped enhancers per connection - #17979

Merged
kamilmysliwiec merged 3 commits into
nestjs:masterfrom
jackie-cqz:fix/ws-global-scoped-enhancers
Oct 2, 2026
Merged

kamilmysliwiec merged 3 commits into
nestjs:masterfrom
jackie-cqz:fix/ws-global-scoped-enhancers

Conversation

@jackie-cqz

Copy link
Copy Markdown
Contributor

PR Checklist

  • The commit message follows the contribution guidelines.
  • Tests for the changes have been added.
  • Docs have been added / updated (this restores the documented global-enhancer behavior; no new public configuration).

PR Type

  • Bugfix

What is the current behavior?

A request-scoped or transient APP_GUARD is skipped on singleton WebSocket gateways, so even a guard that returns false allows the message handler to run. On gateways with a request-scoped dependency tree, global scoped enhancers can be read before their constructors and injected dependencies have been initialized. Global scoped pipes and interceptors are affected as well.

Closes #17975.

What is the new behavior?

WebSocket message handlers resolve global scoped guards, pipes and interceptors in the existing socket connection context before building the execution pipeline. Singleton gateways remain singletons, preserving their constructor initialization, shared state, afterInit and onModuleInit behavior.

The injector accepts an explicit enhancer list, allowing these globals to be loaded without adding them to the gateway dependency tree. Local transient enhancers are also loaded when a singleton message pipeline uses a connection context. Disconnect cleanup releases the connection context after the singleton gateway's async hook completes, including gateways without a disconnect hook. Global exception filters retain their existing exclusion from WebSocket pipelines.

Regression coverage includes denying guards on singleton and request-scoped gateways, REQUEST and TRANSIENT enhancer construction and injection, reuse within a connection and isolation across connections, pipe/interceptor-only registrations, singleton lifecycle/state, disconnect cleanup, and mixing global scoped with local transient enhancers.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Validation on Windows with Node.js 24.15.0:

  • npm run build β€” passed.
  • WebSocket integration suite β€” 31 tests passed, including 9 new regression cases. The final regression suite was also rerun after the last changes.
  • Unit suite β€” 305 files passed; 3,779 tests passed and 2 existing signal tests excluded.
  • npm run lint β€” passed, with one existing warning in socket-module.ts.
  • Prettier checks for changed files and git diff --check β€” passed.
  • Before the fix, the new pipe-only Socket.IO regression failed because the handler received the untransformed payload.

The unmodified unit-test command hit an existing color assertion because this environment sets NO_COLOR=1, and its SIGTERM tests terminated the worker on Windows. The successful unit run cleared NO_COLOR and used:

npm test -- --testNamePattern '^(?!.*(shutdown process should not be interrupted by another handler|should defer shutdown until all init hooks are resolved)).*'

The full Docker-dependent integration suite was not run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global request-scoped guards do not run on WebSocket gateways

2 participants