Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,27 @@ func lookupHandle(handle unsafe.Pointer) any {
return lookupHandleVal(handle).val
}

// deleteHandle releases a single handle created by newHandle. It is a no-op
// if the handle is unknown (e.g. already released).
func deleteHandle(handle unsafe.Pointer) {
handleLock.Lock()
defer handleLock.Unlock()

current := loadHandleVals()
if _, ok := current[handle]; !ok {
return
}
next := make(map[unsafe.Pointer]handleVal, len(current)-1)
for h, v := range current {
if h == handle {
continue
}
next[h] = v
}
handleVals.Store(next)
C.free(handle)
}

func deleteHandles(db *SQLiteConn) {
handleLock.Lock()
defer handleLock.Unlock()
Expand Down
7 changes: 7 additions & 0 deletions sqlite3_opt_vtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ func goVRelease(pVTab unsafe.Pointer, isDestroy C.int) *C.char {
} else {
err = vt.vTab.Disconnect()
}
// The vtab is gone as far as SQLite is concerned regardless of the
// callback result, so release the handle either way.
deleteHandle(pVTab)
if err != nil {
return mPrintf("%s", err.Error())
}
Expand Down Expand Up @@ -492,6 +495,9 @@ func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
func goVClose(pCursor unsafe.Pointer) *C.char {
vtc := lookupHandle(pCursor).(*sqliteVTabCursor)
err := vtc.vTabCursor.Close()
// The cursor is gone as far as SQLite is concerned regardless of the
// callback result, so release the handle either way.
deleteHandle(pCursor)
if err != nil {
return mPrintf("%s", err.Error())
}
Expand All @@ -502,6 +508,7 @@ func goVClose(pCursor unsafe.Pointer) *C.char {
func goMDestroy(pClientData unsafe.Pointer) {
m := lookupHandle(pClientData).(*sqliteModule)
m.module.DestroyModule()
deleteHandle(pClientData)
}

//export goVFilter
Expand Down
38 changes: 38 additions & 0 deletions sqlite3_opt_vtable_leak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//go:build sqlite_vtable

package sqlite3

import (
"database/sql"
"testing"
)

func TestVtabCursorHandleRelease(t *testing.T) {
sql.Register("sqlite3_HandleLeakCheck", &SQLiteDriver{
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
ConnectHook: func(conn *SQLiteConn) error {
return conn.CreateModule("test", &testModule{t: t, intarray: []int{1, 2, 3}})
},
})
db, err := sql.Open("sqlite3_HandleLeakCheck", ":memory:")
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec("CREATE VIRTUAL TABLE vtab USING test('1', 2, three)"); err != nil {
t.Fatal(err)
}
var before, after int
for i := 0; i < 50; i++ {
var n int
if err := db.QueryRow("SELECT count(*) FROM vtab").Scan(&n); err != nil {
t.Fatal(err)
}
if i == 0 {
before = len(loadHandleVals())
}
}
after = len(loadHandleVals())
if after > before {
t.Fatalf("handle map grew from %d to %d over repeated cursor open/close", before, after)
}
}
Loading