Skip to content

Data race WARNING #1180

@mymtw

Description

@mymtw

Hi, I'm setting gin mode

	gin.SetMode("test")
	r := gin.Default()

And when I'm running my tests in t.Parallel() with flag -race -> I'm getting data race warning:

==================
WARNING: DATA RACE
Write at 0x000005c34cb8 by goroutine 38:
  gitea.io/atlantide/curiosity/vendor/github.com/gin-gonic/gin.SetMode()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/gin-gonic/gin/mode.go:56 +0x1fe
  gitea.io/atlantide/curiosity/config/routes.GinRouter()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/config/routes/routes.go:33 +0x2ba
  gitea.io/atlantide/curiosity/test/controllers.(*SessionsControllerSuite).TestCreate()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/test/controllers/sessions_controller_test.go:124 +0xec6
  runtime.call32()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/runtime/asm_amd64.s:514 +0x47
  reflect.Value.Call()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/reflect/value.go:302 +0xc0
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.Run.func2()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:102 +0x31f
  testing.tRunner()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:657 +0x107

Previous write at 0x000005c34cb8 by goroutine 44:
  gitea.io/atlantide/curiosity/vendor/github.com/gin-gonic/gin.SetMode()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/gin-gonic/gin/mode.go:56 +0x1fe
  gitea.io/atlantide/curiosity/config/routes.GinRouter()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/config/routes/routes.go:33 +0x2ba
  gitea.io/atlantide/curiosity/test/controllers.(*UsersControllerSuite).TestCreate()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/test/controllers/users_controller_test.go:164 +0xc9d
  runtime.call32()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/runtime/asm_amd64.s:514 +0x47
  reflect.Value.Call()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/reflect/value.go:302 +0xc0
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.Run.func2()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:102 +0x31f
  testing.tRunner()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:657 +0x107

Goroutine 38 (running) created at:
  testing.(*T).Run()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:697 +0x543
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.runTests()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:121 +0xd6
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.Run()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:108 +0x709
  gitea.io/atlantide/curiosity/test/controllers.TestSessionsControllerSuite()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/test/controllers/sessions_controller_test.go:236 +0x6c
  testing.tRunner()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:657 +0x107

Goroutine 44 (running) created at:
  testing.(*T).Run()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:697 +0x543
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.runTests()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:121 +0xd6
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.Run()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:108 +0x709
  gitea.io/atlantide/curiosity/test/controllers.TestUsersControllerSuite()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/test/controllers/users_controller_test.go:489 +0x6c
  testing.tRunner()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:657 +0x107
==================
==================
WARNING: DATA RACE
Write at 0x00000526da40 by goroutine 38:
  gitea.io/atlantide/curiosity/vendor/github.com/gin-gonic/gin.SetMode()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/gin-gonic/gin/mode.go:60 +0x103
  gitea.io/atlantide/curiosity/config/routes.GinRouter()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/config/routes/routes.go:33 +0x2ba
  gitea.io/atlantide/curiosity/test/controllers.(*SessionsControllerSuite).TestCreate()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/test/controllers/sessions_controller_test.go:124 +0xec6
  runtime.call32()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/runtime/asm_amd64.s:514 +0x47
  reflect.Value.Call()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/reflect/value.go:302 +0xc0
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.Run.func2()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:102 +0x31f
  testing.tRunner()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:657 +0x107

Previous write at 0x00000526da40 by goroutine 44:
  gitea.io/atlantide/curiosity/vendor/github.com/gin-gonic/gin.SetMode()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/gin-gonic/gin/mode.go:60 +0x103
  gitea.io/atlantide/curiosity/config/routes.GinRouter()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/config/routes/routes.go:33 +0x2ba
  gitea.io/atlantide/curiosity/test/controllers.(*UsersControllerSuite).TestCreate()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/test/controllers/users_controller_test.go:164 +0xc9d
  runtime.call32()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/runtime/asm_amd64.s:514 +0x47
  reflect.Value.Call()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/reflect/value.go:302 +0xc0
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.Run.func2()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:102 +0x31f
  testing.tRunner()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:657 +0x107

Goroutine 38 (running) created at:
  testing.(*T).Run()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:697 +0x543
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.runTests()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:121 +0xd6
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.Run()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:108 +0x709
  gitea.io/atlantide/curiosity/test/controllers.TestSessionsControllerSuite()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/test/controllers/sessions_controller_test.go:236 +0x6c
  testing.tRunner()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:657 +0x107

Goroutine 44 (running) created at:
  testing.(*T).Run()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:697 +0x543
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.runTests()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:121 +0xd6
  gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite.Run()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/vendor/github.com/stretchr/testify/suite/suite.go:108 +0x709
  gitea.io/atlantide/curiosity/test/controllers.TestUsersControllerSuite()
      /Users/apple/Developer/goproject/src/gitea.io/atlantide/curiosity/test/controllers/users_controller_test.go:489 +0x6c
  testing.tRunner()
      /Users/apple/.gimme/versions/go1.8.1.darwin.amd64/src/testing/testing.go:657 +0x107
==================

Maybe reason in yr code ginMode = testCode ?:

func SetMode(value string) {
	switch value {
	case DebugMode:
		ginMode = debugCode                         <<<<<<<<<<< ?????
	case ReleaseMode:
		ginMode = releaseCode                         <<<<<<<<<<< ?????
	case TestMode:
		ginMode = testCode                                <<<<<<<<<<< ?????
	default:
		panic("gin mode unknown: " + value)
	}
	modeName = value                                      <<<<<<<<<<< ?????
}

Your are changing global var from function, it looks like this is not safe.
The warning in this context is not dangerous, but it will be better to fix this ASAP

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