From 4a53ab9d2860914a99b86fd775ccaa158820f8c3 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Thu, 8 Oct 2026 00:19:56 +0800 Subject: [PATCH] fix(desktop): capture GUI logs in the shared session log The desktop authorization refactor moved GUI launching out of gpservice, so GUI stderr no longer inherited the service log destination. Redirect it explicitly to GP_LOG_FILE and open both writers in append mode to preserve interleaved output and GUI restarts. --- crates/gpapi/src/process/desktop_launcher.rs | 30 ++++++++++++++-- crates/gpapi/src/process/gui_launcher.rs | 36 +++++++++++++++++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/crates/gpapi/src/process/desktop_launcher.rs b/crates/gpapi/src/process/desktop_launcher.rs index 0ddee1f..1a8268f 100644 --- a/crates/gpapi/src/process/desktop_launcher.rs +++ b/crates/gpapi/src/process/desktop_launcher.rs @@ -1,6 +1,7 @@ use std::{ collections::HashMap, - fs::File, + fs::{File, OpenOptions}, + path::Path, process::{ExitStatus, Stdio}, sync::Arc, }; @@ -53,7 +54,7 @@ impl<'a> DesktopLauncher<'a> { .stdin(Stdio::piped()) .stdout(Stdio::piped()); if let Some(path) = &self.log_file { - command.stderr(Stdio::from(File::create(path)?)); + command.stderr(Stdio::from(create_log_file(Path::new(path))?)); } let mut service = command.spawn().context("Failed to start the privileged service")?; let mut input = service.stdin.take().context("Service input pipe is unavailable")?; @@ -111,6 +112,12 @@ impl<'a> DesktopLauncher<'a> { } } +fn create_log_file(path: &Path) -> std::io::Result { + // Start a fresh desktop session, then let both processes append without overwriting each other. + File::create(path)?; + OpenOptions::new().append(true).open(path) +} + async fn read_credential(reader: &mut (impl AsyncRead + Unpin)) -> anyhow::Result> { let mut header = [0; 2]; if reader.read(&mut header[..1]).await? == 0 { @@ -130,8 +137,27 @@ async fn read_credential(reader: &mut (impl AsyncRead + Unpin)) -> anyhow::Resul #[cfg(test)] mod tests { + use std::io::Write; + use super::*; + #[test] + fn desktop_log_starts_fresh_and_service_writes_preserve_gui_logs() { + let dir = tempfile::TempDir::new().unwrap(); + let path = dir.path().join("desktop.log"); + std::fs::write(&path, "previous session\n").unwrap(); + let mut service = create_log_file(&path).unwrap(); + writeln!(service, "[gpservice] started").unwrap(); + let mut gui = OpenOptions::new().append(true).open(&path).unwrap(); + writeln!(gui, "[gpgui] initialized").unwrap(); + writeln!(service, "[gpservice] connected").unwrap(); + + assert_eq!( + std::fs::read_to_string(&path).unwrap(), + "[gpservice] started\n[gpgui] initialized\n[gpservice] connected\n" + ); + } + #[tokio::test] async fn credential_channel_distinguishes_clean_close_from_truncated_or_oversized_frames() { assert!(read_credential(&mut &[][..]).await.unwrap().is_none()); diff --git a/crates/gpapi/src/process/gui_launcher.rs b/crates/gpapi/src/process/gui_launcher.rs index b3f3282..dff0abb 100644 --- a/crates/gpapi/src/process/gui_launcher.rs +++ b/crates/gpapi/src/process/gui_launcher.rs @@ -1,11 +1,12 @@ use std::{ collections::HashMap, + fs::OpenOptions, path::{Path, PathBuf}, process::{ExitStatus, Stdio}, sync::Arc, }; -use anyhow::bail; +use anyhow::{Context, bail}; use common::binary_paths; use log::info; use tokio::{io::AsyncWriteExt, process::Command}; @@ -87,6 +88,18 @@ impl<'a> GuiLauncher<'a> { cmd.envs(envs); } + let log_file = match &self.envs { + Some(envs) => envs.get("GP_LOG_FILE").map(PathBuf::from), + None => std::env::var_os("GP_LOG_FILE").map(PathBuf::from), + }; + if let Some(path) = log_file { + let file = OpenOptions::new() + .append(true) + .open(&path) + .with_context(|| format!("Failed to open GUI log file: {}", path.display()))?; + cmd.stderr(Stdio::from(file)); + } + cmd.arg("--service-credential-on-stdin"); if self.minimized { @@ -184,6 +197,27 @@ mod tests { } } + #[tokio::test] + async fn captures_gui_logs_in_the_desktop_log_file_across_restarts() { + let dir = TempDir::new().unwrap(); + let log_file = dir.path().join("desktop.log"); + fs::write(&log_file, "[gpservice] started\n").unwrap(); + let program = dir.path().join("gui"); + fs::write(&program, "#!/bin/sh\nprintf '[gpgui] initialized\\n' >&2\n").unwrap(); + fs::set_permissions(&program, fs::Permissions::from_mode(0o755)).unwrap(); + let launcher = launcher(VERSION, vec![]).envs(HashMap::from([( + "GP_LOG_FILE".to_owned(), + log_file.to_str().unwrap().to_owned(), + )])); + + assert!(launcher.launch_program(&program).await.unwrap().success()); + assert!(launcher.launch_program(&program).await.unwrap().success()); + assert_eq!( + fs::read_to_string(log_file).unwrap(), + "[gpservice] started\n[gpgui] initialized\n[gpgui] initialized\n" + ); + } + #[tokio::test] async fn prefers_the_first_compatible_program() { let dir = TempDir::new().unwrap();