From c026845bd2984002168e7496cb9d0150f79164d0 Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Tue, 7 Feb 2017 10:19:02 -0800 Subject: database/sql: record the context error in Rows if canceled Previously it was intended that Rows.Scan would return an error and Rows.Err would return nil. This was problematic because drivers could not differentiate between a normal Rows.Close or a context cancel close. The alternative is to require drivers to return a Scan to return an error if the driver is closed while there are still rows to be read. This is currently not how several drivers currently work and may be difficult to detect when there are additional rows. At the same time guard the the Rows.lasterr and prevent a close while a Rows operation is active. For the drivers that do not have Context methods, do not check for context cancelation after the operation, but before for any operation that may modify the database state. Fixes #18961 Change-Id: I49a25318ecd9f97a35d5b50540ecd850c01cfa5e Reviewed-on: https://go-review.googlesource.com/36485 Reviewed-by: Russ Cox Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot --- src/database/sql/sql_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/database/sql/sql_test.go') diff --git a/src/database/sql/sql_test.go b/src/database/sql/sql_test.go index 2c0417dc78..1cc38a5838 100644 --- a/src/database/sql/sql_test.go +++ b/src/database/sql/sql_test.go @@ -313,9 +313,13 @@ func TestQueryContext(t *testing.T) { got = append(got, r) index++ } - err = rows.Err() - if err != nil { - t.Fatalf("Err: %v", err) + select { + case <-ctx.Done(): + if err := ctx.Err(); err != context.Canceled { + t.Fatalf("context err = %v; want context.Canceled") + } + default: + t.Fatalf("context err = nil; want context.Canceled") } want := []row{ {age: 1, name: "Alice"}, -- cgit v1.3