Skip to content

Commit

Permalink
Exit instead of panicking on X11 connection loss. (rust-windowing#2)
Browse files Browse the repository at this point in the history
* Exit instead of panicking on X11 connection loss.

* formatting

* formatting 2, electric boogaloo
  • Loading branch information
vorporeal authored May 6, 2024
1 parent 4b3c065 commit 4cdd894
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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");
}

Expand Down
14 changes: 13 additions & 1 deletion src/platform_impl/linux/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 4cdd894

Please sign in to comment.