Repository navigation
The desktop tests no longer crash at exit with a core still running or a database still closing - #310
Merged
Merged
Conversation
…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.
Owner
Author
Review (
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-corefeaturetest-exit. It compilestest_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 normalshowssig-core []for the app and[test-exit]once dev-dependencies are in.Client::openrecords aWeakof each core (test builds only). The exit handler ends every core still alive, the same way dropping theClientdoes: it drops the command sender and joins the thread (moved intoClient::end_core, whichDropnow 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.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 onlyleft_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.test-exitin the desktop binary, so the core is still running) and passes with this PR (3 of 3, about 0.1 s).libc::atexitand_exit.make lintalso runscargo clippy -p hush-desktop, the app as it ships. Both--all-targetsruns include the desktop's dev-dependencies, which builds the core withtest-exit. Without this line, no part of the gate would compile the core the way the app gets it.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 inexit()with 51sig-corethreads and 160 sqlx workers still alive. The worker that crashed was insidesqlite3_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'scfg(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::stopreturns 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 Clientonly moved its body intoend_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 onexit, the 50 tests that open a core:sig-corethreadsCrash counts:
yes× 24sqlite3_close→sqlcipher_free, the same stack as the old reports)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::openand either drop them ormem::forgetthem 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 checkpasses.cargo fmt --check: passes.-D warnings(the two--all-targetsones 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.finished in, while its last databases close. The new message for a wait that gives up never appeared.Closes #308