Skip to content

Commit

Permalink
Check error return of rows.Close(). (#6)
Browse files Browse the repository at this point in the history
According to
https://github.blog/2020-05-20-three-bugs-in-the-go-mysql-driver/, when
QueryContext is called with a context that is cancelled during scan, you
can receive incomplete or corrupted results. As I understand it, the
corruption is fixed upstream, but it's still possible to get incomplete
results that will only show up in the error result from Close.

It's still possible and correct to call `defer rows.Close()`, since the
database/sql docs say this:

https://godoc.org/database/sql#Rows.Close

> Close is idempotent and does not affect the result of Err.

Note: This is a recreation of go-gorp#420

Co-authored-by: Jacob Hoffman-Andrews <[email protected]>
  • Loading branch information
aarongable and jsha authored Jul 5, 2023
1 parent a4dac79 commit 792de22
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion select.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ func selectVal(e SqlExecutor, holder interface{}, query string, args ...interfac
return sql.ErrNoRows
}

return rows.Scan(holder)
err = rows.Scan(holder)
if err != nil {
return err
}

return rows.Close()
}

func hookedselect(m *DbMap, exec SqlExecutor, i interface{}, query string,
Expand Down Expand Up @@ -351,6 +356,11 @@ func rawselect(m *DbMap, exec SqlExecutor, i interface{}, query string,
}
}

err = rows.Close()
if err != nil {
return nil, err
}

if appendToSlice && sliceValue.IsNil() {
sliceValue.Set(reflect.MakeSlice(sliceValue.Type(), 0, 0))
}
Expand Down

0 comments on commit 792de22

Please sign in to comment.