From b0940b663f8a08cdcbba5613756b997b0d0982cf Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 4 Dec 2018 00:24:18 +0900 Subject: [PATCH 1/2] Reset internal buffer when rows.Close() is called. Fixes #903 --- buffer.go | 6 ++++++ rows.go | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/buffer.go b/buffer.go index 19486bd6f..964920358 100644 --- a/buffer.go +++ b/buffer.go @@ -37,6 +37,12 @@ func newBuffer(nc net.Conn) buffer { } } +func (b *buffer) reset() { + b.buf = make([]byte, defaultBufSize) + b.idx = 0 + b.length = 0 +} + // fill reads into the buffer until at least _need_ bytes are in it func (b *buffer) fill(need int) error { n := b.length diff --git a/rows.go b/rows.go index d3b1e2822..08727f999 100644 --- a/rows.go +++ b/rows.go @@ -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.reset() + // Remove unread packets from stream if !rows.rs.done { err = mc.readUntilEOF() From 30ff57936e26e6d356506eb55bf74b174cf6f513 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 4 Dec 2018 10:15:23 +0900 Subject: [PATCH 2/2] fix --- buffer.go | 21 ++++++++++++++++++--- rows.go | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/buffer.go b/buffer.go index 964920358..d9ae7f52e 100644 --- a/buffer.go +++ b/buffer.go @@ -37,10 +37,25 @@ func newBuffer(nc net.Conn) buffer { } } -func (b *buffer) reset() { - b.buf = make([]byte, defaultBufSize) +// 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 >= defaultBufSize { + 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 - b.length = 0 } // fill reads into the buffer until at least _need_ bytes are in it diff --git a/rows.go b/rows.go index 08727f999..5fc6f23e4 100644 --- a/rows.go +++ b/rows.go @@ -113,7 +113,7 @@ func (rows *mysqlRows) Close() (err error) { // We can't reuse receive buffer when rows.Close() is called. // See https://github.com/golang/go/commit/651ddbdb5056ded455f47f9c494c67b389622a47 - mc.buf.reset() + mc.buf.discard() // Remove unread packets from stream if !rows.rs.done {