// It is only used by Scan, Next, and NextResultSet which are expected
// not to be called concurrently.
closemuScanHold bool
+
+ // hitEOF is whether Next hit the end of the rows without
+ // encountering an error. It's set in Next before
+ // returning. It's only used by Next and Err which are
+ // expected not to be called concurrently.
+ hitEOF bool
}
// lasterrOrErrLocked returns either lasterr or the provided err.
if doClose {
rs.Close()
}
+ if doClose && !ok {
+ rs.hitEOF = true
+ }
return ok
}
// Err returns the error, if any, that was encountered during iteration.
// Err may be called after an explicit or implicit Close.
func (rs *Rows) Err() error {
- if errp := rs.contextDone.Load(); errp != nil {
- return *errp
+ // Return any context error that might've happened during row iteration,
+ // but only if we haven't reported the final Next() = false after rows
+ // are done, in which case the user might've canceled their own context
+ // before calling Rows.Err.
+ if !rs.hitEOF {
+ if errp := rs.contextDone.Load(); errp != nil {
+ return *errp
+ }
}
rs.closemu.RLock()
}
}
+func TestContextCancelBetweenNextAndErr(t *testing.T) {
+ db := newTestDB(t, "people")
+ defer closeDB(t, db)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ r, err := db.QueryContext(ctx, "SELECT|people|name|")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for r.Next() {
+ }
+ cancel() // wake up the awaitDone goroutine
+ time.Sleep(10 * time.Millisecond) // increase odds of seeing failure
+ if err := r.Err(); err != nil {
+ t.Fatal(err)
+ }
+}
+
// badConn implements a bad driver.Conn, for TestBadDriver.
// The Exec method panics.
type badConn struct{}