Repository navigation
fix(daemon): restart when the session bus goes away - #343
Merged
Merged
Conversation
zackb
requested changes
Nov 23, 2026
zackb
left a comment
Owner
There was a problem hiding this comment.
Looks like a good fix for a real bug. Thanks for that! I have one small change requested regarding ensuring a closed fd is not written to. Unless I'm missing something, that fd would get written to every time under these circumstances.
| tether::SessionBusWatcher session_bus([session_bus_lost] { | ||
| g_exit_code = 1; | ||
| const uint64_t one = 1; | ||
| (void)!write(session_bus_lost, &one, sizeof(one)); |
Owner
There was a problem hiding this comment.
session_bus_lost is closed before session_bus destructs so it can write to a dead fd (use-after-close)
GNOME restarts the user's D-Bus at logout (gnome-session-restart-dbus.service). tetherd is a user service and keeps running if the user logs back in, but every session-bus user in it holds the connection it first opened: OBEX for messages and contacts, desktop popups, MPRIS, PipeWire telephony. Those calls then all fail with "The connection is closed" until tetherd is restarted by hand. With OBEX gone the iPhone drops the BR/EDR link, and the daemon's reconnect attempts (paging a phone that will not answer, ~30 s each) delay the ANCS replies past their timeout, so notifications stop as well. GLib's exit-on-close does not save it: the shared connection reports the close on the main context of the thread that opened it, which nothing here iterates. SessionBusWatcher keeps its own connection to the session bus on its own thread and main context. When the bus disconnects it, tetherd stops its loop and exits with status 1, so the unit's Restart=on-failure starts a fresh tetherd on the new bus. Without a session bus (headless) nothing is watched. Tested against a private dbus-daemon (GTestDBus) that is stopped under the watcher; skipped where dbus-daemon is not installed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
fernando2393
force-pushed
the
pr/session-bus-restart
branch
from
November 23, 2026 20:16
d05fcc0 to
65d4b28
Compare
Contributor
Author
|
Good catch, thanks. The watcher's thread outlived the close, and logout is exactly when the bus can die during shutdown. The fd is now owned by a small closer declared before the watcher, so it's closed only after the watcher has joined its thread; the manual |
This was referenced Nov 23, 2026
Closed
fernando2393
added a commit
to fernando2393/tether
that referenced
this pull request
Nov 25, 2026
…to compare it to an earlier session. The first session after the daemon starts has no earlier copy, and since zackb#343 the daemon restarts at every logout, so the same old entries still popped up once per login. When a session starts with an empty registry, the run of additions that opens it (the first within ReplayBurst::START of subscribing, then no gap longer than ReplayBurst::GAP) is marked pre-existing. Everything in that replay was already on the phone. A notification that lands inside the burst is still listed, just without a popup. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Problem
On GNOME, logging out and back in while
tetherdruns leaves it half-dead until it is restarted by hand: messages and contacts stop syncing, and soon notifications stop too.At logout GNOME restarts the user's D-Bus (
gnome-session-restart-dbus.service, which runsgnome-session-ctl --restart-dbus).tetherdis a user service, so it survives a quick re-login, but every session-bus user in it keeps the connection it first opened (OBEX for MAP/PBAP, desktop popups, MPRIS, PipeWire telephony). From then on:The connection is closed.Connection terminated by remote user, ~14 s after the bus restart here).BR/EDR connect failed (Timeout was reached)every ~30 s). Each page occupies the shared radio, so ANCS replies arrive just past their 15 s timeout and notifications stop.A manual restart fixes all of it at once (seen live: contacts and notifications back within 40 s).
GLib's
exit-on-closedoesn't help: the shared connection reports the close on the main context of the thread that opened it, and nothing intetherditerates that context, so no SIGTERM is ever raised.Change
SessionBusWatcher(core) keeps its own connection to the session bus, on its own thread and main context. When the bus disconnects it, the daemon logs why, stops its epoll loop (woken through an eventfd) and exits with status 1. The unit's existingRestart=on-failurethen starts a freshtetherdon the new bus. Without a session bus (headless) nothing is watched and nothing changes.Exiting and restarting covers every session-bus user in one place, rather than teaching each one to reconnect.
Testing
dbus-daemon(GTestDBus): stopping the bus under the watcher fires it exactly once; destroying the watcher first stays quiet; no bus means nothing is watched. Skipped wheredbus-daemonis not installed. Repeated 30× without a failure. Suite: 650 passing.Note
A related issue: a daemon started before the desktop session (boot, or right after a logout) starts with no display, so clipboard sync stays off. Locally I also made
tetherdpart ofgraphical-session.target, but I didn't propose that here because it would stoptetherdfrom ever starting on a headless machine.🤖 Generated with Claude Code