From 1222ff62ab3de948e2f67104f5594181706d7449 Mon Sep 17 00:00:00 2001 From: Conn O'Griofa Date: Mon, 28 Sep 2026 14:35:35 +0100 Subject: [PATCH] fix(linux/portalgrab): don't use TaskPool for xdg_worker thread. (#5822) --- src/main.cpp | 7 +---- src/platform/linux/misc.cpp | 43 ++++++++++++++++++++++++++----- src/platform/linux/misc.h | 3 +++ src/platform/linux/portalgrab.cpp | 17 +++++++++--- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index de45d2cbf..e6fee67b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -381,12 +381,7 @@ int main(int argc, char *argv[]) { #endif - int task_pool_threads = 1; -#ifdef SUNSHINE_BUILD_PORTAL - // Allocate an extra thread for fallback capture, otherwise a pending XDG user reply can block input. - task_pool_threads = 2; -#endif - task_pool.start(task_pool_threads); + task_pool.start(1); // Create signal handler after logging has been initialized auto shutdown_event = mail::man->event(mail::shutdown); diff --git a/src/platform/linux/misc.cpp b/src/platform/linux/misc.cpp index 0295b5ec3..85aa1b7a0 100644 --- a/src/platform/linux/misc.cpp +++ b/src/platform/linux/misc.cpp @@ -1270,12 +1270,21 @@ namespace platf { case no_token: BOOST_LOG(fatal) << "Portal capture is awaiting user permission. "sv << "The current session will attempt to use a fallback capture method."sv; - task_pool.push([]() { - if (!portal_display_names(false).empty()) { - platf::restart(); - } else { - BOOST_LOG(error) << "[portalgrab] Portal session token was not negotiated."sv; - } + std::call_once(portal::xdg_worker_flag, []() { + portal::xdg_worker = std::jthread([]() { + try { + platf::set_thread_name("xdg_worker"); + if (!portal_display_names(false).empty()) { + platf::restart(); + } else { + BOOST_LOG(error) << "[portalgrab] Portal session token was not negotiated."sv; + } + } catch (const std::exception &e) { + BOOST_LOG(error) << "[portalgrab] Exception caught in xdg_worker: "sv << e.what(); + } catch (...) { + BOOST_LOG(error) << "[portalgrab] Unknown exception caught in xdg_worker"sv; + } + }); }); return false; default: @@ -1489,6 +1498,28 @@ namespace platf { return nullptr; } +#ifdef SUNSHINE_BUILD_PORTAL + class deinit_t: public platf::deinit_t { + public: + /** + * @brief Handle xdg_worker thread cleanup in destructor. + */ + ~deinit_t() override { + try { + if (portal::xdg_worker.joinable()) { + // Make sure the worker's response loop sees shutdown before we block on join(). + if (mail::man) { + mail::man->event(mail::shutdown)->raise(true); + } + portal::xdg_worker.join(); + } + } catch (const std::exception &err) { + BOOST_LOG(error) << "[portalgrab] Exception while joining xdg_worker: "sv << err.what(); + } + } + }; +#endif + return std::make_unique(); } diff --git a/src/platform/linux/misc.h b/src/platform/linux/misc.h index 0754371c2..25e575c56 100644 --- a/src/platform/linux/misc.h +++ b/src/platform/linux/misc.h @@ -70,6 +70,9 @@ namespace portal { * @return True if the Portal is reachable. */ bool is_portal_service_reachable(); + + inline std::jthread xdg_worker; ///< XDG worker thread to negotiate restore token. + inline std::once_flag xdg_worker_flag; ///< Flag to ensure xdg_worker spawns only once. } // namespace portal #endif diff --git a/src/platform/linux/portalgrab.cpp b/src/platform/linux/portalgrab.cpp index 1608ab4ed..773ff5454 100644 --- a/src/platform/linux/portalgrab.cpp +++ b/src/platform/linux/portalgrab.cpp @@ -848,14 +848,22 @@ namespace portal { g_main_context_pop_thread_default(context); } + /** + * @brief Data for loop and shutdown_event mail used by check_shutdown_cb(). + */ + struct loop_context_t { + GMainLoop *loop; + std::shared_ptr> shutdown_event; + }; + /** * @brief Check for Sunshine shutdown and quit the Portal response loop if requested. * * @result True (continue) if shutdown event is not in progress. */ static gboolean check_shutdown_cb(gpointer user_data) { - if (auto shutdown_event = mail::man->event(mail::shutdown); shutdown_event->peek()) { - g_main_loop_quit(static_cast(user_data)); + if (auto *ctx = static_cast(user_data); ctx->shutdown_event && ctx->shutdown_event->peek()) { + g_main_loop_quit(ctx->loop); return G_SOURCE_REMOVE; } return G_SOURCE_CONTINUE; @@ -886,8 +894,11 @@ namespace portal { } constexpr guint shutdown_poll_interval_ms = 1000; + auto shutdown_event = mail::man ? mail::man->event(mail::shutdown) : nullptr; + loop_context_t ctx {response->loop, shutdown_event}; + GSource *shutdown_source = g_timeout_source_new(shutdown_poll_interval_ms); - g_source_set_callback(shutdown_source, check_shutdown_cb, response->loop, nullptr); + g_source_set_callback(shutdown_source, check_shutdown_cb, &ctx, nullptr); g_source_attach(shutdown_source, g_main_loop_get_context(response->loop)); g_main_loop_run(response->loop);