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: small DB.SetConnMaxLifetime does not release idle connection #40841

Closed
Warashi opened this issue Aug 17, 2020 · 4 comments
Closed

database/sql: small DB.SetConnMaxLifetime does not release idle connection #40841

Warashi opened this issue Aug 17, 2020 · 4 comments
Assignees
Milestone

Comments

@Warashi
Copy link
Contributor

@Warashi Warashi commented Aug 17, 2020

What version of Go are you using (go version)?

$ go1.15 version
go version go1.15 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go1.15 env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/s01062/Library/Caches/go-build"
GOENV="/Users/s01062/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/s01062/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/s01062/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/s01062/sdk/go1.15"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/s01062/sdk/go1.15/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Volumes/ramdisk/t/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kr/xqgv_vjn6mzgbrlhhzjny2w4b3pxzd/T/go-build776631324=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://play.golang.org/p/jav62YYGNJc

package main

import (
	"fmt"
	"time"

	"github.com/DATA-DOG/go-sqlmock"
)

func main() {
	fmt.Println("start")

	db, mock, err := sqlmock.New()
	if err != nil {
		panic(err)
	}
	mock.ExpectQuery(
		".*",
	).WillReturnRows(
		sqlmock.NewRows([]string{"entry_id"}).AddRow(1))

	rows, err := db.Query("SELECT entry_id")
	if err != nil {
		panic(err)
	}
	for rows.Next() {
	}
	if err := rows.Err(); err != nil {
		panic(err)
	}
	time.Sleep(1 * time.Second)
	db.SetConnMaxLifetime(1 * time.Nanosecond)
	for {
		stats := db.Stats()
		if stats.OpenConnections == 0 {
			break
		}
		fmt.Println("OpenConnections:", stats.OpenConnections)
		fmt.Println("Idle:", stats.Idle)
		fmt.Println("InUse:", stats.InUse)
		time.Sleep(time.Second)
	}
	fmt.Println("completed")
}

What did you expect to see?

When I do go1.14.7 run main.go, got below result.

start
OpenConnections: 1
Idle: 1
InUse: 0
OpenConnections: 1
Idle: 1
InUse: 0
completed

What did you see instead?

When I do go1.15 run main.go, got below result and program do not exit.

start
OpenConnections: 1
Idle: 1
InUse: 0
OpenConnections: 1
Idle: 1
InUse: 0
OpenConnections: 1
Idle: 1
InUse: 0
OpenConnections: 1
Idle: 1
InUse: 0
OpenConnections: 1
Idle: 1
InUse: 0
OpenConnections: 1
Idle: 1
InUse: 0
......
@gopherbot
Copy link

@gopherbot gopherbot commented Aug 17, 2020

Change https://golang.org/cl/248817 mentions this issue: database/sql: fix sql.DB.shortestIdleTimeLocked to return non-zero value

@dmitshur
Copy link
Member

@dmitshur dmitshur commented Aug 18, 2020

@dmitshur dmitshur added this to the Backlog milestone Aug 18, 2020
@dmitshur dmitshur changed the title database/sql: Set small DB.SetConnMaxLifetime does not release idle connection database/sql: small DB.SetConnMaxLifetime does not release idle connection Aug 18, 2020
@id9051
Copy link

@id9051 id9051 commented Aug 28, 2020

We have run into this issue was well, but with longer max lifetime value. In the example code on this ticket the max lifetime is set to 1 nanosecond. We use a value if 5 minutes and still have DB connections remain open after 5 minutes. Downgrading from Go v1.15 to Go v1.14.7 resolved this issue for us.

@kardianos kardianos self-assigned this Aug 28, 2020
@gopherbot gopherbot closed this in 4fc3896 Aug 29, 2020
@Warashi
Copy link
Contributor Author

@Warashi Warashi commented Sep 11, 2020

Isn't the fix for this issue included in the patch version?
Due to this problem, I can't upgrade to 1.15, so please include it if possible.

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

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.