From 48729daaa63437b184bab77c3525610ebb101865 Mon Sep 17 00:00:00 2001 From: Ryan Chou <88779759+ryanchou1994@users.noreply.github.com> Date: Fri, 6 Nov 2026 08:59:26 +0800 Subject: [PATCH] Release the authorizer callback that RegisterAuthorizer replaces RegisterAuthorizer created a callback handle on every call and never released the one it replaced, so every replaced or removed authorizer stayed in handleVals, with whatever its closure references, until the connection closed. A connection has a single authorizer, and SQLite no longer calls the previous one once sqlite3_set_authorizer returns, so its handle can be released then. The connection now keeps the handle of its authorizer and releases it when SQLite accepts the replacement. A mutex of its own guards it, not c.mu: sqlite3_set_authorizer waits for the database mutex while a statement runs, and holding c.mu meanwhile would block cancelling that statement, which takes c.mu to call sqlite3_interrupt. This is go-sqlite3 commit 068ac771 (mattn/go-sqlite3#1458), unchanged, with its test. --- sqlite3.go | 28 ++++++++++-- sqlite3_test.go | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+), 4 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index a4aaf7d3..d5ec9b97 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -496,6 +496,11 @@ type SQLiteConn struct { txlock string funcs []*functionInfo aggregators []*aggInfo + // authorizerMu guards authorizerHandle. It is deliberately not c.mu: + // sqlite3_set_authorizer takes db->mutex, while the cancellation path + // takes c.mu, so holding c.mu here would invert the lock order. + authorizerMu sync.Mutex + authorizerHandle unsafe.Pointer // Prepared-statement cache. The slice is allocated at Open with a // fixed capacity equal to the configured cache size; cap bounds the // cache, len is the live count, and entries are ordered LRU-first @@ -751,11 +756,22 @@ func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64 // depending on operation. More details see: // https://www.sqlite.org/c3ref/c_alter_table.html func (c *SQLiteConn) RegisterAuthorizer(callback func(int, string, string, string) int) { - if callback == nil { - C.sqlite3_set_authorizer(c.db, nil, nil) - } else { - C.sqlite3_set_authorizer(c.db, (*[0]byte)(C.authorizerTrampoline), newHandle(c, callback)) + c.authorizerMu.Lock() + defer c.authorizerMu.Unlock() + + var handle unsafe.Pointer + var trampoline *[0]byte + if callback != nil { + handle = newHandle(c, callback) + trampoline = (*[0]byte)(C.authorizerTrampoline) + } + if C.sqlite3_set_authorizer(c.db, trampoline, handle) != C.SQLITE_OK { + deleteHandle(handle) + return } + // SQLite no longer uses the previous callback after replacing it. + deleteHandle(c.authorizerHandle) + c.authorizerHandle = handle } // RegisterFunc makes a Go function available as a SQLite function. @@ -2024,6 +2040,10 @@ func (c *SQLiteConn) Close() error { return lastError(c.db) } deleteHandles(c) + // Lock order is c.mu -> authorizerMu; RegisterAuthorizer never takes c.mu. + c.authorizerMu.Lock() + c.authorizerHandle = nil + c.authorizerMu.Unlock() c.db = nil return nil } diff --git a/sqlite3_test.go b/sqlite3_test.go index 4e7cf58d..6b40f83e 100644 --- a/sqlite3_test.go +++ b/sqlite3_test.go @@ -1971,6 +1971,124 @@ func TestAuthorizer(t *testing.T) { } } +func TestAuthorizerHandleRelease(t *testing.T) { + openConn := func() *SQLiteConn { + t.Helper() + conn, err := (&SQLiteDriver{}).Open(":memory:") + if err != nil { + t.Fatal(err) + } + c := conn.(*SQLiteConn) + t.Cleanup(func() { + if err := c.Close(); err != nil { + t.Error(err) + } + }) + return c + } + countHandles := func(c *SQLiteConn) int { + got := 0 + handleVals.Range(func(_, value any) bool { + if value.(handleVal).db == c { + got++ + } + return true + }) + return got + } + checkHandles := func(c *SQLiteConn, want int) { + t.Helper() + got := countHandles(c) + if got != want { + t.Fatalf("connection has %d callback handles, want %d", got, want) + } + } + + c, other := openConn(), openConn() + updates := 0 + c.RegisterUpdateHook(func(int, string, string, int64) { updates++ }) + if err := c.RegisterFunc("retained", func() int { return 42 }, true); err != nil { + t.Fatal(err) + } + if _, err := c.Exec("CREATE TABLE foo (value INTEGER)", nil); err != nil { + t.Fatal(err) + } + baseline := countHandles(c) + otherBaseline := countHandles(other) + otherCalls := 0 + other.RegisterAuthorizer(func(int, string, string, string) int { + otherCalls++ + return SQLITE_OK + }) + checkHandles(other, otherBaseline+1) + c.RegisterAuthorizer(nil) + checkHandles(c, baseline) + + calls := make([]int, 3) + for i := range calls { + i := i + previous := append([]int(nil), calls...) + c.RegisterAuthorizer(func(int, string, string, string) int { + calls[i]++ + return SQLITE_OK + }) + checkHandles(c, baseline+1) + if _, err := c.Exec("INSERT INTO foo VALUES (retained())", nil); err != nil { + t.Fatal(err) + } + if calls[i] == 0 { + t.Fatalf("authorizer %d was not called", i) + } + if !reflect.DeepEqual(calls[:i], previous[:i]) { + t.Fatal("a replaced authorizer was called") + } + } + + previous := append([]int(nil), calls...) + c.RegisterAuthorizer(nil) + c.RegisterAuthorizer(nil) + checkHandles(c, baseline) + if _, err := c.Exec("INSERT INTO foo VALUES (retained())", nil); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(calls, previous) { + t.Fatal("a removed authorizer was called") + } + if updates != len(calls)+1 { + t.Fatalf("update hook called %d times, want %d", updates, len(calls)+1) + } + rows, err := c.Query("SELECT sum(value) FROM foo", nil) + if err != nil { + t.Fatal(err) + } + values := make([]driver.Value, 1) + if err := rows.Next(values); err != nil { + rows.Close() + t.Fatal(err) + } + if err := rows.Close(); err != nil { + t.Fatal(err) + } + if values[0] != int64(42*(len(calls)+1)) { + t.Fatalf("sum(value) = %v, want %d", values[0], 42*(len(calls)+1)) + } + if err := c.Close(); err != nil { + t.Fatal(err) + } + checkHandles(c, 0) + checkHandles(other, otherBaseline+1) + if _, err := other.Exec("SELECT 1", nil); err != nil { + t.Fatal(err) + } + if otherCalls == 0 { + t.Fatal("the other connection's authorizer was not called") + } + if err := other.Close(); err != nil { + t.Fatal(err) + } + checkHandles(other, 0) +} + func TestSetFileControlInt(t *testing.T) { t.Run("PERSIST_WAL", func(t *testing.T) { tempFilename := TempFilename(t)