Skip to content

The core's tests no longer crash at exit: they wait for databases still closing - #197

Merged
pitis merged 2 commits into
mainfrom
fix/sqlcipher-exit-crash
Nov 14, 2026
Merged

pitis merged 2 commits into
mainfrom
fix/sqlcipher-exit-crash

Conversation

@pitis

@pitis pitis commented Nov 14, 2026

Copy link
Copy Markdown
Owner

What changed

New test-only module sig-core/src/test_exit.rs. Before the first encrypted database opens (ProjectionDb::open with a key, and actor::sqlite_options for presage's store), it registers an exit handler. At exit, the handler waits until sqlx's worker threads have ended, 5 s at most. Each worker ends once its connection has closed.

  • macOS: threads are listed with proc_pidinfo.
  • Linux: from /proc/self/task.
  • Elsewhere: no wait.

It's all #[cfg(test)], so the app's code doesn't change.

Why

cargo test -p sig-core sometimes ended with SIGSEGV after test result: ok. There are 22 crash reports, all with the same stack. sqlx closes a dropped connection on its worker thread after the drop returns, so the last tests' databases can still be closing when the binary exits. SQLCipher's exit handler then frees its private heap and providers while an encrypted database's close still uses them (sqlcipher_codec_ctx_free → sqlcipher_free).

Exit handlers run newest first, so a handler registered after SQLCipher's runs before it. One catch, found by a crash with the first version: SQLite runs SQLCipher's init (which registers its handler) after it already counts as initialized. Another test's first open can still be in it, so sqlite3_initialize() returning doesn't mean SQLCipher's handler exists yet. The module also calls sqlcipher_extra_init, which is idempotent and runs under SQLite's main mutex, so it waits for that init to finish (or does it itself) before registering.

How it was tested

Same binary, sig_core-… expire -q run 50 times in a row:

build crashed
before (main) 12 of 50
first version, sqlite3_initialize only 14 of 50 (the crash report showed SQLCipher's handler had run before ours)
this PR 0 of 100
  • Also: presage, group, sticker × 10 each, no crash; the full suite × 3, 537 passed, no crash. The full suite's wall time equals its "finished in" (14.3 s), so the wait doesn't hold up exit.
  • No new sig_core-*.ips reports since the fix.
  • clippy (--all-targets -D warnings) passes through the pre-commit hook.
  • Not run: the desktop crate's tests and the Swift tests. They don't compile this crate's cfg(test) code.

The app itself: when it quits, its connections are open and idle, not closing, so the race needs a close in progress at exit. Not seen in the app's crash reports.

Closes #120

sqlx closes a dropped connection on its worker thread after the drop returns, so the last tests'
databases could still be closing as the test binary exited, while SQLCipher's exit handler freed
what an encrypted one's close uses. A test-only exit handler, registered after SQLCipher's, runs
first and waits for sqlx's workers to end.
@pitis

pitis commented Nov 14, 2026

Copy link
Copy Markdown
Owner Author

Review (code-reviewer on git diff origin/main...HEAD): no Critical or Important findings. The reviewer checked against the sources:

  • the extern declarations match the C prototypes
  • PROC_PIDLISTTHREADS returns handles that PROC_PIDTHREADINFO accepts (XNU's cthread_self), and the buffer bounds hold
  • the init race is real: isInit = 1, then SQLITE_EXTRA_INIT outside the mutex
  • no deadlock against exit
  • every keyed open in sig-core goes through one of the two registration points

Suggestions:

  • wait_for_closing_databases read as if every open waits. Taken: renamed wait_at_exit_for_closing_databases.
  • The call order matters: sqlcipher_extra_init on an uninitialized SQLite deadlocks on SQLite's main mutex. Taken: a comment says so.
  • Use cfg(target_os = "macos"), since proc_pidinfo/proc_threadinfo are macOS-only in libc. Taken.
  • The desktop crate's test attach.rs opens a keyed Client and could hit the same race. This crate's cfg(test) code doesn't reach it. Out of scope here: the issue and the 22 crash reports are all the sig-core test binary. If the desktop binary ever crashes the same way, a test-util feature can export the wait.

Re-run after the changes: expire × 50, no crash. clippy passes through the pre-commit hook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The core's tests no longer crash at exit when SQLCipher shuts down under a closing connection

1 participant