apache / apache/cassandra-gocql-driver

Data race when calling SetConsistency

Open
#1,473 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
2.7k
Forks
658
PR merge metrics
No merged PRs in 30d

Description

Please answer these questions before submitting your issue. Thanks!

### What version of Cassandra are you using?

Docker image cassandra:3.11.

### What version of Gocql are you using?

```
$ grep gocql go.mod
github.com/gocql/gocql v0.0.0-20200624222514-34081eda590e
```

### What did you do?

```
sess, err := cluster.CreateSession()
if err != nil {
return nil, err
}
sess.SetConsistency(consistency)
```

Elsewhere:
```
sess.Query(...)
```

### What did you expect to see?

No data race when running with `-race`.

### What did you see instead?

```
=== FAIL: physical/cassandra TestCassandraBackend (25.07s)
==================
WARNING: DATA RACE
Read at 0x00c0001d8380 by goroutine 25:
github.com/hashicorp/vault/vendor/github.com/gocql/gocql.(*Conn).UseKeyspace()
/go/src/github.com/hashicorp/vault/vendor/github.com/gocql/gocql/conn.go:1235 +0x186
github.com/hashicorp/vault/vendor/github.com/gocql/gocql.(*hostConnPool).connect()
/go/src/github.com/hashicorp/vault/vendor/github.com/gocql/gocql/connectionpool.go:534 +0x53d
github.com/hashicorp/vault/vendor/github.com/gocql/gocql.(*hostConnPool).connectMany.func1()
/go/src/github.com/hashicorp/vault/vendor/github.com/gocql/gocql/connectionpool.go:487 +0x79

Previous write at 0x00c0001d8380 by goroutine 7:
github.com/hashicorp/vault/vendor/github.com/gocql/gocql.(*Session).SetConsistency()
/go/src/github.com/hashicorp/vault/vendor/github.com/gocql/gocql/session.go:336 +0x54
github.com/hashicorp/vault/physical/cassandra.NewCassandraBackend()
/go/src/github.com/hashicorp/vault/physical/cassandra/cassandra.go:144 +0x917
github.com/hashicorp/vault/physical/cassandra.TestCassandraBackend()
/go/src/github.com/hashicorp/vault/physical/cassandra/cassandra_test.go:29 +0x2dc
testing.tRunner()
/usr/local/go/src/testing/testing.go:1039 +0x1eb

Goroutine 25 (running) created at:
github.com/hashicorp/vault/vendor/github.com/gocql/gocql.(*hostConnPool).connectMany()
/go/src/github.com/hashicorp/vault/vendor/github.com/gocql/gocql/connectionpool.go:485 +0x159
github.com/hashicorp/vault/vendor/github.com/gocql/gocql.(*hostConnPool).fill.func1()
/go/src/github.com/hashicorp/vault/vendor/github.com/gocql/gocql/connectionpool.go:439 +0x42

Goroutine 7 (running) created at:
testing.(*T).Run()
/usr/local/go/src/testing/testing.go:1090 +0x700
testing.runTests.func1()
/usr/local/go/src/testing/testing.go:1334 +0xa6
testing.tRunner()
/usr/local/go/src/testing/testing.go:1039 +0x1eb
testing.runTests()
/usr/local/go/src/testing/testing.go:1332 +0x527
testing.(*M).Run()
/usr/local/go/src/testing/testing.go:1249 +0x43f
main.main()
_testmain.go:46 +0x223
```

---

To reproduce, clone `github.com/hashicorp/vault`, then:

```
go test -race ./physical/cassandra
```

The issue looks to be that although `Session.SetConsistency` uses a lock to protect writes to `s.cons`, `Conn.UseKeyscape` reads `c.session.cons` without any mechanism to manage thread safety.

Contributor guide

Open the contributing guide

Research direction

Run `go test -race ./physical/cassandra` to reproduce the report. Read `session.go` at `Session.SetConsistency`, then follow the stack trace through `conn.go` and `connectionpool.go`; done means the reported race no longer appears under the race detector.

Written by the indexing model from the issue text.

Assessment

Tech stack
cassandra, go
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.