Skip to content

Fix(middleware): Prevent double handler invocation in RouteHeaders with empty router#1045

Merged
VojtechVitek merged 1 commit intogo-chi:masterfrom
mahanadh:master
Feb 5, 2026
Merged

Fix(middleware): Prevent double handler invocation in RouteHeaders with empty router#1045
VojtechVitek merged 1 commit intogo-chi:masterfrom
mahanadh:master

Conversation

@mahanadh
Copy link
Contributor

@mahanadh mahanadh commented Feb 4, 2026

Description

This PR fixes a bug in the RouteHeaders middleware where an empty router
(created with RouteHeaders() but no routes added) would invoke the next
handler twice.

Problem

In HeaderRouter.Handler(), when len(hr) == 0, the code calls
next.ServeHTTP(w, r) but doesn't return, causing execution to continue
and potentially call next.ServeHTTP again at line 102.

// Before (buggy):
if len(hr) == 0 {
    // skip if no routes set
    next.ServeHTTP(w, r)
}  // <-- Missing return! Continues to execute...

Checklist

  • Bug fix (non-breaking change that fixes an issue)
  • Added tests
  • All existing tests pass

The RouteHeaders middleware was missing a return statement after calling
next.ServeHTTP when the router had no routes configured. This caused the
next handler to be called twice - once in the empty check and again at
the end of the function.

Also adds comprehensive test coverage for the RouteHeaders middleware
and Pattern matching functionality.
Copy link
Contributor

@VojtechVitek VojtechVitek left a comment

Choose a reason for hiding this comment

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

LGTM

if len(hr) == 0 {
// skip if no routes set
next.ServeHTTP(w, r)
return
Copy link
Contributor

Choose a reason for hiding this comment

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

Very nice catch! Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to contribute in any way possible!

@VojtechVitek VojtechVitek merged commit 05f1ef7 into go-chi:master Feb 5, 2026
8 checks passed
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