fix(linux/portalgrab): don't use TaskPool for xdg_worker thread. (#5822)

This commit is contained in:
Conn O'Griofa authored and GitHub committed 2026-09-28 09:35:35 -04:00
1 parent 8fc40974c9
commit 1222ff62ab
4 files changed
+55 -15

No files matched your search

+1 -6
View File
@@ -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<bool>(mail::shutdown);
+37 -6
View File
@@ -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<bool>(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<deinit_t>();
}
+3
View File
@@ -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
+14 -3
View File
@@ -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<safe::event_t<bool>> 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<bool>(mail::shutdown); shutdown_event->peek()) {
g_main_loop_quit(static_cast<GMainLoop *>(user_data));
if (auto *ctx = static_cast<loop_context_t *>(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<bool>(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);