Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib/database/sql: Fix race condition when registering drivers #1450

Merged
merged 2 commits into from Sep 21, 2022
Merged

contrib/database/sql: Fix race condition when registering drivers #1450

merged 2 commits into from Sep 21, 2022

Conversation

rafaeljusto
Copy link
Contributor

Library failing when registering drivers in parallel on different goroutines.

Internal driverRegistry wasn't goroutine safe, causing race conditions with multiple writes in the internal maps.

$ go test -race -run TestRegister

WARNING: DATA RACE
Write at 0x00c000254000 by goroutine 9:
  runtime.mapaccessK()
      /opt/homebrew/Cellar/go/1.19/libexec/src/runtime/map.go:518 +0x1ec
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.(*driverRegistry).add()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql.go:60 +0x144
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.TestRegister.func1()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql_test.go:282 +0x54
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.TestRegister.func2()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql_test.go:285 +0x44

Previous write at 0x00c000254000 by goroutine 15:
  runtime.mapaccessK()
      /opt/homebrew/Cellar/go/1.19/libexec/src/runtime/map.go:518 +0x1ec
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.(*driverRegistry).add()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql.go:60 +0x144
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.TestRegister.func1()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql_test.go:282 +0x54
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.TestRegister.func2()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql_test.go:285 +0x44

Library failing when registering drivers in parallel on different goroutines.

Internal `driverRegistry` wasn't goroutine safe, causing race conditions with
multiple writes in the internal maps.

```
$ go test -race -run TestRegister

WARNING: DATA RACE
Write at 0x00c000254000 by goroutine 9:
  runtime.mapaccessK()
      /opt/homebrew/Cellar/go/1.19/libexec/src/runtime/map.go:518 +0x1ec
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.(*driverRegistry).add()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql.go:60 +0x144
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.TestRegister.func1()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql_test.go:282 +0x54
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.TestRegister.func2()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql_test.go:285 +0x44

Previous write at 0x00c000254000 by goroutine 15:
  runtime.mapaccessK()
      /opt/homebrew/Cellar/go/1.19/libexec/src/runtime/map.go:518 +0x1ec
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.(*driverRegistry).add()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql.go:60 +0x144
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.TestRegister.func1()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql_test.go:282 +0x54
  gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql.TestRegister.func2()
      /Users/rafael.justo/dev/go/src/github.com/rafaeljusto/dd-trace-go/contrib/database/sql/sql_test.go:285 +0x44
```
@rafaeljusto rafaeljusto requested a review from a team August 29, 2022 20:52
@knusbaum knusbaum added this to the 1.43.0 milestone Sep 21, 2022
Copy link
Contributor

@knusbaum knusbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I have a minor doc change for you.

I misread the diff. I have no doc change for you. 😄

@rafaeljusto
Copy link
Contributor Author

Looks like something is missing after main was merged back into this PR.
@knusbaum would you know how to set this environment variable for the tests?

image

@knusbaum
Copy link
Contributor

@rafaeljusto We can ignore those failures, as they don't run for outside contributions (we should really disable them).
All of the relevant tests are passing, so I'll go ahead and merge this.

@knusbaum knusbaum merged commit a6a9aa8 into DataDog:main Sep 21, 2022
@rafaeljusto rafaeljusto deleted the fix/register-race branch September 21, 2022 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants