Commit 6348764
committed
fix(db): stop closing a read connection out from under a live query
Found by smoke-testing 2.1.4 and reading the log - which is only possible now
that #263 stopped one call producing 98.5% of it. Closing the Monitor logged:
WidgetState.get_hardware_history - Cannot operate on a closed database
sqlite3.ProgrammingError: Cannot operate on a closed database
Read connections are per-thread, and stale ones were evicted by checking the
owning thread id against threading.enumerate(). Qt worker threads never appear
there: enumerate() only lists threads the threading module knows about, and a
QThread that calls threading.get_ident() - which is all ours do - is never
registered. So the Monitor's graph worker was permanently "dead" by that test,
and the next _get_read_conn() from any other thread closed its connection
while it was still querying.
The obvious fix is wrong, and I verified rather than assumed: calling
threading.current_thread() does register the thread, but on CPython 3.11 the
resulting _DummyThread never leaves enumerate() when the native thread dies -
so nothing would ever be pruned and the connection leak that pruning exists to
prevent comes straight back.
Pruning is now by idle time. A thread mid-query has just fetched its
connection, so it cannot trip a threshold; a finished worker ages out and is
reclaimed as before. The threshold is deliberately generous - it only has to
exceed a live worker's refresh interval, and erring high costs one idle file
handle while erring low reintroduces the bug.
The existing leak test is kept and joined by the race it could not see.
Verified by reverting: the race test fails on the old logic.1 parent a64faaf commit 6348764
2 files changed
Lines changed: 92 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
| |||
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
138 | 144 | | |
139 | 145 | | |
140 | 146 | | |
141 | 147 | | |
142 | 148 | | |
143 | 149 | | |
144 | 150 | | |
145 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
146 | 168 | | |
| 169 | + | |
147 | 170 | | |
148 | 171 | | |
149 | | - | |
150 | | - | |
| 172 | + | |
| 173 | + | |
151 | 174 | | |
152 | | - | |
| 175 | + | |
153 | 176 | | |
154 | 177 | | |
155 | 178 | | |
| |||
164 | 187 | | |
165 | 188 | | |
166 | 189 | | |
167 | | - | |
168 | | - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
169 | 195 | | |
170 | 196 | | |
171 | 197 | | |
| |||
923 | 949 | | |
924 | 950 | | |
925 | 951 | | |
926 | | - | |
| 952 | + | |
927 | 953 | | |
928 | 954 | | |
929 | 955 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
42 | | - | |
| 43 | + | |
43 | 44 | | |
44 | | - | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
51 | 52 | | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | 53 | | |
57 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
58 | 61 | | |
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 | + | |
62 | 109 | | |
63 | 110 | | |
64 | 111 | | |
| |||
0 commit comments