Skip to content

contrib/database/sql: fix race condition#3649

Merged
darccio merged 2 commits into
mainfrom
rarguelloF/database-sql-fix-race
Jun 17, 2025
Merged

contrib/database/sql: fix race condition#3649
darccio merged 2 commits into
mainfrom
rarguelloF/database-sql-fix-race

Conversation

@rarguelloF

@rarguelloF rarguelloF commented Jun 17, 2025

Copy link
Copy Markdown
Contributor

What does this PR do?

Fix race condition in contrib/database/sql.Register.

The race could be triggered by running one of the existing tests TestRegister with -race flag:

INTEGRATION=1 go test -race ./contrib/database/sql -v -run 'TestRegister'
# github.com/DataDog/dd-trace-go/contrib/database/sql/v2.test
=== RUN   TestRegister
==================
WARNING: DATA RACE
Read at 0x000104945998 by goroutine 39:
  github.com/DataDog/dd-trace-go/contrib/database/sql/v2.Register()
      /Users/rodrigo.arguello/go/src/github.com/DataDog/dd-trace-go/contrib/database/sql/sql.go:121 +0x50
  github.com/DataDog/dd-trace-go/contrib/database/sql/v2.TestRegister.func1()
      /Users/rodrigo.arguello/go/src/github.com/DataDog/dd-trace-go/contrib/database/sql/sql_test.go:394 +0x70
  github.com/DataDog/dd-trace-go/contrib/database/sql/v2.TestRegister.gowrap1()
      /Users/rodrigo.arguello/go/src/github.com/DataDog/dd-trace-go/contrib/database/sql/sql_test.go:396 +0x44

Previous write at 0x000104945998 by goroutine 40:
  github.com/DataDog/dd-trace-go/contrib/database/sql/v2.Register()
      /Users/rodrigo.arguello/go/src/github.com/DataDog/dd-trace-go/contrib/database/sql/sql.go:123 +0xa0
  github.com/DataDog/dd-trace-go/contrib/database/sql/v2.TestRegister.func1()
      /Users/rodrigo.arguello/go/src/github.com/DataDog/dd-trace-go/contrib/database/sql/sql_test.go:394 +0x70
  github.com/DataDog/dd-trace-go/contrib/database/sql/v2.TestRegister.gowrap1()
      /Users/rodrigo.arguello/go/src/github.com/DataDog/dd-trace-go/contrib/database/sql/sql_test.go:396 +0x44

Goroutine 39 (running) created at:
  github.com/DataDog/dd-trace-go/contrib/database/sql/v2.TestRegister()
      /Users/rodrigo.arguello/go/src/github.com/DataDog/dd-trace-go/contrib/database/sql/sql_test.go:393 +0x64
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.24.4/libexec/src/testing/testing.go:1792 +0x180
  testing.(*T).Run.gowrap1()
      /opt/homebrew/Cellar/go/1.24.4/libexec/src/testing/testing.go:1851 +0x40

Goroutine 40 (running) created at:
  github.com/DataDog/dd-trace-go/contrib/database/sql/v2.TestRegister()
      /Users/rodrigo.arguello/go/src/github.com/DataDog/dd-trace-go/contrib/database/sql/sql_test.go:393 +0x64
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.24.4/libexec/src/testing/testing.go:1792 +0x180
  testing.(*T).Run.gowrap1()
      /opt/homebrew/Cellar/go/1.24.4/libexec/src/testing/testing.go:1851 +0x40
==================
    testing.go:1490: race detected during execution of test
--- FAIL: TestRegister (0.00s)
FAIL
FAIL	github.com/DataDog/dd-trace-go/contrib/database/sql/v2	0.802s
FAIL

After the fix in this PR:

INTEGRATION=1 go test -race ./contrib/database/sql -v -run 'TestRegister'
# github.com/DataDog/dd-trace-go/contrib/database/sql/v2.test
=== RUN   TestRegister
--- PASS: TestRegister (0.00s)
PASS
ok  	github.com/DataDog/dd-trace-go/contrib/database/sql/v2	(cached)

Motivation

Fix race condition.

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • New code is free of linting errors. You can check this by running golangci-lint run locally.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.
  • Non-trivial go.mod changes, e.g. adding new modules, are reviewed by @DataDog/dd-trace-go-guild.

Unsure? Have a question? Request a review!

@rarguelloF
rarguelloF requested review from a team as code owners June 17, 2025 11:57
@github-actions github-actions Bot added the apm:ecosystem contrib/* related feature requests or bugs label Jun 17, 2025
@pr-commenter

pr-commenter Bot commented Jun 17, 2025

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2025-06-17 12:48:43

Comparing candidate commit 5bee7b6 in PR branch rarguelloF/database-sql-fix-race with baseline commit 6a2d46f in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 24 metrics, 0 unstable metrics.

@darccio
darccio enabled auto-merge (squash) June 17, 2025 12:35
@darccio
darccio merged commit b85212c into main Jun 17, 2025
218 checks passed
@darccio
darccio deleted the rarguelloF/database-sql-fix-race branch June 17, 2025 12:52
mtoffl01 pushed a commit that referenced this pull request Jun 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apm:ecosystem contrib/* related feature requests or bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants