Skip to content

fix(group): register auto 404 routes only once on Use#3048

Closed
Solaris-star wants to merge 1 commit into
labstack:masterfrom
Solaris-star:fix/3047-duplicate-group-404-use
Closed

fix(group): register auto 404 routes only once on Use#3048
Solaris-star wants to merge 1 commit into
labstack:masterfrom
Solaris-star:fix/3047-duplicate-group-404-use

Conversation

@Solaris-star

Copy link
Copy Markdown

Description

After #2996, Group.Use auto-registers RouteNotFound catch-alls so group middleware still runs on 404s. It did that on every Use() call.

With AllowOverwritingRoute: false (or whenever duplicate routes are rejected), a second Use panics:

panic: echo_route_not_found /api: adding duplicate route (same method+path) is not allowed

Repro patterns from #3047:

g := e.Group("/api")
g.Use(mw1)
g.Use(mw2) // panic

g := e.Group("/api", mw1)
g.Use(mw2) // panic

Fix

Only auto-register the 404 routes the first time the group gains middleware (hadMiddleware was empty before append). Subsequent Use calls only append middleware.

Tests

go test ./... -count=1

Added TestGroup_UseMultipleTimesDoesNotPanic.

Linked Issue

Closes #3047

Group.Use always called RouteNotFound for "" and "/*" whenever middleware
was present. A second Use() (or Group(..., mw) followed by Use) re-added
the same routes and panicked when AllowOverwritingRoute is false.

Only auto-register the catch-all 404 routes the first time middleware is
attached to the group.

Fixes labstack#3047
@aldas

aldas commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Thanks, closing, #3049 implemented a little bit different approach

@aldas aldas closed this Jul 21, 2026
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.

Implicit 404 handlers cause panic if Use is called more than once

2 participants