Skip to content

Commit e812889

Browse files
authored
Merge pull request #696 from JakeStanger/fix/upower-crash
fix(upower): avoid panic on client init error
2 parents 9d12535 + 474e1fe commit e812889

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

src/clients/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,17 @@ impl Clients {
164164
}
165165

166166
#[cfg(feature = "upower")]
167-
pub fn upower(&mut self) -> Arc<zbus::fdo::PropertiesProxy<'static>> {
168-
self.upower
169-
.get_or_insert_with(|| {
170-
crate::await_sync(async { upower::create_display_proxy().await })
171-
})
172-
.clone()
167+
pub fn upower(&mut self) -> ClientResult<zbus::fdo::PropertiesProxy<'static>> {
168+
let client = match &self.upower {
169+
Some(client) => client.clone(),
170+
None => {
171+
let client = await_sync(async { upower::create_display_proxy().await })?;
172+
self.upower.replace(client.clone());
173+
client
174+
}
175+
};
176+
177+
Ok(client)
173178
}
174179

175180
#[cfg(feature = "volume")]

src/clients/upower.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
use crate::register_client;
1+
use crate::clients::ClientResult;
2+
use crate::register_fallible_client;
23
use std::sync::Arc;
34
use upower_dbus::UPowerProxy;
45
use zbus::fdo::PropertiesProxy;
56

6-
pub async fn create_display_proxy() -> Arc<PropertiesProxy<'static>> {
7-
let dbus = Box::pin(zbus::Connection::system())
8-
.await
9-
.expect("failed to create connection to system bus");
7+
pub async fn create_display_proxy() -> ClientResult<PropertiesProxy<'static>> {
8+
let dbus = Box::pin(zbus::Connection::system()).await?;
109

11-
let device_proxy = UPowerProxy::new(&dbus)
12-
.await
13-
.expect("failed to create upower proxy");
10+
let device_proxy = UPowerProxy::new(&dbus).await?;
1411

15-
let display_device = device_proxy
16-
.get_display_device()
17-
.await
18-
.unwrap_or_else(|_| panic!("failed to get display device for {device_proxy:?}"));
12+
let display_device = device_proxy.get_display_device().await?;
1913

2014
let path = display_device.path().to_owned();
2115

@@ -26,10 +20,9 @@ pub async fn create_display_proxy() -> Arc<PropertiesProxy<'static>> {
2620
.expect("failed to set proxy path")
2721
.cache_properties(zbus::CacheProperties::No)
2822
.build()
29-
.await
30-
.expect("failed to build proxy");
23+
.await?;
3124

32-
Arc::new(proxy)
25+
Ok(Arc::new(proxy))
3326
}
3427

35-
register_client!(PropertiesProxy<'static>, upower);
28+
register_fallible_client!(PropertiesProxy<'static>, upower);

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ fn run_with_args() {
7878
#[cfg(feature = "schema")]
7979
if args.print_schema {
8080
let schema = schemars::schema_for!(Config);
81-
println!("{}", serde_json::to_string_pretty(&schema).expect("to be serializable"));
81+
println!(
82+
"{}",
83+
serde_json::to_string_pretty(&schema).expect("to be serializable")
84+
);
8285
return;
8386
}
8487

src/modules/upower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Module<gtk::Button> for UpowerModule {
7373
) -> Result<()> {
7474
let tx = context.tx.clone();
7575

76-
let display_proxy = context.client::<PropertiesProxy>();
76+
let display_proxy = context.try_client::<PropertiesProxy>()?;
7777

7878
spawn(async move {
7979
let mut prop_changed_stream = display_proxy.receive_properties_changed().await?;

0 commit comments

Comments
 (0)