fix(api): dynamically reload apps if modified on disk (#5506)

This commit is contained in:
Eduardo Mozart de Oliveira authored and GitHub committed 2026-09-29 17:41:26 -04:00
1 parent f309ef6577
commit 820700cbeb
4 files changed
+395 -2

No files matched your search

+39 -1
View File
@@ -415,6 +415,20 @@ namespace proc {
return _app.name;
}
void proc_t::update_apps_and_env(proc_t &&other) {
if (_app_id > 0 || placebo) {
// Preserve session-specific environment variables for the running app
for (const auto &var : _env) {
std::string name = var.get_name();
if (name.find("SUNSHINE_APP_") == 0 || name.find("SUNSHINE_CLIENT_") == 0) {
other._env[name] = var.to_string();
}
}
}
_env = std::move(other._env);
_apps = std::move(other._apps);
}
proc_t::~proc_t() {
// It's not safe to call terminate() here because our proc_t is a static variable
// that may be destroyed after the Boost loggers have been destroyed. Instead,
@@ -818,12 +832,36 @@ namespace proc {
/**
* @brief Refresh cached platform state from the operating system.
*
* This function compares the current last modified time of the file to the time it was last parsed.
* If the file has been modified since it was last parsed (or if it has never been parsed),
* it will be re-parsed to update the app list.
*
* An optional is used rather than a bare file_time_type because value-initialized
* file_time_type{} is not a reliable "never parsed" sentinel. On some toolchains
* (e.g. MSYS2 UCRT64 GCC 16.2), file_time_type::clock::now() compares
* less-or-equal to file_time_type{}, which would cause the initial parse to be skipped.
*
* When an app is currently running, only the cached app configuration list and environment
* are updated — active process state (_app_id, _process, _process_group, etc.) is preserved
* so that the in-flight streaming session can still be terminated properly.
*/
void refresh(const std::string &file_name) {
static std::optional<std::filesystem::file_time_type> last_apps_file_update; ///< Timestamp of the last successful apps.json parse, or nullopt if never parsed.
std::error_code ec;
auto current_time = std::filesystem::last_write_time(file_name, ec);
// Only skip parsing when we have a known-good timestamp and the file hasn't changed
if (last_apps_file_update && !ec && current_time == *last_apps_file_update) {
return;
}
auto proc_opt = proc::parse(file_name);
if (proc_opt) {
proc = std::move(*proc_opt);
proc.update_apps_and_env(std::move(*proc_opt));
last_apps_file_update = current_time;
}
}
} // namespace proc