Skip to content

Data race in parallel test with RequestLogger middleware #2476

Closed
@rugleb

Description

@rugleb

Issue Description

I get a WARNING: DATA RACE when running parallel tests with the "-race" flag in RequestLoggerConfig.

Checklist

  • [+] Dependencies installed
  • [+] No typos
  • [+] Searched existing issues and docs

Expected behaviour

No data race in parallel tests

Actual behaviour

Data race in parallel tests

Steps to reproduce

Save code below to bug_test.go:

package bug

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

func New() *echo.Echo {
	e := echo.New()
	e.Pre(
		middleware.RequestLoggerWithConfig(
			middleware.RequestLoggerConfig{},  // just example
		),
	)
	return e
}

func TestB(t *testing.T) {
	t.Parallel()

	req := httptest.NewRequest(http.MethodGet, "/", nil)
	res := httptest.NewRecorder()
	New().ServeHTTP(res, req)
}

func TestA(t *testing.T) {
	t.Parallel()

	req := httptest.NewRequest(http.MethodGet, "/", nil)
	res := httptest.NewRecorder()
	New().ServeHTTP(res, req)
}

Run: go test -race bug_test.go.

Working code to debug

See "Steps to reproduce".

Version/commit

v4.10.2

Reason/Solution

RequestLoggerConfig override global now which defined in rate_limiter.go without sync. I think this operation must be safe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions