Skip to content

sqlproxyccl: pass in ctx when transferring connections#154266

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
davidwding:ding/ctx
Sep 29, 2025
Merged

sqlproxyccl: pass in ctx when transferring connections#154266
craig[bot] merged 1 commit intocockroachdb:masterfrom
davidwding:ding/ctx

Conversation

@davidwding
Copy link
Copy Markdown
Collaborator

@davidwding davidwding commented Sep 26, 2025

Previously, the span used when transferring connections was reused from
the forwarder. However, transferring connections has async components,
so it's possible that the forwarder calls Finish on the span before
the connection transfer is done using the span. This leads to a panic:
panic: use of Span after Finish. To address this, this patch passes a
context in when transferring a connection, instead of using the
forwarder's context.

Fixes: #153569
Release note: None

@davidwding davidwding requested a review from a team as a code owner September 26, 2025 18:36
@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Sep 26, 2025

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@davidwding
Copy link
Copy Markdown
Collaborator Author

Regarding the Blathers testing suggestion, it's possible there's some issue with the test itself that is causing the flake linked in the "Fixes" ticket, in which case we'll find out with future tickets. But at the very least this PR fixes a panic caused by an actual mismanagement of spans.

}
}

func newTransferContext(backgroundCtx context.Context) (*transferContext, context.CancelFunc) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is subtle, but I don't think this a complete fix. It's possible the forwarder's span was already free'd by the time we are calling newTransferContext.

I think fixing this requires us to attach a span to the forwarder, or add a context as an argument to TransferConnection and use that context instead of the forwarder's context.

Passing a context with a span into TransferConnection feels more idiomatic. I'm not sure why this code was using the forwarder's context.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL. I don't believe that I need to fork the span anymore in newTransferContext, but let me know if I'm misunderstanding.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks good to me. I'll approve it once CI is green.

@davidwding davidwding changed the title sqlproxyccl: fork span when transferring connections sqlproxyccl: pass in ctx when transferring connections Sep 26, 2025
@davidwding davidwding force-pushed the ding/ctx branch 2 times, most recently from efffc43 to cd354d2 Compare September 26, 2025 19:25
@jeffswenson
Copy link
Copy Markdown
Collaborator

There is a static check failure:

=== RUN   TestLint/TestStaticCheck
    gen-lint_test.go:2121: 
        -: # github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl/balancer [github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl/balancer.test]
    gen-lint_test.go:2121: 
        pkg/ccl/sqlproxyccl/balancer/conn_test.go:31:26: cannot use &testConnHandle{} (value of type *testConnHandle) as ConnectionHandle value in variable declaration: *testConnHandle does not implement ConnectionHandle (wrong type for method TransferConnection)

//
// TransferConnection implements the balancer.ConnectionHandle interface.
func (f *forwarder) TransferConnection() (retErr error) {
func (f *forwarder) TransferConnection(transferCtx context.Context) (retErr error) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think you can call this parameter ctx. Since it is used as a conventional context.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I had to rename another variable to transferCtx

Previously, the span used when transferring connections was reused from
the forwarder. However, transferring connections has async components,
so it's possible that the forwarder calls `Finish` on the span before
the connection transfer is done using the span. This leads to a panic:
`panic: use of Span after Finish`. To address this, this patch passes a
context in when transferring a connection, instead of using the
forwarder's context.

Fixes: cockroachdb#153569
Release note: None
Copy link
Copy Markdown
Collaborator Author

@davidwding davidwding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see if I fixed the test failure too...I wasn't handling the context objects in the test properly 🤞

//
// TransferConnection implements the balancer.ConnectionHandle interface.
func (f *forwarder) TransferConnection() (retErr error) {
func (f *forwarder) TransferConnection(transferCtx context.Context) (retErr error) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I had to rename another variable to transferCtx

Copy link
Copy Markdown
Collaborator

@jeffswenson jeffswenson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@davidwding
Copy link
Copy Markdown
Collaborator Author

TFTR!

@davidwding
Copy link
Copy Markdown
Collaborator Author

bors r=jeffswenson

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Sep 29, 2025

@davidwding davidwding added backport-24.3.x Flags PRs that need to be backported to 24.3 backport-25.2.x Flags PRs that need to be backported to 25.2 backport-25.4.x Flags PRs that need to be backported to 25.4 and removed backport-24.3.x Flags PRs that need to be backported to 24.3 backport-25.2.x Flags PRs that need to be backported to 25.2 backport-25.4.x Flags PRs that need to be backported to 25.4 labels Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ccl/sqlproxyccl: TestCancelQuery failed

3 participants