Skip to content

background_client: release the client reference under the mutex (fixes single-core shutdown crash) - #355

Merged
AlanGriffiths merged 2 commits into
canonical:core24from
fast1ine:fix-background-client-stop-race
Oct 6, 2026
Merged

AlanGriffiths merged 2 commits into
canonical:core24from
fast1ine:fix-background-client-stop-race

Conversation

@fast1ine

@fast1ine fast1ine commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #354

BackgroundClient::stop() took a strong reference to Self before locking the mutex and released it after unlocking. On a single-core system the client thread woken by Self::stop() preempts the main thread right after the unlock, drops its own reference and returns from the runner lambda, which calls wl_display_disconnect(). The main thread then becomes the last owner and ~Self() destroys Wayland objects against a disconnected display: SIGSEGV in libwayland-client (wl_map) on every SIGTERM/SIGINT shutdown.

Taking the mutex first and letting the strong reference die inside the locked scope guarantees that the client thread, which also takes the mutex before client.reset(), is always the last owner, so destruction happens on the client thread before the display is disconnected.

Testing: on an i.MX6 Solo (single Cortex-A9, armhf, Debian 13 host) every shutdown of the stock 24/stable and 24/candidate snaps crashes (exit 139). Forcing SCHED_BATCH on the frame threads, which removes the preemption described above, gives clean shutdowns every time, matching this analysis. I will re-test on the device with the edge build once this lands.

🤖 Generated with Claude Code

@fast1ine
fast1ine requested a review from a team as a code owner October 6, 2026 12:52
BackgroundClient::stop() took a strong reference to Self before locking
the mutex and released it after unlocking. On a single-core system the
client thread woken by Self::stop() preempts the main thread right after
the unlock, drops its own reference and returns from the runner lambda,
which calls wl_display_disconnect(). The main thread then becomes the
last owner and ~Self() destroys Wayland objects against a disconnected
display: SIGSEGV in libwayland-client on every SIGTERM/SIGINT shutdown.

Take the mutex first and let the strong reference die inside the locked
scope. The client thread also takes the mutex before client.reset(), so
it is always the last owner and destruction happens on the client thread
before the display is disconnected.

Fixes canonical#354

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@fast1ine
fast1ine force-pushed the fix-background-client-stop-race branch from c26fe98 to a5a6696 Compare October 6, 2026 13:05
Comment thread src/background_client.cpp
@AlanGriffiths
AlanGriffiths force-pushed the fix-background-client-stop-race branch from 2f045fd to 13f81c9 Compare October 6, 2026 14:13

@AlanGriffiths AlanGriffiths left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@AlanGriffiths

Copy link
Copy Markdown
Contributor

@fast1ine the failing builds for arm64 and armhf are not your problem: they can't succeed in a PR from a fork. (Not sure why they were set up to run.)

@AlanGriffiths
AlanGriffiths disabled auto-merge October 6, 2026 15:19
@AlanGriffiths
AlanGriffiths merged commit ec8a472 into canonical:core24 Oct 6, 2026
8 of 10 checks passed
@AlanGriffiths

Copy link
Copy Markdown
Contributor

@fast1ine the automation should publish the update on the beta channel as frame511-mir2.28.0 in the coming hours. Please let us know if you succeed in testing.

saviq-work pushed a commit that referenced this pull request Oct 7, 2026
…s single-core shutdown crash) (#355)

Fixes #354

`BackgroundClient::stop()` took a strong reference to `Self` before
locking the mutex and released it after unlocking. On a single-core
system the client thread woken by `Self::stop()` preempts the main
thread right after the unlock, drops its own reference and returns from
the runner lambda, which calls `wl_display_disconnect()`. The main
thread then becomes the last owner and `~Self()` destroys Wayland
objects against a disconnected display: SIGSEGV in libwayland-client
(`wl_map`) on every SIGTERM/SIGINT shutdown.

Taking the mutex first and letting the strong reference die inside the
locked scope guarantees that the client thread, which also takes the
mutex before `client.reset()`, is always the last owner, so destruction
happens on the client thread before the display is disconnected.

**Testing:** on an i.MX6 Solo (single Cortex-A9, armhf, Debian 13 host)
every shutdown of the stock 24/stable and 24/candidate snaps crashes
(exit 139). Forcing `SCHED_BATCH` on the frame threads, which removes
the preemption described above, gives clean shutdowns every time,
matching this analysis. I will re-test on the device with the edge build
once this lands.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
saviq-work pushed a commit that referenced this pull request Oct 7, 2026
…s single-core shutdown crash) (#355)

Fixes #354

`BackgroundClient::stop()` took a strong reference to `Self` before
locking the mutex and released it after unlocking. On a single-core
system the client thread woken by `Self::stop()` preempts the main
thread right after the unlock, drops its own reference and returns from
the runner lambda, which calls `wl_display_disconnect()`. The main
thread then becomes the last owner and `~Self()` destroys Wayland
objects against a disconnected display: SIGSEGV in libwayland-client
(`wl_map`) on every SIGTERM/SIGINT shutdown.

Taking the mutex first and letting the strong reference die inside the
locked scope guarantees that the client thread, which also takes the
mutex before `client.reset()`, is always the last owner, so destruction
happens on the client thread before the display is disconnected.

**Testing:** on an i.MX6 Solo (single Cortex-A9, armhf, Debian 13 host)
every shutdown of the stock 24/stable and 24/candidate snaps crashes
(exit 139). Forcing `SCHED_BATCH` on the frame threads, which removes
the preemption described above, gives clean shutdowns every time,
matching this analysis. I will re-test on the device with the edge build
once this lands.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
saviq-work pushed a commit that referenced this pull request Oct 7, 2026
…s single-core shutdown crash) (#355)

Fixes #354

`BackgroundClient::stop()` took a strong reference to `Self` before
locking the mutex and released it after unlocking. On a single-core
system the client thread woken by `Self::stop()` preempts the main
thread right after the unlock, drops its own reference and returns from
the runner lambda, which calls `wl_display_disconnect()`. The main
thread then becomes the last owner and `~Self()` destroys Wayland
objects against a disconnected display: SIGSEGV in libwayland-client
(`wl_map`) on every SIGTERM/SIGINT shutdown.

Taking the mutex first and letting the strong reference die inside the
locked scope guarantees that the client thread, which also takes the
mutex before `client.reset()`, is always the last owner, so destruction
happens on the client thread before the display is disconnected.

**Testing:** on an i.MX6 Solo (single Cortex-A9, armhf, Debian 13 host)
every shutdown of the stock 24/stable and 24/candidate snaps crashes
(exit 139). Forcing `SCHED_BATCH` on the frame threads, which removes
the preemption described above, gives clean shutdowns every time,
matching this analysis. I will re-test on the device with the edge build
once this lands.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
saviq-work pushed a commit that referenced this pull request Oct 7, 2026
…s single-core shutdown crash) (#355)

Fixes #354

`BackgroundClient::stop()` took a strong reference to `Self` before
locking the mutex and released it after unlocking. On a single-core
system the client thread woken by `Self::stop()` preempts the main
thread right after the unlock, drops its own reference and returns from
the runner lambda, which calls `wl_display_disconnect()`. The main
thread then becomes the last owner and `~Self()` destroys Wayland
objects against a disconnected display: SIGSEGV in libwayland-client
(`wl_map`) on every SIGTERM/SIGINT shutdown.

Taking the mutex first and letting the strong reference die inside the
locked scope guarantees that the client thread, which also takes the
mutex before `client.reset()`, is always the last owner, so destruction
happens on the client thread before the display is disconnected.

**Testing:** on an i.MX6 Solo (single Cortex-A9, armhf, Debian 13 host)
every shutdown of the stock 24/stable and 24/candidate snaps crashes
(exit 139). Forcing `SCHED_BATCH` on the frame threads, which removes
the preemption described above, gives clean shutdowns every time,
matching this analysis. I will re-test on the device with the edge build
once this lands.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
AlanGriffiths added a commit that referenced this pull request Oct 8, 2026
- **docs: update configuration options reference (#341)**
- **docs: intersphinx to `page` - that's the default**
- **docs: adapt standard release page format (#344)**
- **background_client: release the client reference under the mutex
(fixes single-core shutdown crash) (#355)**
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.

Shutdown crash (SIGSEGV in libwayland-client) on single-core systems: use-after-free in BackgroundClient::stop()

3 participants