Skip to content

The desktop tests no longer crash at exit with a core still running or a database still closing - #310

Merged
pitis merged 3 commits into
mainfrom
fix/test-exit-race
Nov 30, 2026
Merged

pitis merged 3 commits into
mainfrom
fix/test-exit-race

Conversation

@pitis

@pitis pitis commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

What changed

The desktop test binary now gets the exit handling the core's own tests got in #197, and that handling also ends any core still running at exit.

  • sig-core feature test-exit. It compiles test_exit (until now #[cfg(test)], so only in sig-core's own test binary) for crates that depend on the core. The desktop crate turns it on in [dev-dependencies], so only its test builds get it. cargo tree -e normal shows sig-core [] for the app and [test-exit] once dev-dependencies are in.
  • Cores still open at exit are ended first. Client::open records a Weak of each core (test builds only). The exit handler ends every core still alive, the same way dropping the Client does: it drops the command sender and joins the thread (moved into Client::end_core, which Drop now calls). The ended cores' connections that stay open are idle for good, so the handler doesn't wait for them. It still waits, 5 s at most, for the rest of sqlx's workers to finish closing.
  • A test for it: app::running::tests::a_core_left_open_is_ended_as_the_test_binary_exits. It runs the test binary again as a child, which runs only left_open, an ignored half that does nothing unless the child is started with its folder. The child registers an exit handler before any database opens, so that handler runs after every other one, SQLCipher's included. It then opens a core and leaks it. The handler fails the child if the core still holds its folder's lock by then.
    • It failed 3 of 3 with main's core code (no test-exit in the desktop binary, so the core is still running) and passes with this PR (3 of 3, about 0.1 s).
    • It is Unix-only, because it uses libc::atexit and _exit.
  • make lint also runs cargo clippy -p hush-desktop, the app as it ships. Both --all-targets runs include the desktop's dev-dependencies, which builds the core with test-exit. Without this line, no part of the gate would compile the core the way the app gets it.
  • The desktop tests no longer leak their cores. The four std::mem::forget(client.clone()) calls are removed (attach::with_core, running::with_core, backup.rs, view.rs). The desktop app pins, archives, mutes, blocks, deletes and exports chats, and answers requests #200 added them to work around the close race. Each core now stops and closes when its test's app drops it.

Why

The crash report from #305 (hush-8eca7f4bae954e64-2026-11-30-000122.ips) shows the desktop test binary in exit() with 51 sig-core threads and 160 sqlx workers still alive. The worker that crashed was inside sqlite3_step → sqlite3Codec → sqlcipher_shield: a query still running when SQLCipher's exit handler had already freed its memory.

The query came from cores the tests had leaked on purpose. Each running core sweeps disappearing messages every 5 s (EXPIRY_SWEEP). With ~50 cores that is about 10 queries a second, so sooner or later one runs during exit.

The older desktop crash reports (hush-afa4137…, hush-144084f…, hush-d99430e…) show the other case, the one #197 fixed for sig-core: a connection being closed (sqlite3_close → sqlcipher_codec_ctx_free) during exit. The desktop binary never had #197's wait, because a dependency's cfg(test) code isn't compiled into it (#197's PR said so).

Removing the leaks alone wasn't enough. GPUI's test teardown drops the tasks still queued, but not a task that is waiting on a waker held outside the app: the core's reply, a tokio timer of async-compat's runtime, or a woken task queued after the drain. A task like that keeps its Arc<Client>. On the full run, 8 cores were still alive at exit after the forgets were gone, mostly from the notifications tests, which vary from run to run. Those cores still run their sweeps, so the handler ends them.

The app

The app has a narrower form of the same race, and this PR doesn't fix it.

When the app quits, Client::stop returns once the core thread has ended. The thread's end drops presage's store, and its 1 to 3 connections then close on sqlx's workers after that, at the same time as the rest of quitting. The projection database's connection stays open and idle until exit, which is safe.

No crash of the app at quit shows up in the crash reports. A real fix needs presage's pool closed and waited for before the core thread ends. SqliteStore's pool is private to presage, so that needs a change to presage (our fork) and a bump of the pinned rev, which goes in its own PR. Production code here is unchanged: Drop for Client only moved its body into end_core.

How it was tested

A test inside the binary can't catch the crash itself: the new test checks the mechanism instead. The crash is measured by counting crashes over many runs under load (yes × 12 on 12 cores, plus the other agents' builds; load average 50 to 140).

Threads alive at exit: lldb, breakpoint on exit, the 50 tests that open a core:

build sig-core threads sqlx workers
main 29 94
this PR 0 0

Crash counts:

run main this PR
full desktop binary 0 of 20 0 of 20 (all 405 passed each time)
the 50 tests that open a core, yes × 24 0 of 100 (not rerun)
amplifier: 20 cores dropped as the binary exits 21 of 50 (SIGSEGV/SIGBUS, sqlite3_close → sqlcipher_free, the same stack as the old reports) 0 of 50
amplifier: 40 cores left running at exit 0 of 50 (150 cores: 0 of 10) 0 of 50

The real binary crashes too rarely to count in a reasonable time, so the comparison uses a scratch amplifier: two throwaway tests (not committed) that open cores through Client::open and either drop them or mem::forget them right before the binary exits.

  • The running-cores amplifier didn't crash on main either, so the query-at-exit case has no before/after count. The evidence for it is the crash report and the thread counts above. With this PR, 150 cores left running are ended at exit within 2.5 s.

  • The exit costs nothing extra when nothing leaks: the full binary's wall time matches its finished in (12.57 s vs 12.59 s). Before the cores were ended, the leaked cores' idle connections made the wait run its full 5 s.

  • Gate on the merged head (after make update, with The window tests pass at any hour and give the core a minute to answer, and a press on GNOME's title bar selects no text #309 and A reply or edit ends, as on Esc, when its chat can no longer be written in #306): make check passes.

    • cargo fmt --check: passes.
    • The three clippy runs with -D warnings (the two --all-targets ones and -p hush-desktop): pass.
    • cargo test --features hush-desktop/snapshot: desktop 406 passed and 1 ignored (the child half), sig-core 554 passed and 4 ignored.
    • sig-core's binary exits about 1 s after finished in, while its last databases close. The new message for a wait that gives up never appeared.

Closes #308

…still closing at exit

The desktop test binary never had the core's exit wait (a dependency's
cfg(test) code isn't compiled into it), so its tests leaked their cores
to dodge the close race; a leaked core's expiry sweep then ran a query
while SQLCipher's exit handler freed its memory. sig-core's test-exit
feature brings the wait to the desktop's tests, the wait now first ends
the cores still open (a GPUI test's task can keep one), and the leaks
are gone.
…its; make lint checks the app as it ships

The exit handler says when its wait gives up and reads its list of
cores even if a panic poisoned it. The comments say the feature is for
test builds only, and the stories test no longer gives the old crash as
its reason.
@pitis

pitis commented Nov 30, 2026

Copy link
Copy Markdown
Owner Author

Review (/pr-review-toolkit:review-pr, on git diff origin/main...HEAD)

Four reviewers ran on the diff: code, silent failures, comments and tests. None of them found a Critical issue. The fixes are in 99934ef, and make check passes on the merged head.

Important: all fixed

  1. The gate no longer compiled the core the way the app ships it (code). Every --all-targets clippy run and cargo test include the desktop's dev-dependencies, so the core was always built with test-exit. A compile error or warning that only shows up without the feature would first appear at publish time.
    • Fixed: make lint also runs cargo clippy -p hush-desktop -q -- -D warnings, with no --all-targets, so no dev-dependencies and no feature.
  2. Nothing guarded the mechanism (tests). The real binary crashed too rarely to notice, so dropping the dev-dependency line, or the record in Client::open, would go unseen.
    • Fixed: new test app::running::tests::a_core_left_open_is_ended_as_the_test_binary_exits. It reruns the binary as a child that leaks a core. An exit handler registered before any database opens runs last, and fails the child if the core still holds its folder lock.
    • It failed 3 of 3 with main's core code and passes 3 of 3 with this PR.
  3. The 5 s wait gave up silently (silent failures).
    • Fixed: it now writes sqlx's workers still run after 5 s: SQLCipher's exit may crash to stderr. It uses writeln! with its result ignored: eprintln! panics if the write fails, and a panic in an extern "C" handler aborts.
    • The message never appeared in any run.
  4. The test-exit feature comment didn't say "dev-dependencies only" (comments). The reason to say it: turned on as a normal dependency, the feature would ship in the app.
    • Fixed: the comments in both Cargo.tomls now say it is for test builds through [dev-dependencies] only, never the app.

Suggestions

  • Fixed:
    • A poisoned OPENED lock is now read through with PoisonError::into_inner, instead of being skipped.
    • The doc comments now say what end_at_exit and end_open_cores return, and the module doc names the expiry sweep. The handler's doc says it waits for the connections still closing.
    • The stories test's comment no longer gives the old crash at exit as its reason; the wait it describes stays.
    • backup.rs drops reopened before removing its folders.
  • Declined: a time limit on joining the cores at exit (code, silent failures, tests).
    • It is the same unbounded join Client::drop has always done.
    • The desktop's test cores are never linked, so no network wait or 15 s unlink can be under way.
    • A hang would show up as a stuck make check, not pass silently.
  • Declined: pool.size() can count a connection still closing, for example a task frozen inside db.close(). The window is a few milliseconds and no run showed it. I'd add code only if it ever shows up.
  • Declined: Windows waits for nothing. workers() returns 0 off macOS and Linux, and that predates this PR. The desktop tests don't run on Windows; the release workflow only builds there and checks the app starts.
  • Noted: the test and app builds now compile the core twice per worktree, once with the feature and once without, so they take more disk. That is the cost of keeping the feature out of the app.

For the owner (no change here)

The app has a narrower form of the race at quit. After Client::stop returns, presage's 1 to 3 connections are still closing on sqlx's workers. No crash at quit shows up in the reports.

Closing them properly needs presage's SqliteStore to expose a close: its pool is private to presage. That is a change to our presage fork and a bump of the pinned rev, which goes in its own PR. Worth a follow-up issue if you want it.

@pitis
pitis merged commit c164522 into main Nov 30, 2026
@pitis
pitis deleted the fix/test-exit-race branch November 30, 2026 01:17
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 don't crash at exit when a database query is still running

1 participant