mirror of
https://github.com/storytold/craft-launcher.git
synced 2026-12-02 20:22:35 -05:00
fix(launcher): relaunch when the Wayland connection isn't ready yet (#1714)
* test(launcher): add failing-first regressions for the Wayland reconnect retry * fix(launcher): retry the window start when the Wayland connection isn't ready yet * fix(launcher): retry the same failure when it surfaces while the event loop is created * fix(launcher): relaunch the process instead of retrying the window start in place * fix(launcher): hand the Wayland retry to a new process and exit, so it can claim the instance port --------- Co-authored-by: Darul Qutni <[email protected]>
This commit is contained in:
1 parent
96e02dd233
commit
581a79961b
1 file changed
+175
@@ -92,6 +92,12 @@ fn main() -> ExitCode {
|
||||
artcraft_launcher_native::process::ensure_discord_detection_started();
|
||||
|
||||
let wake: Wake = Arc::new(OnceLock::new());
|
||||
// A relaunched Wayland retry waits before claiming the instance port: the process that
|
||||
// relaunched it is still exiting, and its claim is what frees the port (#1705). A normal
|
||||
// start carries no budget in the environment, so it never waits here.
|
||||
if std::env::var_os(WAYLAND_RETRIES_LEFT_ENV).is_some() {
|
||||
std::thread::sleep(WAYLAND_CONNECT_RETRY_DELAY);
|
||||
}
|
||||
let starts = match single_instance::claim(wake.clone()) {
|
||||
Instance::Secondary => {
|
||||
// No log file yet (a second start must not rotate the running one's), so say it on stderr.
|
||||
@@ -160,6 +166,7 @@ fn main() -> ExitCode {
|
||||
// (`StartupBackends`), so a graphics driver that fails the UI pipeline doesn't end the
|
||||
// launcher before its window appears.
|
||||
let mut backends = StartupBackends::new(eframe::wgpu::Backends::from_env().is_some());
|
||||
let mut wayland_retries = WaylandConnectRetries::from_env();
|
||||
// One pass per window: where a window can't hide, closing to the tray ends its pass and the
|
||||
// launcher carries on without one until the window is asked for again.
|
||||
loop {
|
||||
@@ -178,6 +185,26 @@ fn main() -> ExitCode {
|
||||
};
|
||||
waker.set_window(None);
|
||||
if let Err(e) = result {
|
||||
// The compositor may not be ready yet when a session restore starts the
|
||||
// launcher: relaunch a few times before falling back to the next backend
|
||||
// or reporting the failure (#1705). The failure can surface while the
|
||||
// event loop is created just as well as while the window is created. A
|
||||
// second `run_native` in this process can never work: winit lets a process
|
||||
// create one event loop only (`EventLoopBuilder::build` sets a flag that
|
||||
// is never cleared on native platforms), so the next attempt fails at once
|
||||
// with "EventLoop can't be recreated". The retry is therefore a fresh
|
||||
// process, with the budget left carried in the environment. This process
|
||||
// exits once the new one is started: the new process can only claim the
|
||||
// instance port once this one has freed it, so waiting for it here would
|
||||
// make the new process exit as a second instance instead.
|
||||
if matches!(e, eframe::Error::Winit(_) | eframe::Error::WinitEventLoop(_)) && wayland_retries.take(&e.to_string()) {
|
||||
let attempt = WAYLAND_CONNECT_RETRIES - wayland_retries.remaining();
|
||||
log::warn!("the Wayland connection wasn't ready yet ({e}); relaunching (attempt {attempt} of {WAYLAND_CONNECT_RETRIES})",);
|
||||
if relaunch_with_wayland_retries_left(wayland_retries.remaining()) {
|
||||
return ExitCode::SUCCESS;
|
||||
}
|
||||
// Relaunching failed: fall through and report the original failure.
|
||||
}
|
||||
if let Some(next) = backends.after_failure(&e) {
|
||||
log::warn!("the window didn't start: {e}; trying {next:?} instead");
|
||||
continue;
|
||||
@@ -185,6 +212,8 @@ fn main() -> ExitCode {
|
||||
report_startup_failure(attempt, &e, startup_language(saved_settings(settings_path.as_deref())));
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
// A window opened: a later start gets its own retry budget.
|
||||
wayland_retries = WaylandConnectRetries::new();
|
||||
if !std::mem::take(&mut launcher.closed_to_tray) {
|
||||
return ExitCode::SUCCESS;
|
||||
}
|
||||
@@ -309,6 +338,96 @@ impl StartupBackends {
|
||||
}
|
||||
}
|
||||
|
||||
/// How many times a window start that failed because the Wayland connection was not ready
|
||||
/// yet is retried, by relaunching the launcher.
|
||||
const WAYLAND_CONNECT_RETRIES: u8 = 3;
|
||||
|
||||
/// How long a relaunched Wayland retry waits before claiming the instance port, so the
|
||||
/// process that relaunched it has exited and freed the port.
|
||||
const WAYLAND_CONNECT_RETRY_DELAY: Duration = Duration::from_secs(1);
|
||||
|
||||
/// The environment a relaunched process carries its Wayland retry budget left in, so the
|
||||
/// next process knows how many attempts remain. It is the launcher's own bookkeeping, not
|
||||
/// a setting: nothing else reads it. Its presence is also how a process knows it is a
|
||||
/// retry, and so waits before claiming the instance port.
|
||||
const WAYLAND_RETRIES_LEFT_ENV: &str = "ARTCRAFT_LAUNCHER_WAYLAND_RETRIES_LEFT";
|
||||
|
||||
/// Relaunches this executable with the Wayland retry budget left. `true` when the new
|
||||
/// process was started, so the caller exits and frees the instance port for it; `false`
|
||||
/// when the executable cannot be relaunched at all, so the caller reports the original
|
||||
/// failure instead.
|
||||
fn relaunch_with_wayland_retries_left(retries_left: u8) -> bool {
|
||||
let Ok(exe) = std::env::current_exe() else { return false };
|
||||
std::process::Command::new(exe).args(std::env::args_os().skip(1)).env(WAYLAND_RETRIES_LEFT_ENV, retries_left.to_string()).spawn().is_ok()
|
||||
}
|
||||
|
||||
/// Retries a window start that failed because the Wayland connection was not ready yet.
|
||||
///
|
||||
/// When KDE restores a session it can start the launcher before the compositor is ready, and
|
||||
/// the first window start then fails with a Wayland "Broken pipe" (os error 32) before any
|
||||
/// window exists (#1705). The connection can break while the event loop is created just as
|
||||
/// well as while the window is created, so both surfaces earn the same retries. A few tries
|
||||
/// over a few seconds cover a session restore; anything else fails the same way on every
|
||||
/// backend, so only this failure earns another attempt, and the budget is bounded so a start
|
||||
/// that can never succeed still ends.
|
||||
///
|
||||
/// The retry is a fresh process, not another `run_native` in this one: winit lets a process
|
||||
/// create one event loop only, so a second start in the same process fails at once with
|
||||
/// "EventLoop can't be recreated" instead of connecting again. The budget left travels in
|
||||
/// the environment, so a relaunched process knows how many attempts remain. The process
|
||||
/// that relaunches exits once the new one is started, so the new process can claim the
|
||||
/// instance port; the new process waits a moment before claiming it, so the old one has
|
||||
/// exited and freed the port by then.
|
||||
struct WaylandConnectRetries {
|
||||
/// Attempts left, not counting the attempt that just failed.
|
||||
remaining: u8,
|
||||
}
|
||||
|
||||
impl WaylandConnectRetries {
|
||||
fn new() -> Self {
|
||||
Self { remaining: WAYLAND_CONNECT_RETRIES }
|
||||
}
|
||||
|
||||
/// The budget a process starts with: the full budget, unless a relaunched process
|
||||
/// carries fewer attempts left in the environment.
|
||||
fn from_env() -> Self {
|
||||
Self { remaining: wayland_retries_left_from_env(std::env::var(WAYLAND_RETRIES_LEFT_ENV).ok().as_deref()) }
|
||||
}
|
||||
|
||||
/// Attempts left, not counting the attempt that just failed.
|
||||
fn remaining(&self) -> u8 {
|
||||
self.remaining
|
||||
}
|
||||
|
||||
/// Whether the failed start earns another attempt: its message is the early Wayland
|
||||
/// connection failure and the budget is not spent. A granted attempt uses one up. The
|
||||
/// caller only passes a winit error's message here, from either the window failure or
|
||||
/// the event-loop failure; the variant is checked at the call site, where the error
|
||||
/// itself is in hand.
|
||||
fn take(&mut self, message: &str) -> bool {
|
||||
if self.remaining == 0 || !wayland_connection_failed_early(message) {
|
||||
return false;
|
||||
}
|
||||
self.remaining -= 1;
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// Retries left from the environment value a relaunched process carries: the number it
|
||||
/// names, clamped to the budget, or the full budget when it names nothing usable.
|
||||
fn wayland_retries_left_from_env(value: Option<&str>) -> u8 {
|
||||
value.and_then(|value| value.parse::<u8>().ok()).map_or(WAYLAND_CONNECT_RETRIES, |left| left.min(WAYLAND_CONNECT_RETRIES))
|
||||
}
|
||||
|
||||
/// Whether a failed window start's message is the Wayland connection failing before the
|
||||
/// first window exists: the compositor is not ready yet and the socket breaks
|
||||
/// ("Broken pipe", os error 32). The failure can surface while the event loop is created
|
||||
/// just as well as while the window is created, and the message is the same either way.
|
||||
/// winit's `OsError` keeps its cause private, so the message is the only thing to match on.
|
||||
fn wayland_connection_failed_early(message: &str) -> bool {
|
||||
message.contains("Broken pipe")
|
||||
}
|
||||
|
||||
/// The settings the launcher saved, for the places that need them without a window having loaded
|
||||
/// them (a tray start, and a start that never got a window at all).
|
||||
fn saved_settings(path: Option<&Path>) -> Option<Settings> {
|
||||
@@ -1594,6 +1713,62 @@ mod tests {
|
||||
assert_eq!(backends.after_failure(&error), None);
|
||||
}
|
||||
|
||||
/// A window start that fails because the Wayland connection was not ready yet is
|
||||
/// retried, and the retries are bounded so a start that can never succeed still ends (#1705).
|
||||
#[test]
|
||||
fn a_wayland_connection_that_was_not_ready_yet_is_retried() {
|
||||
// The exact error a session restore reports: KDE starts the launcher before the
|
||||
// compositor is ready, so the Wayland socket breaks before the first window exists.
|
||||
let broken_pipe = "winit error: os error at /home/runner/.cargo/git/checkouts/winit-0b02aa607dc0343f/a517b38/src/platform_impl/linux/wayland/window/mod.rs:206: Backend error: Io error: Broken pipe (os error 32)";
|
||||
let mut retries = WaylandConnectRetries::new();
|
||||
assert!(retries.take(broken_pipe));
|
||||
// The budget is bounded: once it is spent, the same failure earns nothing more.
|
||||
while retries.take(broken_pipe) {}
|
||||
assert!(!retries.take(broken_pipe));
|
||||
}
|
||||
|
||||
/// Other window failures are not retried as a Wayland reconnect, and a failure that
|
||||
/// earns nothing does not spend the budget.
|
||||
#[test]
|
||||
fn other_window_failures_are_not_retried_as_a_wayland_reconnect() {
|
||||
let mut retries = WaylandConnectRetries::new();
|
||||
for message in [
|
||||
"winit error: os error at /home/runner/.cargo/git/checkouts/winit-0b02aa607dc0343f/a517b38/src/platform_impl/linux/wayland/window/mod.rs:206: Backend error: Io error: Connection refused (os error 111)",
|
||||
"wgpu error: no surface formats available",
|
||||
"no window",
|
||||
"",
|
||||
] {
|
||||
assert!(!retries.take(message), "{message}");
|
||||
}
|
||||
// Nothing above spent the budget: the real failure still earns its retries.
|
||||
assert!(retries.take("Backend error: Io error: Broken pipe (os error 32)"));
|
||||
}
|
||||
|
||||
/// The same failure can surface while the event loop is created, before any window
|
||||
/// exists: the message is the same, so it earns the same retries (#1705).
|
||||
#[test]
|
||||
fn a_wayland_connection_that_breaks_while_the_event_loop_is_created_is_retried() {
|
||||
// The exact error a connection that breaks this early reports: the event loop
|
||||
// never gets created, so there is no window at all yet.
|
||||
let broken_pipe = "winit EventLoopError: os error at /home/runner/.cargo/git/checkouts/winit-0b02aa607dc0343f/a517b38/src/platform_impl/linux/wayland/event_loop/mod.rs:93: Backend error: Io error: Broken pipe (os error 32)";
|
||||
let mut retries = WaylandConnectRetries::new();
|
||||
assert!(retries.take(broken_pipe));
|
||||
}
|
||||
|
||||
/// A relaunched process reads the budget left from the environment: the number it
|
||||
/// names, clamped to the budget, or the full budget when it names nothing usable (#1705).
|
||||
#[test]
|
||||
fn a_relaunched_process_reads_its_wayland_retry_budget_from_the_environment() {
|
||||
assert_eq!(wayland_retries_left_from_env(None), WAYLAND_CONNECT_RETRIES);
|
||||
assert_eq!(wayland_retries_left_from_env(Some("2")), 2);
|
||||
assert_eq!(wayland_retries_left_from_env(Some("0")), 0);
|
||||
// More than the budget is still only the budget, and a value that is not a number
|
||||
// is the full budget rather than a start that never retries.
|
||||
assert_eq!(wayland_retries_left_from_env(Some("99")), WAYLAND_CONNECT_RETRIES);
|
||||
assert_eq!(wayland_retries_left_from_env(Some("abc")), WAYLAND_CONNECT_RETRIES);
|
||||
assert_eq!(wayland_retries_left_from_env(Some("")), WAYLAND_CONNECT_RETRIES);
|
||||
}
|
||||
|
||||
/// The fallback is OpenGL wherever it exists, never Vulkan (`wgpu_options` says why).
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user