Skip to content

Data Race Warnings #496

Closed
Closed

Description

@jatinn

Description

After instrumenting with otelsql, running with -race I see data race warnings from the otelsql library.

Environment

  • OS: macOS 15.5
  • Architecture: arm64
  • Go Version: 1.24.2
  • otelsql version: v0.38.1-0.20250520162741-d712195e13f4

Steps To Reproduce

Run the following code with -race a few times and you will see the data race warnings generated

package main

import (
	"context"
	"database/sql"
	"fmt"
	"log"
	"sync"
	"time"

	"github.com/XSAM/otelsql"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	driverName, err := otelsql.Register("sqlite3", otelsql.WithAttributes(
		otelsql.AttributesFromDSN("file:test.db?cache=shared&mode=memory&_busy_timeout=5000")...,
	))
	if err != nil {
		log.Fatal(err)
	}

	db, err := sql.Open(driverName, "file:test.db?cache=shared&mode=memory&_busy_timeout=5000")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	ctx := context.Background()

	_, err = db.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS kv (key TEXT PRIMARY KEY, value TEXT);`)
	if err != nil {
		log.Fatal(err)
	}

	var wg sync.WaitGroup
	wg.Add(2)

	// Transaction 1: Succeeds
	go func() {
		defer wg.Done()
		tx1, err := db.BeginTx(ctx, nil)
		if err != nil {
			log.Fatal("tx1 begin:", err)
		}
		_, err = tx1.ExecContext(ctx, `INSERT INTO kv (key, value) VALUES (?, ?)`, "foo", "bar")
		if err != nil {
			log.Println("tx1 insert:", err)
			tx1.Rollback()
			return
		}
		time.Sleep(500 * time.Millisecond) // Hold lock
		err = tx1.Commit()
		if err != nil {
			log.Println("tx1 commit:", err)
		} else {
			log.Println("tx1 committed")
		}
	}()

	// Transaction 2: Collides, retries after rollback
	go func() {
		defer wg.Done()
		for i := 0; i < 3; i++ { // Retry loop
			tx2, err := db.BeginTx(ctx, nil)
			if err != nil {
				log.Fatal("tx2 begin:", err)
			}
			_, err = tx2.ExecContext(ctx, `INSERT INTO kv (key, value) VALUES (?, ?)`, "foo", "baz")
			if err != nil {
				log.Printf("tx2 insert attempt %d failed: %v\n", i+1, err)
				tx2.Rollback()
				time.Sleep(300 * time.Millisecond)
				continue
			}
			err = tx2.Commit()
			if err != nil {
				log.Printf("tx2 commit attempt %d failed: %v\n", i+1, err)
				tx2.Rollback()
				time.Sleep(300 * time.Millisecond)
				continue
			}
			log.Println("tx2 committed")
			break
		}
	}()

	wg.Wait()

	// Final DB state
	row := db.QueryRowContext(ctx, `SELECT value FROM kv WHERE key = ?`, "foo")
	var value string
	_ = row.Scan(&value)
	fmt.Println("Final value for 'foo':", value)
}

Depending on the OTEL_SEMCONV_STABILITY_OPT_IN env var you might see it from

github.com/XSAM/otelsql.recordDuration()
github.com/XSAM/otelsql.recordLegacyLatency()

Expected behavior

No data race warnings should be found.

Activity

  1. added
    bug
    on May 21, 2025
  2. XSAM commented on May 23, 2025

    @XSAM
    Owner

    @jatinn Good catch! Thanks for creating this issue. I very appreciate.

    I will create a PR to fix this.

  3. self-assigned this
    on May 23, 2025
  4. added a commit that references this issue on Jun 8, 2025
    3b74da0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions