cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
30.14k stars 3.81k forks source link

SQL Proxy Misuses Stopper's Context #77689

Open jeffswenson opened 2 years ago

jeffswenson commented 2 years ago

The SQL proxy uses stoper.RunAsyncTask to create a goroutine for each connection. The context created for the async task is passed to child go routines that are spawned using the go command. Stopper attaches a span to the context and the span is returned to a pool when the task returns. During the connection tear down process, child workers may use the task's context. This is inappropriate as it leads to a use after free bug.

Per https://github.com/cockroachdb/cockroach/issues/58164 the sqlproxy should avoid raw use of the go keyword and should use the stopper to spawn goroutines.

Jira issue: CRDB-13713

jeffswenson commented 1 year ago

When we clean this up, we should also remove all instances of stopper.WithCancelOnQuiesce(ctx) that do not defer the cancel function. Callers that do not call the cancel function are at a high risk for leaking the context. See https://github.com/cockroachdb/cockroach/pull/101787 for details.