Skip to content

fix: avoid mutating shared routerOptions across instances#6515

Merged
mcollina merged 1 commit intomainfrom
fix/6512-routeroptions-reuse
Feb 20, 2026
Merged

fix: avoid mutating shared routerOptions across instances#6515
mcollina merged 1 commit intomainfrom
fix/6512-routeroptions-reuse

Conversation

@mcollina
Copy link
Member

Problem

Reusing the same routerOptions object across multiple Fastify instances can fail with:

TypeError: Cannot read properties of null (reading 'server')

buildRouterOptions() was mutating the user-provided options.routerOptions object in place while filling defaults (including instance-bound callbacks like defaultRoute / onBadUrl).

Fix

  • clone options.routerOptions before filling defaults
  • avoid mutating user-provided config objects

Tests

  • add regression test: Should allow reusing a routerOptions object across instances
  • add regression test: Should not mutate user-provided routerOptions object
  • ensure cleanup uses t.after() so instances are always closed

Fixes #6512

@Eomm Eomm added the bugfix Issue or PR that should land as semver patch label Feb 20, 2026
Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit b4db85b into main Feb 20, 2026
39 checks passed
@mcollina mcollina deleted the fix/6512-routeroptions-reuse branch February 20, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Issue or PR that should land as semver patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

routerOptions cannot be reused across multiple fastify instances

4 participants