Skip to content

Commit cb9cfff

Browse files
committed
Return false from AutoCommit once Close has started
AutoCommit takes c.mu, which Close holds while SQLite runs callbacks such as the rollback hook. Since 0f12d4e moved the lock in Close in front of sqlite3_close_v2 (v1.14.37), a callback that calls AutoCommit waits for c.mu forever. Check the closing flag before taking c.mu, as dbConnOpen does, and report false, as AutoCommit does after Close.
1 parent c1910db commit cb9cfff

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

‎sqlite3.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,11 @@ func (c *SQLiteConn) RegisterAggregator(name string, impl any, pure bool) error
11021102
}
11031103

11041104
// AutoCommit return which currently auto commit or not.
1105-
// It reports false once the connection has been closed.
1105+
// It reports false once Close has started.
11061106
func (c *SQLiteConn) AutoCommit() bool {
1107+
if c.closing.Load() {
1108+
return false
1109+
}
11071110
c.mu.Lock()
11081111
defer c.mu.Unlock()
11091112
if c.db == nil {

‎sqlite3_test.go‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,21 +3103,25 @@ func TestConnMethodsAfterClose(t *testing.T) {
31033103
})
31043104
}
31053105

3106-
// TestConnMethodsDuringClose calls a connection method from a callback
3106+
// TestConnMethodsDuringClose calls connection methods from a callback
31073107
// that SQLite runs while Close closes the connection: closing a connection
31083108
// with an open transaction rolls it back and runs the rollback hook. Close
3109-
// holds c.mu meanwhile, so the method must not wait for it, and it sees the
3110-
// connection as closed.
3109+
// holds c.mu meanwhile, so the methods must not wait for it, and they see
3110+
// the connection as closed.
31113111
func TestConnMethodsDuringClose(t *testing.T) {
31123112
conn, err := (&SQLiteDriver{}).Open(":memory:")
31133113
if err != nil {
31143114
t.Fatal(err)
31153115
}
31163116
c := conn.(*SQLiteConn)
3117-
got := make(chan int, 1)
3117+
type values struct {
3118+
limit int
3119+
autoCommit bool
3120+
}
3121+
got := make(chan values, 1)
31183122
c.RegisterRollbackHook(func() {
31193123
select {
3120-
case got <- c.GetLimit(SQLITE_LIMIT_LENGTH):
3124+
case got <- values{c.GetLimit(SQLITE_LIMIT_LENGTH), c.AutoCommit()}:
31213125
default:
31223126
}
31233127
})
@@ -3135,9 +3139,12 @@ func TestConnMethodsDuringClose(t *testing.T) {
31353139
t.Fatal("Close did not return")
31363140
}
31373141
select {
3138-
case limit := <-got:
3139-
if limit != -1 {
3140-
t.Errorf("GetLimit() = %d during Close, want -1", limit)
3142+
case v := <-got:
3143+
if v.limit != -1 {
3144+
t.Errorf("GetLimit() = %d during Close, want -1", v.limit)
3145+
}
3146+
if v.autoCommit {
3147+
t.Error("AutoCommit() = true during Close, want false")
31413148
}
31423149
default:
31433150
t.Error("the rollback hook did not run")

0 commit comments

Comments
 (0)