Skip to content

fix race on cancelling context passed to QueryContext #905

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
"time"
)

const defaultBufSize = 4096
const minBufSize = 4096
const defaultBufSize = 16 * 1024

// A buffer which is used for both reading and writing.
// This is possible since communication on each connection is synchronous.
Expand All @@ -37,6 +38,27 @@ func newBuffer(nc net.Conn) buffer {
}
}

// discard trims b.buf[:b.idx] to prohibit it reused.
//
// This is required by Rows.Close().
// See https://github.com/golang/go/commit/651ddbdb5056ded455f47f9c494c67b389622a47
func (b *buffer) discard() {
if len(b.buf)-b.idx >= minBufSize {
b.buf = b.buf[b.idx:]
b.idx = 0
return
}

bufSize := defaultBufSize
if bufSize < b.length {
bufSize = b.length
}
newBuf := make([]byte, bufSize)
copy(newBuf, b.buf[b.idx:b.idx+b.length])
b.buf = newBuf
b.idx = 0
}

// fill reads into the buffer until at least _need_ bytes are in it
func (b *buffer) fill(need int) error {
n := b.length
Expand All @@ -47,11 +69,9 @@ func (b *buffer) fill(need int) error {
}

// grow buffer if necessary
// TODO: let the buffer shrink again at some point
// Maybe keep the org buf slice and swap back?
if need > len(b.buf) {
// Round up to the next multiple of the default size
newBuf := make([]byte, ((need/defaultBufSize)+1)*defaultBufSize)
newBuf := make([]byte, ((need/minBufSize)+1)*minBufSize)
copy(newBuf, b.buf)
b.buf = newBuf
}
Expand Down Expand Up @@ -129,7 +149,7 @@ func (b *buffer) takeBuffer(length int) ([]byte, error) {
}

// takeSmallBuffer is shortcut which can be used if length is
// known to be smaller than defaultBufSize.
// known to be smaller than minBufSize.
// Only one buffer (total) can be used at a time.
func (b *buffer) takeSmallBuffer(length int) ([]byte, error) {
if b.length > 0 {
Expand Down
4 changes: 4 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (rows *mysqlRows) Close() (err error) {
return err
}

// We can't reuse receive buffer when rows.Close() is called.
// See https://github.com/golang/go/commit/651ddbdb5056ded455f47f9c494c67b389622a47
mc.buf.discard()

// Remove unread packets from stream
if !rows.rs.done {
err = mc.readUntilEOF()
Expand Down