Skip to content
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

database/sql: goroutine leak detection made difficult with sql.DB.Close() #50616

Open
MichaelSnowden opened this issue Jan 14, 2022 · 0 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@MichaelSnowden
Copy link

MichaelSnowden commented Jan 14, 2022

We start a goroutine for connectionOpener like this:

go db.connectionOpener(ctx)

And then it terminates when the context is cancelled:

go/src/database/sql/sql.go

Lines 1224 to 1233 in 4f0c32d

func (db *DB) connectionOpener(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case <-db.openerCh:
db.openNewConnection(ctx)
}
}
}

Which happens here:

db.stop()

The Close() method then returns after some additional cleanup. Since we don't wait for the actual goroutine to close, after Close() happens, there could still be a goroutine running.

I think we should add something like this to the OpenDB method:

go func() {
  defer db.openerRoutine.Done()
  db.connectionOpener(ctx)
}

and then add this to the Close() method:

db.stop()
db.openerRoutine.Wait()

This would make it easier to use things like goleak with this library. Currently, I have to workaround it somehow, e.g. with a sleep or an ignore.

@seankhliao seankhliao changed the title goroutine leak detection made difficult with sql.DB.Close() database/sql: goroutine leak detection made difficult with sql.DB.Close() Jan 16, 2022
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 16, 2022
@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants