SQLCipher crashes on a segfault
I have found these related issues/pull requests
I don't see any issues mentioning any SQLCipher crashes in this repo, but did come across this related issue in the sqlcipher repo: https://github.com/sqlcipher/sqlcipher/issues/598
Description
In my testing, the sqlcipher test suite crashes about 8% of the time. I recently saw this while rebasing a PR of mine (#4343), and decided to have a robot look into it.
Here are all the failures exclusively caused by SQLCipher segfaults since 2026-07-01:
- https://github.com/transact-rs/sqlx/actions/runs/36413688549/job/108900510675
- https://github.com/transact-rs/sqlx/actions/runs/35104419529/job/104823079570
- https://github.com/transact-rs/sqlx/actions/runs/34564198418/job/103153392092
- https://github.com/transact-rs/sqlx/actions/runs/34534510553/job/103063488008
- https://github.com/transact-rs/sqlx/actions/runs/33692629721/job/100461951727
- https://github.com/transact-rs/sqlx/actions/runs/32526655032/job/96910377835
- https://github.com/transact-rs/sqlx/actions/runs/32071123060/job/95515306920
- https://github.com/transact-rs/sqlx/actions/runs/31418818610/job/93555336615
- https://github.com/transact-rs/sqlx/actions/runs/28627420274/job/84897256505
- https://github.com/transact-rs/sqlx/actions/runs/28499215476/job/84472614197
And a few more where there were additional errors, but SQLCipher also segfaulted:
- https://github.com/transact-rs/sqlx/actions/runs/36420553996/job/108922827779
- https://github.com/transact-rs/sqlx/actions/runs/33396839599/job/99503995707
- https://github.com/transact-rs/sqlx/actions/runs/32287758007/job/96181951993
- https://github.com/transact-rs/sqlx/actions/runs/31318702936/job/93258371439
- https://github.com/transact-rs/sqlx/actions/runs/30447397282/job/90561546169
It looks like this is because dropping the connection causes the worker thread to call sqlite3_close, which races SQLCipher's atexit handler which scribbles over its memory and frees its providers etc.
This problem does not replicate if the test suite calls close and then drops the connection.
I have a potential fix for this, but I did not author it by hand and I don't want to spam you. I can send it if you'd like me to.
Reproduction steps
Re-run sqlite-sqlcipher a few hundred times.
SQLx version
b54008a3 (head)
Enabled SQLx features
any, macros, migrate, sqlite, _unstable-all-types, runtime-tokio, sqlite-preupdate-hook
Database server and version
SQLite 3.50.4, SQLCipher 4.10.0
Operating system
Linux
Rust version
1.94.1, 1.98.1
Source: transact-rs/sqlx