From a2d736c03cebde18cd7cf4a24920c58d60e53c52 Mon Sep 17 00:00:00 2001 From: David Stern Date: Mon, 6 May 2024 18:13:14 -0400 Subject: [PATCH 1/3] Exit instead of panicking on X11 connection loss. --- src/platform_impl/linux/x11/mod.rs | 11 ++++++++++- src/platform_impl/linux/x11/window.rs | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 6a4708e7b5..1654205eae 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -17,7 +17,7 @@ use calloop::generic::Generic; use calloop::ping::Ping; use calloop::{EventLoop as Loop, Readiness}; use libc::{setlocale, LC_CTYPE}; -use tracing::warn; +use tracing::{error, warn}; use x11rb::connection::RequestConnection; use x11rb::errors::{ConnectError, ConnectionError, IdsExhausted, ReplyError}; @@ -670,6 +670,15 @@ impl ActiveEventLoop { self.xconn .select_xinput_events(self.root, ALL_MASTER_DEVICES, mask) + .map_err(|err| { + // Handle loss of connection to the X server by exiting instead of panicking. + if let X11Error::Connection(_) = err { + error!("Detected loss of connection to X server while reading window geometry; exiting application..."); + // Use exit code 1 to match the default Xlib I/O error handler. + std::process::exit(1); + } + err + }) .expect_then_ignore_error("Failed to update device event filter"); } diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index ea7c02354a..55d72c8e33 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -5,7 +5,7 @@ use std::path::Path; use std::sync::{Arc, Mutex, MutexGuard}; use std::{cmp, env}; -use tracing::{debug, info, warn}; +use tracing::{debug, error, info, warn}; use x11rb::connection::Connection; use x11rb::properties::{WmHints, WmSizeHints, WmSizeHintsSpecification}; use x11rb::protocol::shape::SK; @@ -1239,6 +1239,15 @@ impl UnownedWindow { // is BadWindow, and if the window handle is bad we have bigger problems. self.xconn .get_geometry(self.xwindow) + .map_err(|err| { + // Handle loss of connection to the X server by exiting instead of panicking. + if let X11Error::Connection(_) = err { + error!("Detected loss of connection to X server while reading window geometry; exiting application..."); + // Use exit code 1 to match the default Xlib I/O error handler. + std::process::exit(1); + } + err + }) .map(|geo| (geo.width.into(), geo.height.into())) .unwrap() } From ae121f50fb10b794593495c053e3d1fb534b2a00 Mon Sep 17 00:00:00 2001 From: David Stern Date: Mon, 6 May 2024 18:26:18 -0400 Subject: [PATCH 2/3] formatting --- src/platform_impl/linux/x11/mod.rs | 5 ++++- src/platform_impl/linux/x11/window.rs | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 1654205eae..5ecc0e6016 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -673,7 +673,10 @@ impl ActiveEventLoop { .map_err(|err| { // Handle loss of connection to the X server by exiting instead of panicking. if let X11Error::Connection(_) = err { - error!("Detected loss of connection to X server while reading window geometry; exiting application..."); + error!( + "Detected loss of connection to X server while reading window geometry; \ + exiting application..." + ); // Use exit code 1 to match the default Xlib I/O error handler. std::process::exit(1); } diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 55d72c8e33..a8691e9fe9 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -1242,7 +1242,10 @@ impl UnownedWindow { .map_err(|err| { // Handle loss of connection to the X server by exiting instead of panicking. if let X11Error::Connection(_) = err { - error!("Detected loss of connection to X server while reading window geometry; exiting application..."); + error!( + "Detected loss of connection to X server while reading window geometry; \ + exiting application..." + ); // Use exit code 1 to match the default Xlib I/O error handler. std::process::exit(1); } From 0fc76893c0def48f7f17a4fb50fe98d312abe1c6 Mon Sep 17 00:00:00 2001 From: David Stern Date: Mon, 6 May 2024 18:27:26 -0400 Subject: [PATCH 3/3] formatting 2, electric boogaloo --- src/platform_impl/linux/x11/mod.rs | 2 +- src/platform_impl/linux/x11/window.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 5ecc0e6016..523f4f03ca 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -675,7 +675,7 @@ impl ActiveEventLoop { if let X11Error::Connection(_) = err { error!( "Detected loss of connection to X server while reading window geometry; \ - exiting application..." + exiting application..." ); // Use exit code 1 to match the default Xlib I/O error handler. std::process::exit(1); diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index a8691e9fe9..fcd21c1e8e 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -1244,7 +1244,7 @@ impl UnownedWindow { if let X11Error::Connection(_) = err { error!( "Detected loss of connection to X server while reading window geometry; \ - exiting application..." + exiting application..." ); // Use exit code 1 to match the default Xlib I/O error handler. std::process::exit(1);