Skip to content

schema.VisitJSONString contains DATA RACE #371

@alexanderbolgov-ef

Description

@alexanderbolgov-ef

Hello!
Thanks for creating and supporting this library

I've noticed data race, let me show.

Consider following sample test:

import (
	"github.com/getkin/kin-openapi/openapi3"
	"testing"
)

func TestRace_test(t *testing.T) {
	var schema = openapi3.Schema{
		Pattern:                     "race",
	}
	go func() { _ = schema.VisitJSONString("test")}()
	_ = schema.VisitJSONString("test")
}

when I run this test, I get the following output:

=== RUN   TestRace_test
==================
WARNING: DATA RACE
Write at 0x00c00011f658 by goroutine 7:
  github.com/getkin/kin-openapi/openapi3.(*Schema).visitJSONString()
      /Users/alexanderbolgov/go/pkg/mod/github.com/getkin/kin-openapi@v0.63.0/openapi3/schema.go:1144 +0xf84
  github.com/getkin/kin-openapi/openapi3.(*Schema).VisitJSONString()
      /Users/alexanderbolgov/go/pkg/mod/github.com/getkin/kin-openapi@v0.63.0/openapi3/schema.go:1086 +0x70
  ef-studio/tf/shr.TestRace_test()
      /Users/alexanderbolgov/ef-code/ef-studio/backend/go/tf/shr/test-race_test.go:13 +0xd2
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1194 +0x202

Previous read at 0x00c00011f658 by goroutine 8:
  github.com/getkin/kin-openapi/openapi3.(*Schema).visitJSONString()
      /Users/alexanderbolgov/go/pkg/mod/github.com/getkin/kin-openapi@v0.63.0/openapi3/schema.go:1142 +0x14a6
  github.com/getkin/kin-openapi/openapi3.(*Schema).VisitJSONString()
      /Users/alexanderbolgov/go/pkg/mod/github.com/getkin/kin-openapi@v0.63.0/openapi3/schema.go:1086 +0x70
  ef-studio/tf/shr.TestRace_test.func1()
      /Users/alexanderbolgov/ef-code/ef-studio/backend/go/tf/shr/test-race_test.go:12 +0x4d

Goroutine 7 (running) created at:
  testing.(*T).Run()
      /usr/local/go/src/testing/testing.go:1239 +0x5d7
  testing.runTests.func1()
      /usr/local/go/src/testing/testing.go:1512 +0xa6
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1194 +0x202
  testing.runTests()
      /usr/local/go/src/testing/testing.go:1510 +0x612
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:1418 +0x3b3
  main.main()
      _testmain.go:43 +0x236

Goroutine 8 (running) created at:
  ef-studio/tf/shr.TestRace_test()
      /Users/alexanderbolgov/ef-code/ef-studio/backend/go/tf/shr/test-race_test.go:12 +0xaf
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1194 +0x202
==================
    testing.go:1093: race detected during execution of test
--- FAIL: TestRace_test (0.00s)

=== CONT  
    testing.go:1093: race detected during execution of test
FAIL

Looks like the problem sits here:
image

I believe this can happen when a web server receives two concurrent requests and performs two concurrent validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions