Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/system_tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,12 @@ namespace system_tray {
}

void run_tray_until_exit(const std::shared_ptr<safe::event_t<bool>> &shutdown_event) {
if (!tray_initialized_state().load()) {
BOOST_LOG(info) << "System tray is unavailable; continuing without it"sv;
shutdown_event->view();
return;
}

while (process_tray_events() == 0);
shutdown_event->raise(true);
}
Expand Down
3 changes: 3 additions & 0 deletions src/system_tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ namespace system_tray {
/**
* @brief Process tray events until exit and notify the application's workers.
*
* If tray initialization failed, wait for the application's shutdown event instead
* of stopping its workers. An initialized tray still requests shutdown when its loop exits.
*
* @param shutdown_event Event used to stop Sunshine's server threads.
*/
void run_tray_until_exit(const std::shared_ptr<safe::event_t<bool>> &shutdown_event);
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_system_tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cstdlib>
#include <filesystem>
#include <format>
#include <future>
#include <optional>
#include <string>
#include <thread>
Expand Down Expand Up @@ -670,6 +671,40 @@ TEST_F(SystemTrayTest, ShowsMacBrokerLicenseAndAvailabilityWithOptionalStartupNo
}
#endif

/**
* @brief Verify the tray fallback waits for shutdown using an explicitly owned worker thread.
*/
TEST_F(SystemTrayTest, KeepsWorkersRunningWhenTrayIsUnavailable) {
ASSERT_FALSE(system_tray::tray_initialized_for_testing());
auto shutdown_event = std::make_shared<safe::event_t<bool>>();
std::promise<void> tray_loop_exited;
auto tray_loop = tray_loop_exited.get_future();
std::jthread tray_worker([shutdown_event, &tray_loop_exited]() {
system_tray::run_tray_until_exit(shutdown_event);
tray_loop_exited.set_value();
});

EXPECT_EQ(tray_loop.wait_for(100ms), std::future_status::timeout);
EXPECT_FALSE(shutdown_event->view(0ms));

shutdown_event->raise(true);
EXPECT_EQ(tray_loop.wait_for(5s), std::future_status::ready);
tray_worker.join();
EXPECT_TRUE(shutdown_event->peek());
EXPECT_FALSE(system_tray::tray_initialized_for_testing());
}

TEST_F(SystemTrayTest, HonorsPendingShutdownWhenTrayIsUnavailable) {
ASSERT_FALSE(system_tray::tray_initialized_for_testing());
auto shutdown_event = std::make_shared<safe::event_t<bool>>();
shutdown_event->raise(true);

system_tray::run_tray_until_exit(shutdown_event);

EXPECT_TRUE(shutdown_event->peek());
EXPECT_FALSE(system_tray::tray_initialized_for_testing());
}

#ifndef _WIN32
TEST_F(SystemTrayTest, LifecycleMenuAndStateTransitions) {
if (const int result = initialize_tray(); result != 0) {
Expand Down
Loading