Commit 2751290
fix: rollback on drop so write conn returns to pool clean (#46)
Dropping an ActiveInterruptibleTransaction without commit/rollback left
the write connection in the pool with an open transaction. sqlx pools
reuse connections rather than closing them, so SQLite's close-time
auto-rollback never fired, and the next acquire_writer() got a
connection where BEGIN IMMEDIATE failed with "cannot start a
transaction within a transaction".
Drop now takes the writer and spawns a tokio task that issues ROLLBACK
(and detach_if_attached) before the connection is released to the pool.
The rollback task is bounded by a 5 s timeout so a stuck ROLLBACK
cannot hold the single-writer permit indefinitely. To eliminate a panic
risk at Drop time on threads without a tokio thread-local (notably
Tauri teardown after a programmatic app_handle.exit(N) on the main
thread), ActiveInterruptibleTransaction captures a tokio runtime handle
at construction and uses it unconditionally in Drop. The
insert()/remove()/abort_all() paths that previously relied on Drop now
roll back explicitly.
As defense-in-depth against any other writer path that might leak a
transaction (e.g. user code that does BEGIN and returns early), the
write pool gets an after_release hook that runs ROLLBACK. The hook
treats "no transaction is active" as the expected benign case and
instructs the pool to discard the connection on any other ROLLBACK
failure, so a broken connection is not handed to the next caller.
The plugin's ExitRequested handler no longer short-circuits on
programmatic exits (Some(code)). Treating those the same as
user-initiated exits means a user-space app_handle.exit(N) — fatal-
error handler, auto-updater, Ctrl+C handler — triggers transaction and
database cleanup instead of tearing down plugin state with live
interruptible transactions still in the map. The original exit code is
preserved through the cleanup detour via ExitGuard. CLEANUP_STATE == 2
is used to recognize the ExitGuard-driven re-exit and let it through.
Adds a regression test that mirrors the reporter's MCVE plus a second
test covering the attached-database drop path. Both wrap the second
begin_interruptible_transaction in tokio::time::timeout so a regression
in the drop path fails CI fast instead of hanging.
Updates the README to document the auto-rollback-on-drop guarantee
(including early returns via ?).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 240eb77 commit 2751290
5 files changed
Lines changed: 322 additions & 60 deletions
File tree
- crates
- sqlx-sqlite-conn-mgr/src
- sqlx-sqlite-toolkit
- src
- tests
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
381 | 381 | | |
382 | 382 | | |
383 | 383 | | |
384 | | - | |
385 | | - | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
386 | 388 | | |
387 | 389 | | |
388 | 390 | | |
| |||
784 | 786 | | |
785 | 787 | | |
786 | 788 | | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
787 | 793 | | |
788 | 794 | | |
789 | 795 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
180 | 192 | | |
181 | 193 | | |
182 | 194 | | |
183 | 195 | | |
184 | 196 | | |
185 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
186 | 214 | | |
187 | 215 | | |
188 | 216 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
102 | 109 | | |
103 | 110 | | |
104 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
105 | 117 | | |
106 | 118 | | |
107 | 119 | | |
108 | 120 | | |
109 | 121 | | |
110 | 122 | | |
| 123 | + | |
111 | 124 | | |
112 | 125 | | |
113 | 126 | | |
| |||
230 | 243 | | |
231 | 244 | | |
232 | 245 | | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
233 | 250 | | |
234 | 251 | | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
244 | 302 | | |
245 | 303 | | |
246 | 304 | | |
| |||
288 | 346 | | |
289 | 347 | | |
290 | 348 | | |
291 | | - | |
292 | | - | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
293 | 353 | | |
294 | 354 | | |
295 | 355 | | |
296 | 356 | | |
297 | 357 | | |
298 | 358 | | |
299 | 359 | | |
300 | | - | |
301 | | - | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
302 | 364 | | |
303 | 365 | | |
304 | 366 | | |
| |||
308 | 370 | | |
309 | 371 | | |
310 | 372 | | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
315 | 382 | | |
316 | | - | |
| 383 | + | |
317 | 384 | | |
318 | 385 | | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
319 | 389 | | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | 390 | | |
325 | 391 | | |
326 | 392 | | |
327 | 393 | | |
328 | 394 | | |
329 | | - | |
330 | | - | |
| 395 | + | |
| 396 | + | |
331 | 397 | | |
332 | 398 | | |
333 | 399 | | |
334 | 400 | | |
335 | 401 | | |
336 | 402 | | |
337 | 403 | | |
338 | | - | |
339 | 404 | | |
340 | 405 | | |
341 | 406 | | |
| |||
344 | 409 | | |
345 | 410 | | |
346 | 411 | | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
358 | 416 | | |
359 | 417 | | |
360 | | - | |
361 | | - | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
362 | 433 | | |
363 | 434 | | |
364 | 435 | | |
| |||
0 commit comments