From 4cdd894e32c829c107f655d72fa30ad80de303f6 Mon Sep 17 00:00:00 2001 From: David Stern Date: Mon, 6 May 2024 18:32:54 -0400 Subject: [PATCH] Exit instead of panicking on X11 connection loss. (#2) * Exit instead of panicking on X11 connection loss. * formatting * formatting 2, electric boogaloo --- src/platform_impl/linux/x11/mod.rs | 14 +++++++++++++- src/platform_impl/linux/x11/window.rs | 14 +++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 6a4708e7b5..523f4f03ca 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,18 @@ 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..fcd21c1e8e 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,18 @@ 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() }