Skip to content

Fix connection leak caused by rapid context cancellation #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 1, 2019

Conversation

mherr-google
Copy link
Contributor

Fix connection leak caused by rapid context cancellation (#1023)

Description

This change fixes a connection leak when the context is canceled shortly after a net.Conn is created.

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

Fix connection leak caused by rapid context cancellation (go-sql-driver#1023)
Regression test for go-sql-driver#1023.
fix go vet warning for possibly-unused cancel (go-sql-driver#1023)
@methane methane merged commit 296987f into go-sql-driver:master Nov 1, 2019
@julienschmidt julienschmidt added this to the v1.5.0 milestone Nov 1, 2019
@zjj
Copy link
Contributor

zjj commented Nov 21, 2019

I digged into the code,
When will the Connect's ctx.Err() return Error since I didn't find any cancel() to the Backgroud context ???

from context godoc

// If Done is not yet closed, Err returns nil.
// If Done is closed, Err returns a non-nil error explaining why:
// Canceled if the context was canceled
// or DeadlineExceeded if the context's deadline passed.
// After Err returns a non-nil error, successive calls to Err return the same error.
Err() error

@methane
Copy link
Member

methane commented Nov 21, 2019

I can not understand your question. Please elaborate.

the Connect's ctx.Err() return Error

What? There is no ctx.Err() in this pull request.

I didn't find any cancel() to the Backgroud context ???

What is "the Background context"? I can not find background context in this pull request.

Do you know you can paste permalink of the code?

@zjj
Copy link
Contributor

zjj commented Nov 21, 2019

mysql/connection.go

Lines 595 to 598 in 15462c1

// When ctx is already cancelled, don't watch it.
if err := ctx.Err(); err != nil {
return err
}

above code, if the return value is an error, It must from L597
but the ctx here is from Connect, which is a BackgoudContext, when would the backgroudcontext's Err() is not nil ? since there's no any cancel() calls to a backgroud context.

mysql/connector.go

Lines 67 to 72 in 15462c1

mc.startWatcher()
if err := mc.watchCancel(ctx); err != nil {
mc.cleanup()
return nil, err
}
defer mc.finish()

mysql/driver.go

Lines 75 to 82 in 15462c1

return nil, err
}
c := &connector{
cfg: cfg,
}
return c.Connect(context.Background())
}

maybe, I mistook something

@methane
Copy link
Member

methane commented Nov 21, 2019

but the ctx here is from Connect, which is a BackgoudContext

Why do you think it is a BackgroundContext?

mysql/driver.go

Lines 75 to 82 in 15462c1

return nil, err
}
c := &connector{
cfg: cfg,
}
return c.Connect(context.Background())
}

This is an legacy API which is designed before context era.
Connect() is called from database/sql in modern API.

https://github.com/golang/go/blob/39a9cb4b5dbf1e518b0c66fa3a7b4175f90226fc/src/database/sql/sql.go#L1078-L1083

@zjj
Copy link
Contributor

zjj commented Nov 21, 2019

@methane thx for your pantience. Finally, I found the gate...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants