Skip to content

fix(daemon): restart when the session bus goes away - #343

Merged
zackb merged 1 commit into
zackb:mainfrom
fernando2393:pr/session-bus-restart
Nov 23, 2026
Merged

zackb merged 1 commit into
zackb:mainfrom
fernando2393:pr/session-bus-restart

Conversation

@fernando2393

Copy link
Copy Markdown
Contributor

Problem

On GNOME, logging out and back in while tetherd runs 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 runs gnome-session-ctl --restart-dbus). tetherd is 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:

  1. Every OBEX call fails with The connection is closed.
  2. With no profile in use, the iPhone drops the BR/EDR link (Connection terminated by remote user, ~14 s after the bus restart here).
  3. The bearer supervisor keeps paging the phone (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-close doesn't help: the shared connection reports the close on the main context of the thread that opened it, and nothing in tetherd iterates 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 existing Restart=on-failure then starts a fresh tetherd on 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

  • 3 new tests against a private 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 where dbus-daemon is not installed. Repeated 30× without a failure. Suite: 650 passing.
  • The failure was diagnosed from a live log on Ubuntu 26.04 / GNOME 50 with an iPhone. I didn't restart the user bus under the patched daemon on my desktop, since that tears down the whole session.

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 tetherd part of graphical-session.target, but I didn't propose that here because it would stop tetherd from ever starting on a headless machine.

🤖 Generated with Claude Code

@zackb zackb left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/daemon/main.cpp
tether::SessionBusWatcher session_bus([session_bus_lost] {
g_exit_code = 1;
const uint64_t one = 1;
(void)!write(session_bus_lost, &one, sizeof(one));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
fernando2393 force-pushed the pr/session-bus-restart branch from d05fcc0 to 65d4b28 Compare November 23, 2026 20:16
@fernando2393

Copy link
Copy Markdown
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 close() is gone. I also trimmed the comments in this PR.

@zackb
zackb merged commit 1588208 into zackb:main Nov 23, 2026
7 checks passed
@fernando2393
fernando2393 deleted the pr/session-bus-restart branch November 23, 2026 22:13
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]>
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.

2 participants