From 74e079bd6efaeaca7e2105a95ff3f9b740fc5412 Mon Sep 17 00:00:00 2001 From: Yukino Song Date: Thu, 12 Sep 2024 05:50:16 +0800 Subject: [PATCH] Launch server cmd in a new thread to prevent deadlock after sleep commands --- src/stream.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/stream.cpp b/src/stream.cpp index d55cca66..47925653 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -996,23 +996,27 @@ namespace stream { }); server->map(packetTypes[IDX_EXEC_SERVER_CMD], [server](session_t *session, const std::string_view &payload) { - BOOST_LOG(debug) << "type [IDX_EXEC_SERVER_CMD]: "sv; + BOOST_LOG(debug) << "type [IDX_EXEC_SERVER_CMD]"sv; uint8_t cmdIndex = *(uint8_t*)payload.data(); if (cmdIndex < config::sunshine.server_cmds.size()) { const auto& cmd = config::sunshine.server_cmds[cmdIndex]; BOOST_LOG(info) << "Executing server command: " << cmd.cmd_name; - std::error_code ec; - auto env = proc::proc.get_env(); - boost::filesystem::path working_dir = proc::find_working_directory(cmd.cmd_val, env); - auto child = platf::run_command(cmd.elevated, true, cmd.cmd_val, working_dir, {}, nullptr, ec, nullptr); + auto exec_thread = std::thread([&cmd]{ + std::error_code ec; + auto env = proc::proc.get_env(); + boost::filesystem::path working_dir = proc::find_working_directory(cmd.cmd_val, env); + auto child = platf::run_command(cmd.elevated, true, cmd.cmd_val, working_dir, env, nullptr, ec, nullptr); - if (ec) { - BOOST_LOG(error) << "Failed to execute server command: " << ec.message(); - } else { - child.detach(); - } + if (ec) { + BOOST_LOG(error) << "Failed to execute server command: " << ec.message(); + } else { + child.detach(); + } + }); + + exec_thread.detach(); } else { BOOST_LOG(error) << "Invalid server command index: " << (int)cmdIndex; }