Repository navigation
Expand file tree
/
Copy pathcallback_panic_test.go
More file actions
158 lines (144 loc) · 4.24 KB
/
Copy pathcallback_panic_test.go
File metadata and controls
158 lines (144 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//go:build cgo
package sqlite3
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"runtime"
"testing"
"time"
)
// closeAfterFuncFromOtherThread keeps the calling goroutine pinned to its
// OS thread so Close runs on a different thread. A panic inside the
// registered function previously left SQLite's connection mutex held by
// the first thread, so Close would hang forever (see #1476).
func closeAfterFuncFromOtherThread(t *testing.T, fn any) {
t.Helper()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
conn, err := (&SQLiteDriver{}).Open(":memory:")
if err != nil {
t.Fatal(err)
}
c := conn.(*SQLiteConn)
if err := c.RegisterFunc("boom", fn, false); err != nil {
t.Fatal(err)
}
func() {
defer func() { recover() }()
rows, err := c.Query("SELECT boom()", nil)
if err != nil {
return
}
rows.Next(make([]driver.Value, 1))
}()
done := make(chan error, 1)
go func() { done <- c.Close() }()
select {
case err := <-done:
if err != nil {
t.Fatal(err)
}
case <-time.After(5 * time.Second):
t.Fatal("Close did not return")
}
}
func TestRegisterFuncPanicReleasesConnectionMutex(t *testing.T) {
closeAfterFuncFromOtherThread(t, func() int64 { panic("boom") })
}
func TestRegisterFuncErrorStillCloses(t *testing.T) {
closeAfterFuncFromOtherThread(t, func() (int64, error) { return 0, errors.New("boom") })
}
// ExecContext with a cancellable context runs the statement on a worker
// goroutine. A callback panic used to be re-raised there, where recover is
// impossible, killing the process instead of reaching the caller (see #1476).
func TestRegisterFuncPanicPropagatesFromExecContext(t *testing.T) {
conn, err := (&SQLiteDriver{}).Open(":memory:")
if err != nil {
t.Fatal(err)
}
c := conn.(*SQLiteConn)
defer func() {
if closeErr := c.Close(); closeErr != nil {
t.Fatal(closeErr)
}
}()
if err := c.RegisterFunc("boom", func() int64 { panic("boom") }, false); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
defer func() {
r := recover()
if r == nil {
t.Fatal("callback panic did not reach the caller goroutine")
}
if got := fmt.Sprint(r); got != "boom" {
t.Fatalf("unexpected panic value: %v", r)
}
}()
if _, execErr := c.ExecContext(ctx, "SELECT boom()", nil); execErr != nil {
t.Fatalf("ExecContext returned an error instead of the callback panic: %v", execErr)
}
t.Fatal("ExecContext returned without re-raising the callback panic")
}
// The interrupt path waits for the worker before returning, so it must also
// re-raise a callback panic instead of returning the interrupt status.
func TestRegisterFuncPanicAfterContextCancel(t *testing.T) {
conn, err := (&SQLiteDriver{}).Open(":memory:")
if err != nil {
t.Fatal(err)
}
c := conn.(*SQLiteConn)
defer func() {
if closeErr := c.Close(); closeErr != nil {
t.Fatal(closeErr)
}
}()
if err := c.RegisterFunc("boom", func() int64 {
panic("boom after cancel")
}, false); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
defer func() {
r := recover()
if r == nil {
t.Fatal("callback panic did not reach the caller goroutine")
}
if got := fmt.Sprint(r); got != "boom after cancel" {
t.Fatalf("unexpected panic value: %v", r)
}
}()
if _, execErr := c.ExecContext(ctx, "SELECT boom()", nil); execErr != nil {
t.Fatalf("ExecContext returned an error instead of the callback panic: %v", execErr)
}
t.Fatal("ExecContext returned without re-raising the callback panic")
}
// Exec without args takes the execNoArgs fast path, which must re-raise a
// callback panic itself instead of leaving it for the next statement.
func TestRegisterFuncPanicPropagatesFromExecNoArgs(t *testing.T) {
conn, err := (&SQLiteDriver{}).Open(":memory:")
if err != nil {
t.Fatal(err)
}
c := conn.(*SQLiteConn)
defer c.Close()
if err := c.RegisterFunc("boom", func() int64 { panic("boom") }, false); err != nil {
t.Fatal(err)
}
func() {
defer func() {
if r := recover(); fmt.Sprint(r) != "boom" {
t.Fatalf("unexpected panic value: %v", r)
}
}()
_, err := c.Exec("SELECT boom()", nil)
t.Fatalf("Exec returned without re-raising the callback panic: %v", err)
}()
if _, err := c.Exec("SELECT 1", nil); err != nil {
t.Fatal(err)
}
}