]> Cypherpunks repositories - gostls13.git/commitdiff
database/sql: return context errors from Rows.Scan
authorEric Rykwalder <e.rykwalder@gmail.com>
Tue, 3 Apr 2018 05:15:59 +0000 (22:15 -0700)
committerDaniel Theophanes <kardianos@gmail.com>
Wed, 11 Apr 2018 19:45:16 +0000 (19:45 +0000)
The previous implementation would return "sql: Rows are closed" for any
context errors, which can be confusing for context timeouts or
cancelations.

Fixes #24431

Change-Id: I884904ec43204c43f4e94e2335b2802aab77a888
Reviewed-on: https://go-review.googlesource.com/104276
Reviewed-by: Daniel Theophanes <kardianos@gmail.com>
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/database/sql/sql.go
src/database/sql/sql_test.go

index 355b6aa300a1f3622179c62a29b735e57413a954..beccf7fec1fe1e43f8f38abfebec19facec3ced2 100644 (file)
@@ -2870,6 +2870,11 @@ func rowsColumnInfoSetupConnLocked(rowsi driver.Rows) []*ColumnType {
 // string inputs parseable by strconv.ParseBool.
 func (rs *Rows) Scan(dest ...interface{}) error {
        rs.closemu.RLock()
+
+       if rs.lasterr != nil && rs.lasterr != io.EOF {
+               rs.closemu.RUnlock()
+               return rs.lasterr
+       }
        if rs.closed {
                rs.closemu.RUnlock()
                return errors.New("sql: Rows are closed")
index 12cea0de9fcfbc609423cfcc855d5794d4e7b69a..f194744aefe727b0b0e51039de90c0fb29ac4345 100644 (file)
@@ -325,8 +325,8 @@ func TestQueryContext(t *testing.T) {
                        }
                        t.Fatalf("Scan: %v", err)
                }
-               if index == 2 && err == nil {
-                       t.Fatal("expected an error on last scan")
+               if index == 2 && err != context.Canceled {
+                       t.Fatalf("Scan: %v; want context.Canceled", err)
                }
                got = append(got, r)
                index++