Skip to content

Commit 27c002e

Browse files
author
Collin Van Dyck
committed
Ensure that SqliteStmt.closed property is guarded.
Because the closed property of the SQLiteRows's *SqliteStmt was not guarded, it was causing an issue during context cancellation. https://github.com/segmentio/go-sqlite3/blob/be424d27acde822f080bdcd8a7ae6abd4d7d801e/sqlite3.go#L1785-L1796 When a statement is performing a query(), if it determines that the context has been canceled, it will launch a goroutine that closes the resulting driver.Rows if it's not already completed. If the driver.Rows is done (and the context has been canceled), it will interrupt the connection and more importantly, perform a rows.Close(). The method rows.Close() guards the closed bool with a sync.Mutex to set it to true. If a reader is reading from the SqliteRow, it will call Next() and that performs this check: https://github.com/segmentio/go-sqlite3/blob/be424d27acde822f080bdcd8a7ae6abd4d7d801e/sqlite3.go#L1915-L1917 Because this is not guarded, a data race ensues, and this was actually caught by the Go race detector recently. I didn't include a test case here because the fix seemed straightforward enough and because race conditions are hard to test for. It's been verified in another program that this fixes the issue. If tests should be provided I'm more than happy to do so.
1 parent be424d2 commit 27c002e

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

sqlite3.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,11 +1912,11 @@ func (rc *SQLiteRows) DeclTypes() []string {
19121912

19131913
// Next move cursor to next.
19141914
func (rc *SQLiteRows) Next(dest []driver.Value) error {
1915+
rc.s.mu.Lock()
1916+
defer rc.s.mu.Unlock()
19151917
if rc.s.closed {
19161918
return io.EOF
19171919
}
1918-
rc.s.mu.Lock()
1919-
defer rc.s.mu.Unlock()
19201920
rv := C.sqlite3_step(rc.s.s)
19211921
if rv == C.SQLITE_DONE {
19221922
return io.EOF

0 commit comments

Comments
 (0)