You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Intermittent sqlite error after upgrade from 0.5.5 to 0.5.7 #1419
I am seeing an odd intermittent issue after upgrading from version 0.5.5. I have a number of queries using the sqlx::query function that run in sequence and after the upgrade I see the following error seemingly at random:
thread '<unnamed>' panicked at 'unable to close due to unfinalized statements or unfinished backups', .cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.7/src/sqlite/connection/handle.rs:68:17
If I downgrade back to version 0.5.5 I no longer experience this issue. I have the following features enabled: "runtime-tokio-native-tls", "sqlite", "migrate". Version of SQLite is 3.31.1-4ubuntu0.2 amd64, running on Ubuntu Focal.
For now running version 0.5.5 allows me to continue so it's not critical but I wanted to make sure an issue was opened. If any more details are required, or if I can help troubleshoot further please let me know what's needed and I'd be happy to help if possible.
I actually have no idea what's causing this. Obviously it's a ConnectionHandle getting dropped while some StatementHandle still exists, but I have no idea how.
The crossbeam-channel docs specify that even if all the senders have gone away (so the Sender in the StatementWorker struct getting dropped), the receiver should still be able to empty the channel of messages and thus work through all the remaining StatementHandles that might be queued.
I could see this panic happening if for some reason the thread we spawn in StatementWorker::new() dies prematurely, as that would leave StatementHandles in the channel, and then ConnectionHandle getting dropped before StatementWorker will trigger the panic.
However, the only way the statement worker thread would die first without consuming all the messages in the channel (barring a bug in crossbeam-channel) is by itself panicking; can you look back through your logs to find a panic with a different message?
I checked again to be sure and this is the only error I see at all unfortunately. I am going to try to recreate this in a stripped down project to make troubleshooting easier. I will update if I find anything out from that.
I'm still at a loss as to how this is happening, though. Any progress on that minimal reproduction? It would help to see how you're actually using SQLx.
Before I share this really strange looking example think I should share a bit of what led up to this. I started out with a sqlite pool limited to 1 connection but was running into issues, specifically I was seeing connection timeouts. This had me kind of stumped because it seemed no matter what I tried it would run into this connection error a crazy number of times.
So I modified things a bit more and ended up wrapping a SqliteConnection in a mutex and that in an arc, then would clone and wait for an owned lock, do the query, then drop that clone after it finished. This ended up working, though I did notice that after weeks of continuous operation I would see the connection timeout error a few times on some days. This program cycles every 30 seconds, so having an error that rarely isn't an issue for what we are doing.
The issue is the books: Vec<SqliteRow>, as SqliteRow retains a strong reference to the StatementHandle which finalizes the inner sqlite3_stmt on-drop.
Since you're prematurely dropping the SqliteConnection with drop(conn), it's trying to finalize the sqlite3 instance before it finalizes the Vec<SqliteRow> and thus the sqlite3_stmts contained therin (as the normal drop order in a function is LIFO).
As a temporary fix on your end, make sure to drop books before conn. With that change, the panic no longer happens in your example.
The permanent fix here on our end is probably to retain a ConnectionHandleRef in the SqliteRow struct so the connection cannot be finalized prematurely, or even just in StatementHandle itself, maybe.
thread '<unnamed>' panicked at 'unable to close due to unfinalized statements or unfinished backups', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.9/src/sqlite/connection/handle.rs:69:17
@abonander looks like I am still encountering this error after all. After your comment I had made some changes so I was surprised to see the error pop up again in the logs.
Quick question on the matter, would active_recs in this example also retain a strong reference to the StatementHandle as you mentioned before?
Just to note, after testing your refactor against two of the issues mentioned and seeing it fixed them, I went ahead and rolled it out to a couple deployments I was seeing lock errors on. So far not a single error, things are looking good!
Edit: Did end up with a couple lock errors so will review things again soon
Activity
abonander commentedon Sep 1, 2021
I actually have no idea what's causing this. Obviously it's a
ConnectionHandlegetting dropped while someStatementHandlestill exists, but I have no idea how.For one, we make sure to clear all the virtual statements in
SqliteConnection::drop(): https://github.com/launchbadge/sqlx/blob/master/sqlx-core/src/sqlite/connection/mod.rs#L105The comment reminded me that drop order is not defined, but either way it shouldn't be an issue:
If
handle: ConnectionHandleis dropped first, that should be fine, as that uses anArcand sends a strong reference to the statement worker thread to make sure it's dropped last: https://github.com/launchbadge/sqlx/blob/master/sqlx-core/src/sqlite/statement/worker.rs#L79The
crossbeam-channeldocs specify that even if all the senders have gone away (so theSenderin theStatementWorkerstruct getting dropped), the receiver should still be able to empty the channel of messages and thus work through all the remainingStatementHandles that might be queued.I could see this panic happening if for some reason the thread we spawn in
StatementWorker::new()dies prematurely, as that would leaveStatementHandles in the channel, and thenConnectionHandlegetting dropped beforeStatementWorkerwill trigger the panic.However, the only way the statement worker thread would die first without consuming all the messages in the channel (barring a bug in
crossbeam-channel) is by itself panicking; can you look back through your logs to find a panic with a different message?06chaynes commentedon Sep 1, 2021
I checked again to be sure and this is the only error I see at all unfortunately. I am going to try to recreate this in a stripped down project to make troubleshooting easier. I will update if I find anything out from that.
abonander commentedon Sep 1, 2021
@06chaynes the full panic backtrace may also help. It couldn't hurt, at least.
06chaynes commentedon Sep 1, 2021
abonander commentedon Sep 1, 2021
Frustratingly, that doesn't actually show where in non-stdlib code that the panic occurred. It looks like a bunch of inlining has happened here. Considering how shallow the stack is, though, I'm guessing that that is the worker thread panicking when it drops the
ConnectionHandleRef: https://github.com/launchbadge/sqlx/blob/master/sqlx-core/src/sqlite/statement/worker.rs#L79I'm still at a loss as to how this is happening, though. Any progress on that minimal reproduction? It would help to see how you're actually using SQLx.
06chaynes commentedon Sep 2, 2021
Before I share this really strange looking example think I should share a bit of what led up to this. I started out with a sqlite pool limited to 1 connection but was running into issues, specifically I was seeing connection timeouts. This had me kind of stumped because it seemed no matter what I tried it would run into this connection error a crazy number of times.
So I modified things a bit more and ended up wrapping a SqliteConnection in a mutex and that in an arc, then would clone and wait for an owned lock, do the query, then drop that clone after it finished. This ended up working, though I did notice that after weeks of continuous operation I would see the connection timeout error a few times on some days. This program cycles every 30 seconds, so having an error that rarely isn't an issue for what we are doing.
After moving past v0.5.5 though I started getting the new error. This example can recreate the error at least, https://github.com/06chaynes/sqlxerr, not quite what I'm doing but apparently close enough!
abonander commentedon Sep 2, 2021
With your example, I figured it out!
The issue is the
books: Vec<SqliteRow>, asSqliteRowretains a strong reference to theStatementHandlewhich finalizes the innersqlite3_stmton-drop.Since you're prematurely dropping the
SqliteConnectionwithdrop(conn), it's trying to finalize thesqlite3instance before it finalizes theVec<SqliteRow>and thus thesqlite3_stmts contained therin (as the normal drop order in a function is LIFO).As a temporary fix on your end, make sure to drop
booksbeforeconn. With that change, the panic no longer happens in your example.The permanent fix here on our end is probably to retain a
ConnectionHandleRefin theSqliteRowstruct so the connection cannot be finalized prematurely, or even just inStatementHandleitself, maybe.06chaynes commentedon Sep 2, 2021
Ah! I didn't consider that at all, thank you. I'll make those changes as soon as possible and test again.
chore(sqlite): add repro for #1419
fix(sqlite): hold a reference to the connection in `SqliteRow`
chore(sqlite): add repro for #1419
fix(sqlite): hold a reference to the connection in `SqliteRow`
chore(sqlite): add repro for #1419
fix(sqlite): hold a reference to the connection in `SqliteRow`
Fix a panic in the worker thread when dropping the connection while `…
definitelycarter commentedon Oct 27, 2021
I hit this error on version 0.5.9.
thread '<unnamed>' panicked at 'unable to close due to unfinalized statements or unfinished backups', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/sqlx-core-0.5.9/src/sqlite/connection/handle.rs:69:1706chaynes commentedon Oct 29, 2021
@definitelycarter do you have a code snippet that could reproduce the issue by chance?
Merge launchbadge/sqlx master into JetASAP/sqlx master (#12)
Update ws-support (#15)
06chaynes commentedon Nov 23, 2021
@abonander looks like I am still encountering this error after all. After your comment I had made some changes so I was surprised to see the error pop up again in the logs.
Quick question on the matter, would
active_recsin this example also retain a strong reference to theStatementHandleas you mentioned before?WatchModelhere is a struct that derivesFromRowabonander commentedon Nov 23, 2021
@06chaynes it shouldn't since the rows aren't kept alive past the connection.
I also have some major internal refactors on #1551 that also address this issue as well as some segfaults.
06chaynes commentedon Nov 23, 2021
That's what I was thinking but wanted to be sure.
Sounds great, really looking forward to these changes!
06chaynes commentedon Nov 24, 2021
Just to note, after testing your refactor against two of the issues mentioned and seeing it fixed them, I went ahead and rolled it out to a couple deployments I was seeing lock errors on. So far not a single error, things are looking good!
Edit: Did end up with a couple lock errors so will review things again soon