Skip to content

Commit 40efae9

Browse files
committed
Ignore broken pipe error from zbus
1 parent b4cc07c commit 40efae9

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

platforms/unix/src/adapter.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl AdapterImpl {
212212
is_window_focused: bool,
213213
root_window_bounds: WindowBounds,
214214
action_handler: Box<dyn ActionHandler + Send>,
215-
) -> Option<Self> {
215+
) -> Self {
216216
let tree = Tree::new(initial_state, is_window_focused);
217217
let id = NEXT_ADAPTER_ID.fetch_add(1, Ordering::SeqCst);
218218
let (messages, context) = {
@@ -222,11 +222,11 @@ impl AdapterImpl {
222222
app_context.push_adapter(id, &context);
223223
(messages, context)
224224
};
225-
Some(AdapterImpl {
225+
AdapterImpl {
226226
id,
227227
messages,
228228
context,
229-
})
229+
}
230230
}
231231

232232
pub(crate) async fn register_tree(&self) {
@@ -428,7 +428,7 @@ impl Drop for AdapterImpl {
428428
}
429429
}
430430

431-
pub(crate) type LazyAdapter = Pin<Arc<Lazy<Option<AdapterImpl>, Boxed<Option<AdapterImpl>>>>>;
431+
pub(crate) type LazyAdapter = Pin<Arc<Lazy<AdapterImpl, Boxed<AdapterImpl>>>>;
432432

433433
pub struct Adapter {
434434
r#impl: LazyAdapter,
@@ -474,23 +474,23 @@ impl Adapter {
474474
let mut bounds = self.root_window_bounds.lock().unwrap();
475475
*bounds = new_bounds;
476476
}
477-
if let Some(Some(r#impl)) = Lazy::try_get(&self.r#impl) {
477+
if let Some(r#impl) = Lazy::try_get(&self.r#impl) {
478478
r#impl.set_root_window_bounds(new_bounds);
479479
}
480480
}
481481

482482
/// If and only if the tree has been initialized, call the provided function
483483
/// and apply the resulting update.
484484
pub fn update_if_active(&self, update_factory: impl FnOnce() -> TreeUpdate) {
485-
if let Some(Some(r#impl)) = Lazy::try_get(&self.r#impl) {
485+
if let Some(r#impl) = Lazy::try_get(&self.r#impl) {
486486
r#impl.update(update_factory());
487487
}
488488
}
489489

490490
/// Update the tree state based on whether the window is focused.
491491
pub fn update_window_focus_state(&self, is_focused: bool) {
492492
self.is_window_focused.store(is_focused, Ordering::SeqCst);
493-
if let Some(Some(r#impl)) = Lazy::try_get(&self.r#impl) {
493+
if let Some(r#impl) = Lazy::try_get(&self.r#impl) {
494494
r#impl.update_window_focus_state(is_focused);
495495
}
496496
}

platforms/unix/src/atspi/bus.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use atspi::{
1515
Interface, InterfaceSet,
1616
};
1717
use serde::Serialize;
18-
use std::{collections::HashMap, env::var, sync::Weak};
18+
use std::{collections::HashMap, env::var, io, sync::Weak};
1919
use zbus::{
2020
names::{BusName, InterfaceName, MemberName, OwnedUniqueName},
2121
zvariant::{Str, Value},
@@ -123,12 +123,15 @@ impl Bus {
123123
Ok(())
124124
}
125125

126-
async fn register_interface<T>(&self, path: &str, interface: T) -> Result<()>
126+
async fn register_interface<T>(&self, path: &str, interface: T) -> Result<bool>
127127
where
128128
T: zbus::Interface,
129129
{
130-
self.conn.object_server().at(path, interface).await?;
131-
Ok(())
130+
map_or_ignoring_broken_pipe(
131+
self.conn.object_server().at(path, interface).await,
132+
false,
133+
|result| result,
134+
)
132135
}
133136

134137
pub(crate) async fn unregister_interfaces(
@@ -160,12 +163,15 @@ impl Bus {
160163
Ok(())
161164
}
162165

163-
async fn unregister_interface<T>(&self, path: &str) -> Result<()>
166+
async fn unregister_interface<T>(&self, path: &str) -> Result<bool>
164167
where
165168
T: zbus::Interface,
166169
{
167-
self.conn.object_server().remove::<T, _>(path).await?;
168-
Ok(())
170+
map_or_ignoring_broken_pipe(
171+
self.conn.object_server().remove::<T, _>(path).await,
172+
false,
173+
|result| result,
174+
)
169175
}
170176

171177
pub(crate) async fn emit_object_event(
@@ -338,14 +344,35 @@ impl Bus {
338344
signal_name: &str,
339345
body: EventBody<'_, T>,
340346
) -> Result<()> {
341-
self.conn
342-
.emit_signal(
343-
Option::<BusName>::None,
344-
target.path(),
345-
InterfaceName::from_str_unchecked(interface),
346-
MemberName::from_str_unchecked(signal_name),
347-
&body,
348-
)
349-
.await
347+
map_or_ignoring_broken_pipe(
348+
self.conn
349+
.emit_signal(
350+
Option::<BusName>::None,
351+
target.path(),
352+
InterfaceName::from_str_unchecked(interface),
353+
MemberName::from_str_unchecked(signal_name),
354+
&body,
355+
)
356+
.await,
357+
(),
358+
|_| (),
359+
)
360+
}
361+
}
362+
363+
pub(crate) fn map_or_ignoring_broken_pipe<T, U, F>(
364+
result: zbus::Result<T>,
365+
default: U,
366+
f: F,
367+
) -> zbus::Result<U>
368+
where
369+
F: FnOnce(T) -> U,
370+
{
371+
match result {
372+
Ok(result) => Ok(f(result)),
373+
Err(zbus::Error::InputOutput(error)) if error.kind() == io::ErrorKind::BrokenPipe => {
374+
Ok(default)
375+
}
376+
Err(error) => Err(error),
350377
}
351378
}

platforms/unix/src/atspi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ impl From<accesskit::Rect> for Rect {
3939
}
4040
}
4141

42-
pub(crate) use bus::Bus;
42+
pub(crate) use bus::*;
4343
pub(crate) use object_address::OwnedObjectAddress;
4444
pub(crate) use object_id::ObjectId;

platforms/unix/src/context.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use zbus::{Connection, Task};
1616

1717
use crate::{
1818
adapter::{LazyAdapter, Message},
19-
atspi::{interfaces::Event, Bus, OwnedObjectAddress},
19+
atspi::{interfaces::Event, map_or_ignoring_broken_pipe, Bus, OwnedObjectAddress},
2020
util::WindowBounds,
2121
};
2222

@@ -167,9 +167,10 @@ async fn listen(session_bus: Connection) -> zbus::Result<()> {
167167
select! {
168168
change = changes.next() => {
169169
atspi_bus = if let Some(change) = change {
170-
match change.get().await {
171-
Ok(true) => Bus::new(&session_bus).await.ok(),
172-
_ => None,
170+
if change.get().await? {
171+
map_or_ignoring_broken_pipe(Bus::new(&session_bus).await, None, Some)?
172+
} else {
173+
None
173174
}
174175
} else {
175176
None
@@ -182,16 +183,14 @@ async fn listen(session_bus: Connection) -> zbus::Result<()> {
182183
if let Some(activation_context) = ACTIVATION_CONTEXT.get() {
183184
let activation_context = activation_context.lock().await;
184185
for adapter in &activation_context.adapters {
185-
if let Some(adapter) = &*adapter.as_ref().await {
186-
adapter.register_tree().await;
187-
}
186+
adapter.as_ref().await.register_tree().await;
188187
}
189188
}
190189
}
191190
}
192191
message = messages.next() => {
193192
if let Some((message, atspi_bus)) = message.zip(atspi_bus.as_ref()) {
194-
let _ = process_adapter_message(atspi_bus, message).await;
193+
process_adapter_message(atspi_bus, message).await?;
195194
}
196195
}
197196
complete => return Ok(()),

0 commit comments

Comments
 (0)