Repository navigation
Fix the Secret Service session being reopened for every credential operation - #243
Merged
huacnlee merged 1 commit intoSep 20, 2026
Merged
Conversation
…eration Every credential get, put and delete built its own runtime, D-Bus connection and DH-encrypted session, then force-closed all three. That teardown races gnome-keyring's aes_negotiate handshake, and gnome-keyring 50.0 answers a client that vanished mid-negotiation by passing NULL into g_variant_new, so the daemon holding every credential on the machine aborts. The connection and session are now opened once for the process and kept. Measured with dbus-monitor against a real daemon, ten credential reads in one backend process cost ten OpenSession calls before and one after. A failed call drops the session so the next operation builds a fresh one, but only a read is repeated: a request already on the socket cannot be retracted, and repeating CreateItem would leave two items with the same attributes, after which find answers Ambiguous and the credential can no longer be read or deleted. A locked item or collection is an answer rather than a failed call, so it keeps the session instead of renegotiating one on every read. An evicted session is not closed, because operations share the connection and closing it would terminate another thread's in-flight call; the last reference closes it. Opening the session and running the operation share one deadline rather than each carrying their own.
huacnlee
approved these changes
Sep 20, 2026
huacnlee
left a comment
Owner
There was a problem hiding this comment.
Reviewed and approved; Security: PASS for the affected Secret Service credential scope.
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.
src/credentials/secret_service.rsbuilt a tokio runtime, a D-Bus connection and a DH-encrypted Secret Service session for every credential get, put and delete, then tore all three down at once:connection.close()under a 250 ms deadline, followed byruntime.shutdown_timeout(Duration::ZERO). The file's own comment conceded the shape of it, "This is close, never graceful_shutdown…".That teardown races the daemon's handshake. gnome-keyring 50.0 handles a client that vanishes mid-negotiation by passing NULL into
g_variant_new, which GLib treats as programmer error, so the daemon holding every credential on the machine aborts. It has done so 13 times on my machine since 15 September, always with the same four lines in the journal inside the same second:systemd restarts the daemon, but the login keyring comes back locked, so the visible result is mail that does not load. Omamail's own
errors.jsonrecordedcredentials.getfailing 13 times over those days, withmail.check,imap.list,imap.trash,imap.modifyandreader.openfailing behind it; at the abort on 17 September at 19:48:19, Omamail's calls failed at 19:48:22 and 19:48:23.The daemon is wrong to abort — a vanished client is a recoverable condition, and the same crash is reported against several other projects — but Omamail is the one provoking it, once per credential operation.
So the connection and the session are now opened once and kept.
Sharedholds a process-lifetime runtime and the session behind a mutex;run_ontakes it, runs the operation, and leaves it alone. Measured against the real daemon withdbus-monitor, ten credential reads inside one backend process:Five things were needed to make that safe rather than merely fewer sessions.
The runtime outlives the operation. One worker thread, so credential operations still run alongside each other as they did when each built its own runtime; a current-thread runtime would serialise every one of them behind whichever holds it, and a stuck prompt would then block them all for the whole deadline. I checked the obvious worry and it is not one: a session left idle for three seconds between operations is still usable, because a call is always awaited inside
block_on.A write is never repeated. The session is dropped and the operation retried when a call fails, because a session the daemon has forgotten fails like an absent one, and that is what makes a daemon restart invisible to a read. Only a read is repeated. A request already written to the socket cannot be retracted, and the daemon may have committed it, so repeating
CreateItem— called withreplace = false— would leave two items carrying identical attributes, after whichfindanswersAmbiguousand Omamail can no longer read or delete that credential. A sign-out would silently leave a live refresh token in the keyring.A locked keyring is an answer, not a broken connection.
Error::Unavailablewas doing double duty: a locked item, a locked default collection and a failedget_secretall produced it alongside genuine transport failures. Retrying on all of them would have renegotiated a session on every read against a keyring that was merely locked — the exact churn this change removes, in the one state where the daemon is least able to survive it. A privateFaultenum now separates an answer the daemon gave from a call that did not complete, and only the latter drops the session.An evicted session is not closed. Operations run on their own
spawn_blockingthreads and share one connection, so closing it on one thread's failure would terminate another thread's in-flight call — and that call would then look like a transport failure. Eviction now only removes it from the slot; the lastArcto be released closes it, once nobody is using it. This is also whySessionno longer holds a connection handle of its own:SecretServiceowns the connection it was built on.One deadline covers the whole operation. Opening the session and running it now share a single budget computed once, rather than each carrying its own
timeout(deadline, …). Without that, one credential operation could wait two deadlines on the happy path and four with a retry, which would have quietly eaten the headroom incalendar/mod.rs's 24 s CalDAV timeout.A poisoned mutex is also no longer fatal. The slot is written only after the connection is built, so it guards nothing a panic could break, and refusing it would have disabled every credential read for the life of the process.
shared()likewise no longer remembers a failed runtime build for ever.The timeout path still drops the session, so
credentials_native_linux_never_completing_prompt_releases_worker_and_connectionkeeps its guarantee that a stuck prompt leaves nothing registered on the bus. That now happens when a session has already failed rather than after every operation, so it no longer races the handshake of the session that follows it. A timeout is not retried: the deadline is the caller's bound and a second attempt would exceed it.Both pre-existing tests are unchanged and still pass.
run_withis kept for them as a test-only one-shot that disposes its session immediately, which is what they observe.This is the Omamail half of #239.
Verification
LC_ALL=C make validategreen on this commit (see the locale note below).cargo test --locked --features integration-test-credentialsgreen: 533 unit, 32 cli and the smaller suites, 0 failed. The Linux credential gate,cargo test --lib -- --ignored credentials_native_linux, passes 8/8 against a realdbus-daemonand a real keyring.I watched every new test fail against the unfixed code rather than assuming it would:
…_reuses_one_session_across_operationsleft: 3, right: 1with the session cache bypassed…_a_failed_write_reaches_the_daemon_onceCreateItemcalls where 1 is correct, with the retry allowed on writes…_a_locked_item_keeps_the_sessionleft: 3, right: 1with a locked item classed as a transport failure…_a_stalled_connect_costs_one_deadlineDriven by hand against the running daemon on Arch with gnome-keyring 1:50.0-1, using
dbus-monitorto countOpenSessionon the session bus while a backend process served tencredentials.getcalls for an account that does not exist. Five samples each: 10/10/10/10/10 before, 1/1/1/1/1 after. Starting the backend and sending nothing produces 0 on both, so every call counted came from the reads.One thing I could not explain, and would rather flag than bury. Under artificial full CPU saturation — a busy loop pinned to every core —
credentials_native_linux_never_completing_prompt_releases_worker_and_connectionfails on this branch and passes onmain, reproducibly: 0/5 against 5/5, and 5/5 on this branch once the load is removed. Instrumenting it showsconnectcompleting in 3.5 ms and theOpenSessionnegotiation not completing inside the test's 150 ms budget. I tried a current-thread runtime, a singleblock_oncovering negotiation and operation rather than two, and both combinations of those with and without the lock held across the negotiation; none of them changed it, so my explanations for it were wrong and I am not offering another. It does not reproduce at any load I can create short of saturating every core,make validateand the full gate are green, and production's budget is 5 s rather than 150 ms against a negotiation measured at roughly 50 ms. Still, it is a real difference frommainand somebody should look at it with fresher eyes than mine.A fresh agent with no context on the change reviewed the diff against
AGENTS.mdand found five faults, all fixed here: the repeated write, the misclassified locked keyring, the eviction that closed a connection other threads were still using, the poisoned-mutex lockout, and the doubled deadline. It demonstrated the first with its owndbus-daemonfixture, showing twoCreateItemcalls for onePutand a subsequentgetanddeleteboth answeringAmbiguous, and the fourth and fifth with runnable probes.Its security verdict on the revision it read was BLOCK, on that repeated write alone: no secret reaches argv, logs or a
Debugimpl, and sharing one session across accounts crosses no boundary that was not already crossed, but a credential that cannot be deleted cannot be revoked. It named three conditions to reach PASS — scope the retry toOperation::Get, stop classifying a locked item and a locked collection as transport failures, and add a write regression test that fails if the daemon sees a secondCreateItemfor one call. All three are met here. It has not re-reviewed the result, so that is my claim rather than its verdict.It also noted that the change lengthens the window in which a DH session key and an open authenticated session exist, from roughly one operation to the process lifetime. That is inherent to sharing a session rather than incidental, and it is written down here because it was written down nowhere else.
Two notes on my environment rather than on this change, both of which reproduce on unmodified
main.tests/test_publish.shfails underen_US.UTF-8: it compares asorted file list against an ASCII-ordered literal, andapp/CMakeLists.txtcollates beforeCargo.lockin a UTF-8 locale and after it underLC_ALL=C. Andmake validateneeds cmake fortest-app-qml, whichCONTRIBUTING.mdlists as a prerequisite formake app-buildbut not formake validate.No interface change, so no screenshots.
Release Notes