Skip to content

Commit c1910db

Browse files
committed
Set closing before Close finalizes cached statements
Finalizing a cached statement can run a callback too: after PRAGMA writable_schema=RESET, a cached query on a virtual table holds the last reference to it, so finalizing the statement calls the module's Disconnect. Close did that with c.mu held but before it set closing, so a checked method called from Disconnect, such as GetLimit, still waited for c.mu forever. Set closing before closeCachedStmtsLocked.
1 parent 1dc96a0 commit c1910db

2 files changed

Lines changed: 87 additions & 4 deletions

File tree

‎sqlite3.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,11 @@ type SQLiteDriver struct {
549549
type SQLiteConn struct {
550550
mu sync.Mutex
551551
db *C.sqlite3
552-
// closing is set once Close starts closing the connection, with mu
553-
// held. SQLite runs callbacks such as the rollback hook while it
554-
// closes the connection, so dbConnOpen reads closing without mu.
552+
// closing is set, with mu held, once Close starts closing the
553+
// connection. SQLite runs callbacks such as the rollback hook or the
554+
// Disconnect method of a virtual table while Close finalizes cached
555+
// statements and closes the connection, so dbConnOpen reads closing
556+
// without mu.
555557
closing atomic.Bool
556558
// activeRows identifies the cancellable Rows currently calling sqlite3_step.
557559
// It is guarded by mu so a stale cancellation cannot interrupt later work.
@@ -2112,8 +2114,8 @@ func (c *SQLiteConn) Close() error {
21122114
return nil
21132115
}
21142116
runtime.SetFinalizer(c, nil)
2115-
c.closeCachedStmtsLocked()
21162117
c.closing.Store(true)
2118+
c.closeCachedStmtsLocked()
21172119
rv := C.sqlite3_close_v2(c.db)
21182120
if rv != C.SQLITE_OK {
21192121
c.closing.Store(false)

‎sqlite3_opt_vtable_destroy_test.go‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"sync/atomic"
1212
"testing"
13+
"time"
1314
)
1415

1516
type destroyErrModule struct{}
@@ -83,3 +84,83 @@ func TestVTabDestroyErrorThenClose(t *testing.T) {
8384
t.Logf("close err: %v", err)
8485
}
8586
}
87+
88+
type closeLimitModule struct{ limits chan int }
89+
90+
type closeLimitVTab struct {
91+
c *SQLiteConn
92+
limits chan int
93+
}
94+
95+
func (m closeLimitModule) Create(c *SQLiteConn, args []string) (VTab, error) {
96+
if err := c.DeclareVTab("CREATE TABLE x(test TEXT)"); err != nil {
97+
return nil, err
98+
}
99+
return &closeLimitVTab{c: c, limits: m.limits}, nil
100+
}
101+
func (m closeLimitModule) Connect(c *SQLiteConn, args []string) (VTab, error) {
102+
return m.Create(c, args)
103+
}
104+
func (m closeLimitModule) DestroyModule() {}
105+
106+
func (v *closeLimitVTab) BestIndex(cst []InfoConstraint, ob []InfoOrderBy) (*IndexResult, error) {
107+
return &IndexResult{Used: make([]bool, len(cst)), EstimatedCost: 100}, nil
108+
}
109+
func (v *closeLimitVTab) Disconnect() error {
110+
select {
111+
case v.limits <- v.c.GetLimit(SQLITE_LIMIT_LENGTH):
112+
default:
113+
}
114+
return nil
115+
}
116+
func (v *closeLimitVTab) Destroy() error { return nil }
117+
func (v *closeLimitVTab) Open() (VTabCursor, error) {
118+
return &destroyErrCursor{}, nil
119+
}
120+
121+
// TestVTabDisconnectDuringClose: after PRAGMA writable_schema=RESET, a
122+
// cached statement holds the last reference to a virtual table, so Close
123+
// calls Disconnect when it finalizes the statement. Close holds c.mu
124+
// meanwhile, so a method that Disconnect calls must not wait for it.
125+
func TestVTabDisconnectDuringClose(t *testing.T) {
126+
conn, err := (&SQLiteDriver{}).Open(":memory:?_stmt_cache_size=4")
127+
if err != nil {
128+
t.Fatal(err)
129+
}
130+
c := conn.(*SQLiteConn)
131+
limits := make(chan int, 1)
132+
if err := c.CreateModule("closelimit", closeLimitModule{limits}); err != nil {
133+
t.Fatal(err)
134+
}
135+
if _, err := c.Exec("CREATE VIRTUAL TABLE vtab USING closelimit()", nil); err != nil {
136+
t.Fatal(err)
137+
}
138+
rows, err := c.Query("SELECT * FROM vtab", nil)
139+
if err != nil {
140+
t.Fatal(err)
141+
}
142+
if err := rows.Close(); err != nil {
143+
t.Fatal(err)
144+
}
145+
if _, err := c.Exec("PRAGMA writable_schema=RESET", nil); err != nil {
146+
t.Fatal(err)
147+
}
148+
closed := make(chan error, 1)
149+
go func() { closed <- c.Close() }()
150+
select {
151+
case err := <-closed:
152+
if err != nil {
153+
t.Fatal(err)
154+
}
155+
case <-time.After(10 * time.Second):
156+
t.Fatal("Close did not return")
157+
}
158+
select {
159+
case limit := <-limits:
160+
if limit != -1 {
161+
t.Errorf("GetLimit() = %d in Disconnect, want -1", limit)
162+
}
163+
default:
164+
t.Error("Disconnect did not run")
165+
}
166+
}

0 commit comments

Comments
 (0)