From 0d166babdc9c71b2665662ac668e45d558cb20df Mon Sep 17 00:00:00 2001 From: Petrica <24418935+PetricaT@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:06:05 +0200 Subject: [PATCH] fix: capture proton stderr and guard stale overlay stderr surfacing --- src/engine/deploy/launch/launcher.cpp | 8 ++ src/engine/deploy/launch/launcher.h | 4 + src/platform/linux/linux_platform.cpp | 13 ++- src/platform/macos/macos_platform.cpp | 10 +- src/platform/platform.h | 3 +- src/platform/windows/windows_platform.cpp | 26 +++-- src/runtime/runtime.cpp | 3 +- src/runtime/runtime.h | 7 ++ src/ui/controllers/launch_controller.cpp | 116 ++++++++++++++-------- 9 files changed, 134 insertions(+), 56 deletions(-) diff --git a/src/engine/deploy/launch/launcher.cpp b/src/engine/deploy/launch/launcher.cpp index 5070d232..cbe944d9 100644 --- a/src/engine/deploy/launch/launcher.cpp +++ b/src/engine/deploy/launch/launcher.cpp @@ -550,6 +550,14 @@ static LaunchResult do_launch(const LaunchParams ¶ms) { // while the OverlayFS path above honored it. auto proton = std::make_unique(params.platform); proton->set_runner_override(params.proton_runner); + // Capture the child's stderr (Proton failures) into the instance cache + // so a failed launch leaves a trace the watchdog can surface. + { + std::filesystem::path cache = params.cache_dir; + if (cache.empty()) + cache = capture_dir.parent_path() / "cache"; + proton->set_stderr_log_path(cache / ".gmm_proton_stderr.log"); + } runtime = std::move(proton); } if (!runtime) diff --git a/src/engine/deploy/launch/launcher.h b/src/engine/deploy/launch/launcher.h index f0c42efa..cde026aa 100644 --- a/src/engine/deploy/launch/launcher.h +++ b/src/engine/deploy/launch/launcher.h @@ -30,6 +30,10 @@ struct LaunchParams { // after the game exits. Empty = default Overwrite capture. std::filesystem::path output_capture_dir; + // Instance cache directory (for stderr capture files etc.). Empty = derive + // from capture_dir/overwrite_dir. + std::filesystem::path cache_dir; + // OverlayFS mod layer directories (priority order, first = highest). // When non-empty, the OverlayFsLauncher layers these over game_dir as // additional read-only overlay lowerdirs, enabling mod deployment without diff --git a/src/platform/linux/linux_platform.cpp b/src/platform/linux/linux_platform.cpp index 06b3d313..c98cab4d 100644 --- a/src/platform/linux/linux_platform.cpp +++ b/src/platform/linux/linux_platform.cpp @@ -1498,9 +1498,10 @@ const char *dlerror_message() { } std::int64_t spawn_detached_process(const std::filesystem::path &executable, - const std::vector &argv, - const std::filesystem::path &work_dir, - bool shell_fallback) { + const std::vector &argv, + const std::filesystem::path &work_dir, + bool shell_fallback, + const std::filesystem::path &stderr_log_path) { if (argv.empty() || executable.empty()) return -1; @@ -1525,6 +1526,12 @@ std::int64_t spawn_detached_process(const std::filesystem::path &executable, ::chdir(work_dir.c_str()); // stdin from the null device so the child never inherits our terminal. std::freopen("/dev/null", "r", stdin); + // Capture stderr to a file when requested (Proton launch path), so a + // failed launch leaves a trace instead of vanishing with the detached + // process. + if (!stderr_log_path.empty()) { + std::freopen(stderr_log_path.c_str(), "w", stderr); + } // execvp, not execv: a bare name is resolved through PATH the way the // native launcher always has, and a path containing a separator is exec'd // directly, which is what the Proton path needs. diff --git a/src/platform/macos/macos_platform.cpp b/src/platform/macos/macos_platform.cpp index ee88be28..3a116262 100644 --- a/src/platform/macos/macos_platform.cpp +++ b/src/platform/macos/macos_platform.cpp @@ -608,9 +608,10 @@ const char *dlerror_message() { } std::int64_t spawn_detached_process(const std::filesystem::path &executable, - const std::vector &argv, - const std::filesystem::path &work_dir, - bool shell_fallback) { + const std::vector &argv, + const std::filesystem::path &work_dir, + bool shell_fallback, + const std::filesystem::path &stderr_log_path) { if (argv.empty() || executable.empty()) return -1; @@ -631,6 +632,9 @@ std::int64_t spawn_detached_process(const std::filesystem::path &executable, if (!work_dir.empty()) ::chdir(work_dir.c_str()); std::freopen("/dev/null", "r", stdin); + if (!stderr_log_path.empty()) { + std::freopen(stderr_log_path.c_str(), "w", stderr); + } // execvp, not execv: a bare name resolves through PATH, a path containing // a separator is exec'd directly, which is what the Proton path needs. ::execvp(exe.c_str(), args.data()); diff --git a/src/platform/platform.h b/src/platform/platform.h index 7dae5718..32c45eac 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -390,7 +390,8 @@ void set_thread_low_priority(); [[nodiscard]] std::int64_t spawn_detached_process(const std::filesystem::path &executable, const std::vector &argv, - const std::filesystem::path &work_dir, bool shell_fallback); + const std::filesystem::path &work_dir, bool shell_fallback, + const std::filesystem::path &stderr_log_path = {}); // Set one variable in THIS process's environment, replacing whatever was there // (setenv(key, value, 1) / SetEnvironmentVariableW). Set per variable on diff --git a/src/platform/windows/windows_platform.cpp b/src/platform/windows/windows_platform.cpp index ee8c7188..a5d4a85c 100644 --- a/src/platform/windows/windows_platform.cpp +++ b/src/platform/windows/windows_platform.cpp @@ -779,9 +779,10 @@ void unload_shared_library(void *handle) noexcept { } std::int64_t spawn_detached_process(const std::filesystem::path &executable, - const std::vector &argv, - const std::filesystem::path &work_dir, - bool /*shell_fallback*/) { + const std::vector &argv, + const std::filesystem::path &work_dir, + bool /*shell_fallback*/, + const std::filesystem::path &stderr_log_path) { if (argv.empty() || executable.empty()) return -1; @@ -799,17 +800,30 @@ std::int64_t spawn_detached_process(const std::filesystem::path &executable, si.cb = sizeof(si); PROCESS_INFORMATION pi{}; + HANDLE stderr_handle = nullptr; + if (!stderr_log_path.empty()) { + stderr_handle = CreateFileW(stderr_log_path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, + nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + if (stderr_handle != INVALID_HANDLE_VALUE) { + si.hStdError = stderr_handle; + si.dwFlags |= STARTF_USESTDHANDLES; + } + } + const std::wstring wdir(work_dir.wstring()); const wchar_t *cwd = wdir.empty() ? nullptr : wdir.c_str(); // DETACHED_PROCESS so the game outlives us and does not inherit our console; // CREATE_NEW_PROCESS_GROUP so Ctrl-C in our terminal does not reach it. The // command line is mutable, so cmd.data() is handed over as-is. - if (!CreateProcessW(exe.c_str(), cmd.data(), nullptr, nullptr, FALSE, - DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, nullptr, cwd, &si, - &pi)) + if (!CreateProcessW(exe.c_str(), cmd.data(), nullptr, nullptr, TRUE, + DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, nullptr, cwd, &si, + &pi)) return -1; + if (stderr_handle && stderr_handle != INVALID_HANDLE_VALUE) + CloseHandle(stderr_handle); + CloseHandle(pi.hThread); const auto pid = static_cast(pi.dwProcessId); // Closed rather than kept: nothing here waits on the child, and holding it diff --git a/src/runtime/runtime.cpp b/src/runtime/runtime.cpp index bf0a5e54..b2927fff 100644 --- a/src/runtime/runtime.cpp +++ b/src/runtime/runtime.cpp @@ -101,7 +101,8 @@ bool ProtonRuntime::launch(const std::filesystem::path &executable, // No shell fallback: a missing or broken proton runner is a real failure the // caller must see as one, not something to paper over by re-running the // runner script through /bin/sh. - last_pid_ = spawn_detached_process(proton, argv, work_dir, /*shell_fallback=*/false); + last_pid_ = spawn_detached_process(proton, argv, work_dir, /*shell_fallback=*/false, + stderr_log_path_); return last_pid_ > 0; } diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 3b630b4e..063f3782 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -59,6 +59,12 @@ class ProtonRuntime : public Runtime { void set_runner_override(const std::string &runner) { runner_override_ = runner; } [[nodiscard]] const std::string &runner_override() const { return runner_override_; } + // Optional path for the child's stderr capture (Proton only). Empty = inherit. + void set_stderr_log_path(const std::filesystem::path &p) { stderr_log_path_ = p; } + [[nodiscard]] const std::filesystem::path &stderr_log_path() const { + return stderr_log_path_; + } + // Find the Proton binary for a game (respects per-game compat tool // override). Returns empty path if none found. All discovery goes through // `platform`. `runner_override`, when non-empty, wins over every @@ -85,6 +91,7 @@ class ProtonRuntime : public Runtime { private: const Platform *platform_; std::string runner_override_; + std::filesystem::path stderr_log_path_; int64_t last_pid_ = -1; }; diff --git a/src/ui/controllers/launch_controller.cpp b/src/ui/controllers/launch_controller.cpp index f487acc0..03689550 100644 --- a/src/ui/controllers/launch_controller.cpp +++ b/src/ui/controllers/launch_controller.cpp @@ -804,6 +804,7 @@ void LaunchController::on_launch_params_prepared(engine::LaunchParams lparams) { lparams.platform = w_->platform_; lparams.overwrite_dir = w_->overwrite_dir_path(); + lparams.cache_dir = w_->cache_dir_path(); // MO2-equivalent plugin order: build + write the game's Plugins.txt (and // the instance profile) right before launch. No-op for games without @@ -869,6 +870,7 @@ void LaunchController::on_launch_params_prepared(engine::LaunchParams lparams) { trace.end_stage("launch", true, "Overlay/staging paths ready"); trace.begin_stage("launch", "Launch executable"); + w_->launch_time_ = std::filesystem::file_time_type::clock::now(); auto lresult = engine::launch_game(lparams); if (lresult.pid <= 0) { @@ -896,7 +898,6 @@ void LaunchController::on_launch_params_prepared(engine::LaunchParams lparams) { w_->overlay_launched_ = lresult.overlay_launched; w_->running_process_pid_ = lresult.pid; w_->cgroup_path_ = lresult.cgroup_path; - w_->launch_time_ = std::filesystem::file_time_type::clock::now(); // P1.3 event bus: mirror MO2 onAboutToRun - emitted only once the launch // actually succeeded (a PID exists) so a failed launch is not reported. @@ -1011,26 +1012,43 @@ void LaunchController::check_running_process() { engine::Logger::instance().debug("Watchdog: cgroup empty, game fully exited"); int supervisor_exit = reap_supervisor(static_cast(w_->running_process_pid_)); - // Surface the real failure to the user-visible log. - { - std::string tail; - auto log_path = w_->cache_dir_path() / ".gmm_overlay_stderr.log"; - std::error_code lec; - if (std::filesystem::exists(log_path, lec)) { - std::ifstream f(log_path); - std::string content((std::istreambuf_iterator(f)), - std::istreambuf_iterator()); - if (content.size() > 500) - tail = content.substr(content.size() - 500); - else - tail = content; - for (char &c : tail) - if (c == '\n' || c == '\r') - c = ' '; - } - engine::Logger::instance().error("game exited code " + - std::to_string(supervisor_exit) + ": " + tail); - } + // Surface the real failure to the user-visible log. Read both the + // overlay and proton stderr captures; only files written this run + // (mtime >= launch start) are surfaced, so a stale traceback from a + // previous run can never masquerade as this run's error. Newest wins. + { + std::string tail; + std::filesystem::path chosen; + std::filesystem::file_time_type chosen_mtime{}; + for (const char *name : + {".gmm_overlay_stderr.log", ".gmm_proton_stderr.log"}) { + auto p = w_->cache_dir_path() / name; + std::error_code lec; + if (!std::filesystem::exists(p, lec)) + continue; + auto mt = std::filesystem::last_write_time(p, lec); + if (lec || mt < w_->launch_time_) + continue; + if (chosen.empty() || mt > chosen_mtime) { + chosen = p; + chosen_mtime = mt; + } + } + if (!chosen.empty()) { + std::ifstream f(chosen); + std::string content((std::istreambuf_iterator(f)), + std::istreambuf_iterator()); + if (content.size() > 500) + tail = content.substr(content.size() - 500); + else + tail = content; + for (char &c : tail) + if (c == '\n' || c == '\r') + c = ' '; + } + engine::Logger::instance().error("game exited code " + + std::to_string(supervisor_exit) + ": " + tail); + } w_->queue_->flush_pending_changes(); hide_game_lock_overlay(); trace.end_stage("launch", true, "Game exited"); @@ -1125,27 +1143,41 @@ void LaunchController::check_running_process() { w_->cgroup_path_.clear(); if (w_->process_watch_timer_) w_->process_watch_timer_->stop(); - // Surface the real failure to the user-visible log. - { - int code = WIFEXITED(reap_status) ? WEXITSTATUS(reap_status) : -1; - std::string tail; - auto log_path = w_->cache_dir_path() / ".gmm_overlay_stderr.log"; - std::error_code lec; - if (std::filesystem::exists(log_path, lec)) { - std::ifstream f(log_path); - std::string content((std::istreambuf_iterator(f)), - std::istreambuf_iterator()); - if (content.size() > 500) - tail = content.substr(content.size() - 500); - else - tail = content; - for (char &c : tail) - if (c == '\n' || c == '\r') - c = ' '; - } - engine::Logger::instance().error("game exited code " + std::to_string(code) + ": " + - tail); - } + // Surface the real failure to the user-visible log. + { + int code = WIFEXITED(reap_status) ? WEXITSTATUS(reap_status) : -1; + std::string tail; + std::filesystem::path chosen; + std::filesystem::file_time_type chosen_mtime{}; + for (const char *name : + {".gmm_overlay_stderr.log", ".gmm_proton_stderr.log"}) { + auto p = w_->cache_dir_path() / name; + std::error_code lec; + if (!std::filesystem::exists(p, lec)) + continue; + auto mt = std::filesystem::last_write_time(p, lec); + if (lec || mt < w_->launch_time_) + continue; + if (chosen.empty() || mt > chosen_mtime) { + chosen = p; + chosen_mtime = mt; + } + } + if (!chosen.empty()) { + std::ifstream f(chosen); + std::string content((std::istreambuf_iterator(f)), + std::istreambuf_iterator()); + if (content.size() > 500) + tail = content.substr(content.size() - 500); + else + tail = content; + for (char &c : tail) + if (c == '\n' || c == '\r') + c = ' '; + } + engine::Logger::instance().error("game exited code " + std::to_string(code) + ": " + + tail); + } // P1.3 event bus: mirror MO2 onFinishedRun (fallback PGID path). engine::EventBus::instance().dispatch( engine::events::kGameFinished,