Skip to content

[Bug] Package-level urlParamFn overwritten on each NewAppWith() call — data race with multiple App instances #41

@ftery0

Description

@ftery0

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

  • File: core/app.go:41, 64

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions