From 8cf4f297eb27e0c9d46623959a487187d5611a33 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 13 Feb 2016 13:20:10 -0800 Subject: [PATCH] pkg/sorted/sqlkv: don't release gate until iterator is closed Change-Id: I4c3f5406b89294c09fb1d64aaeffc3c581664acd --- pkg/sorted/sqlkv/sqlkv.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/sorted/sqlkv/sqlkv.go b/pkg/sorted/sqlkv/sqlkv.go index 1acf4a5c9..9a11ea684 100644 --- a/pkg/sorted/sqlkv/sqlkv.go +++ b/pkg/sorted/sqlkv/sqlkv.go @@ -204,12 +204,10 @@ func (kv *KeyValue) Wipe() error { func (kv *KeyValue) Close() error { return kv.DB.Close() } func (kv *KeyValue) Find(start, end string) sorted.Iterator { + var releaseGate func() // nil if unused if kv.Gate != nil { kv.Gate.Start() - // TODO(mpl): looks like sqlite considers the db locked until we've closed - // the iterator, so we can't do anything else until then. We should probably - // move that Unlock to the closing of the iterator. Investigating. - defer kv.Gate.Done() + releaseGate = kv.Gate.Done } var rows *sql.Rows var err error @@ -224,9 +222,10 @@ func (kv *KeyValue) Find(start, end string) sorted.Iterator { } it := &iter{ - kv: kv, - rows: rows, - closeCheck: leak.NewChecker(), + kv: kv, + rows: rows, + closeCheck: leak.NewChecker(), + releaseGate: releaseGate, } return it } @@ -239,7 +238,8 @@ type iter struct { end string // optional end bound err error // accumulated error, returned at Close - closeCheck *leak.Checker + closeCheck *leak.Checker + releaseGate func() // if non-nil, called on Close rows *sql.Rows // if non-nil, the rows we're reading from @@ -276,6 +276,9 @@ func (t *iter) Close() error { t.rows.Close() t.rows = nil } + if t.releaseGate != nil { + t.releaseGate() + } err := t.err t.err = errClosed return err