cockroachdb / cockroachdb/cockroach
sql: data race in NewDCollatedString
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
There is a data race in `NewDCollatedString` [1] owing to a reuse of `CollationEnvironment` [2] in `evalCtx`. The data race can result in `panic`, owing to `index out of range`, or worse–_data corruption_, owing to incorrect query results. The bug has been lurking since, presumably [3]?!, which predates any of the currently supported releases; i.e., this bug is present in all current releases.
Given its non-deterministic nature, it first popped up on my radar in June [4], after running `sqlancer` against a 3-node cluster. However, it took a while to obtain the root cause, as well as, a semi-deterministic reproduction. The root cause is the reuse of `evalCtx` on a gateway in the case where a pair (or more) outboxes are executed _asynchronously_, each using `COLLATE` in some form. A detailed summary of the data race and a reproduction is given below.
[1] https://github.com/cockroachdb/cockroach/blob/b5836a5b4ab6c4fe58ada6002eb8c56c4e35438e/pkg/sql/sem/tree/datum.go#L1432
[2] https://github.com/cockroachdb/cockroach/blob/b5836a5b4ab6c4fe58ada6002eb8c56c4e35438e/pkg/sql/distsql/server.go#L303-L307
[3] https://github.com/cockroachdb/cockroach/pull/27863
[4] https://cockroachlabs.slack.com/archives/C01RX2G8LT1/p1686602875602219
Jira issue: CRDB-31379
Contributor guide
Assessment
This issue has not been assessed yet.