Problem
urlParamFn is a package-level variable in core/app.go that gets overwritten every time NewAppWith() is called:
// core/app.go
var urlParamFn = func(r *http.Request, key string) string {
return chi.URLParam(r, key)
}
func NewAppWith(adapter RouterAdapter) *App {
...
urlParamFn = adapter.URLParam // overwrites the package-level var
...
}
func URLParam(r *http.Request, key string) string {
return urlParamFn(r, key) // reads the package-level var
}
If two App instances are created with different adapters, the second call to NewAppWith() silently overwrites the urlParamFn used by the first instance. Parallel tests that each create their own App will trigger a data race detectable with go test -race.
Impact
- Running
go test -race ./... on a test suite with multiple App instances reports a data race on urlParamFn
- The first app's
URLParam silently delegates to the wrong adapter after the second app is created
Proposed Fix
Store urlParamFn on the App struct instead of as a package-level variable, and make URLParam a method rather than a free function — or pass the resolver through the request context.
Environment
Problem
urlParamFnis a package-level variable incore/app.gothat gets overwritten every timeNewAppWith()is called:If two
Appinstances are created with different adapters, the second call toNewAppWith()silently overwrites theurlParamFnused by the first instance. Parallel tests that each create their ownAppwill trigger a data race detectable withgo test -race.Impact
go test -race ./...on a test suite with multipleAppinstances reports a data race onurlParamFnURLParamsilently delegates to the wrong adapter after the second app is createdProposed Fix
Store
urlParamFnon theAppstruct instead of as a package-level variable, and makeURLParama method rather than a free function — or pass the resolver through the request context.Environment
core/app.go:41, 64