mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
fix(compositor): buffer appd-bound frames and ignore stale session disconnects
A full socket buffer toward a stalled appd closed the connection and with it every app. Frames for appd are now queued whole and flushed on each send and read, and only a backlog beyond the frame limit or a write error ends the connection. A disconnect report is applied only when it comes from the session's current client, so a client closed with an earlier appd cannot remove the mapping of a new session that reused its ID. A failure to watch a new appd connection no longer leaves the compositor refusing every later one. weft-appd refuses a launch with 503 before anything starts while the compositor is not connected, and waits up to 10 s for the connection before restoring the previous session.
This commit is contained in:
parent
4c778330a3
commit
d6a2f94a1d
4 changed files with 142 additions and 34 deletions
|
|
@ -218,6 +218,14 @@ async fn run() -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(app_ids) = load_session() {
|
if let Some(app_ids) = load_session() {
|
||||||
|
// Apps need the compositor connection; give it a moment to come up.
|
||||||
|
let compositor = registry.lock().await.compositor_tx.clone();
|
||||||
|
if let Some(compositor) = compositor {
|
||||||
|
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(10);
|
||||||
|
while !compositor.is_connected() && tokio::time::Instant::now() < deadline {
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
tracing::info!(count = app_ids.len(), "restoring previous session");
|
tracing::info!(count = app_ids.len(), "restoring previous session");
|
||||||
for app_id in app_ids {
|
for app_id in app_ids {
|
||||||
let _ = dispatch(
|
let _ = dispatch(
|
||||||
|
|
@ -438,8 +446,20 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response {
|
||||||
// The package is resolved and its capabilities granted before a
|
// The package is resolved and its capabilities granted before a
|
||||||
// session exists; a package this host cannot satisfy is refused,
|
// session exists; a package this host cannot satisfy is refused,
|
||||||
// not started.
|
// not started.
|
||||||
|
let compositor_down = registry
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.compositor_tx
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|tx| !tx.is_connected());
|
||||||
let launch = if !weft_ipc_types::package::is_valid_app_id(&app_id) {
|
let launch = if !weft_ipc_types::package::is_valid_app_id(&app_id) {
|
||||||
Err(grants::Refusal::new(400, "invalid app ID"))
|
Err(grants::Refusal::new(400, "invalid app ID"))
|
||||||
|
} else if compositor_down {
|
||||||
|
// An app started now could never show a window.
|
||||||
|
Err(grants::Refusal::new(
|
||||||
|
503,
|
||||||
|
"weft-compositor is not connected",
|
||||||
|
))
|
||||||
} else if std::env::var("WEFT_RUNTIME_BIN").is_ok() {
|
} else if std::env::var("WEFT_RUNTIME_BIN").is_ok() {
|
||||||
resolve_launch(app_id.clone()).await.map(Some)
|
resolve_launch(app_id.clone()).await.map(Some)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1242,7 +1262,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "current_thread")]
|
#[tokio::test(flavor = "current_thread")]
|
||||||
async fn without_a_connected_compositor_the_app_shell_is_not_started() {
|
async fn without_a_connected_compositor_nothing_is_started() {
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
let _env = env_lock().lock().await;
|
let _env = env_lock().lock().await;
|
||||||
use_test_runtime_dir();
|
use_test_runtime_dir();
|
||||||
|
|
@ -1283,8 +1303,7 @@ mod tests {
|
||||||
tx,
|
tx,
|
||||||
std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
|
std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
|
||||||
));
|
));
|
||||||
let mut rx = registry.lock().await.subscribe();
|
let refused = dispatch(
|
||||||
let _ = dispatch(
|
|
||||||
Request::LaunchApp {
|
Request::LaunchApp {
|
||||||
app_id: "org.example.nocomp".into(),
|
app_id: "org.example.nocomp".into(),
|
||||||
surface_id: 0,
|
surface_id: 0,
|
||||||
|
|
@ -1292,16 +1311,7 @@ mod tests {
|
||||||
®istry,
|
®istry,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
let stopped = tokio::time::timeout(std::time::Duration::from_secs(10), async {
|
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
|
||||||
while !matches!(
|
|
||||||
rx.recv().await,
|
|
||||||
Ok(Response::AppState {
|
|
||||||
state: AppStateKind::Stopped,
|
|
||||||
..
|
|
||||||
})
|
|
||||||
) {}
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
let started = std::fs::read_to_string(&log).unwrap_or_default();
|
let started = std::fs::read_to_string(&log).unwrap_or_default();
|
||||||
|
|
||||||
for (key, value) in prior {
|
for (key, value) in prior {
|
||||||
|
|
@ -1314,9 +1324,12 @@ mod tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let _ = std::fs::remove_dir_all(&dir);
|
let _ = std::fs::remove_dir_all(&dir);
|
||||||
assert!(stopped.is_ok(), "session did not stop");
|
assert!(
|
||||||
// Only the runtime started; no app shell, and nothing was queued.
|
matches!(refused, Response::Error { code: 503, .. }),
|
||||||
assert_eq!(started.lines().count(), 1, "{started}");
|
"{refused:?}"
|
||||||
|
);
|
||||||
|
// Nothing started and nothing was queued for the compositor.
|
||||||
|
assert_eq!(started, "");
|
||||||
assert!(compositor.try_recv().is_err());
|
assert!(compositor.try_recv().is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,10 @@ pub struct WeftAppdIpc {
|
||||||
/// Sessions whose first toplevel was reported to appd.
|
/// Sessions whose first toplevel was reported to appd.
|
||||||
reported: HashSet<u64>,
|
reported: HashSet<u64>,
|
||||||
/// Where clients report their session's disconnection.
|
/// Where clients report their session's disconnection.
|
||||||
disconnected: Option<channel::Sender<u64>>,
|
disconnected: Option<channel::Sender<(u64, ClientId)>>,
|
||||||
|
/// Bytes for appd that did not fit in the socket buffer, sent before
|
||||||
|
/// anything else so frames stay whole.
|
||||||
|
out_buf: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
|
@ -58,28 +61,59 @@ impl WeftAppdIpc {
|
||||||
sessions: HashMap::new(),
|
sessions: HashMap::new(),
|
||||||
reported: HashSet::new(),
|
reported: HashSet::new(),
|
||||||
disconnected: None,
|
disconnected: None,
|
||||||
|
out_buf: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send(&mut self, msg: &CompositorToAppd) {
|
pub fn send(&mut self, msg: &CompositorToAppd) {
|
||||||
use std::io::Write;
|
|
||||||
let Some(stream) = &mut self.write_stream else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
match frame_encode(msg) {
|
match frame_encode(msg) {
|
||||||
Ok(frame) => {
|
Ok(frame) => {
|
||||||
if stream.write_all(&frame).is_err() {
|
if self.write_stream.is_some() {
|
||||||
// A partly written frame breaks the stream's framing:
|
self.out_buf.extend_from_slice(&frame);
|
||||||
// end the connection, whose read side then cleans up.
|
self.flush();
|
||||||
tracing::warn!("compositor IPC write failed; closing the appd connection");
|
|
||||||
let _ = stream.shutdown(std::net::Shutdown::Both);
|
|
||||||
self.write_stream = None;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => tracing::warn!(?e, "failed to encode compositor IPC message"),
|
Err(e) => tracing::warn!(?e, "failed to encode compositor IPC message"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Writes what appd's socket accepts now; the rest waits for the next
|
||||||
|
/// send or read. A backlog appd never drains, or a write error, ends the
|
||||||
|
/// connection, whose read side then cleans up.
|
||||||
|
fn flush(&mut self) {
|
||||||
|
use std::io::Write;
|
||||||
|
let Some(stream) = &mut self.write_stream else {
|
||||||
|
self.out_buf.clear();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
while !self.out_buf.is_empty() {
|
||||||
|
match stream.write(&self.out_buf) {
|
||||||
|
Ok(0) => break,
|
||||||
|
Ok(n) => {
|
||||||
|
self.out_buf.drain(..n);
|
||||||
|
}
|
||||||
|
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
|
||||||
|
Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!(
|
||||||
|
?e,
|
||||||
|
"compositor IPC write failed; closing the appd connection"
|
||||||
|
);
|
||||||
|
let _ = stream.shutdown(std::net::Shutdown::Both);
|
||||||
|
self.write_stream = None;
|
||||||
|
self.out_buf.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.out_buf.len() > MAX_FRAME_LEN {
|
||||||
|
tracing::warn!("weft-appd is not reading compositor IPC; closing the connection");
|
||||||
|
let _ = stream.shutdown(std::net::Shutdown::Both);
|
||||||
|
self.write_stream = None;
|
||||||
|
self.out_buf.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Reports a session's first toplevel to appd.
|
/// Reports a session's first toplevel to appd.
|
||||||
pub fn surface_created(&mut self, session_id: u64) {
|
pub fn surface_created(&mut self, session_id: u64) {
|
||||||
if self.sessions.contains_key(&session_id) && self.reported.insert(session_id) {
|
if self.sessions.contains_key(&session_id) && self.reported.insert(session_id) {
|
||||||
|
|
@ -176,6 +210,7 @@ impl WeftAppdIpc {
|
||||||
fn disconnect(&mut self) -> Vec<ClientId> {
|
fn disconnect(&mut self) -> Vec<ClientId> {
|
||||||
self.connected = false;
|
self.connected = false;
|
||||||
self.write_stream = None;
|
self.write_stream = None;
|
||||||
|
self.out_buf.clear();
|
||||||
self.read_buf.clear();
|
self.read_buf.clear();
|
||||||
self.fds.clear();
|
self.fds.clear();
|
||||||
self.reported.clear();
|
self.reported.clear();
|
||||||
|
|
@ -293,15 +328,18 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> {
|
||||||
tracing::info!(path = %socket_path.display(), "compositor IPC socket open");
|
tracing::info!(path = %socket_path.display(), "compositor IPC socket open");
|
||||||
|
|
||||||
// Clients bound to a session report their disconnection here.
|
// Clients bound to a session report their disconnection here.
|
||||||
let (sender, receiver) = channel::channel::<u64>();
|
let (sender, receiver) = channel::channel::<(u64, ClientId)>();
|
||||||
if let Some(ipc) = state.appd_ipc.as_mut() {
|
if let Some(ipc) = state.appd_ipc.as_mut() {
|
||||||
ipc.disconnected = Some(sender);
|
ipc.disconnected = Some(sender);
|
||||||
}
|
}
|
||||||
state
|
state
|
||||||
.loop_handle
|
.loop_handle
|
||||||
.insert_source(receiver, |event, _, state| {
|
.insert_source(receiver, |event, _, state| {
|
||||||
if let channel::Event::Msg(session_id) = event
|
// Only the session's current client: a client closed when its
|
||||||
|
// appd went away may report after a new appd reused the ID.
|
||||||
|
if let channel::Event::Msg((session_id, client)) = event
|
||||||
&& let Some(ipc) = state.appd_ipc.as_mut()
|
&& let Some(ipc) = state.appd_ipc.as_mut()
|
||||||
|
&& ipc.sessions.get(&session_id) == Some(&client)
|
||||||
{
|
{
|
||||||
ipc.sessions.remove(&session_id);
|
ipc.sessions.remove(&session_id);
|
||||||
ipc.reported.remove(&session_id);
|
ipc.reported.remove(&session_id);
|
||||||
|
|
@ -343,7 +381,7 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> {
|
||||||
ipc.fds.clear();
|
ipc.fds.clear();
|
||||||
}
|
}
|
||||||
stream.set_nonblocking(true).ok();
|
stream.set_nonblocking(true).ok();
|
||||||
let _ = handle.insert_source(
|
let registered = handle.insert_source(
|
||||||
Generic::new(stream, Interest::READ, Mode::Edge),
|
Generic::new(stream, Interest::READ, Mode::Edge),
|
||||||
|_, stream, state| {
|
|_, stream, state| {
|
||||||
// Safety: calloop wraps the fd in NoIoDrop to prevent
|
// Safety: calloop wraps the fd in NoIoDrop to prevent
|
||||||
|
|
@ -356,6 +394,9 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> {
|
||||||
for msg in messages {
|
for msg in messages {
|
||||||
handle_message(state, msg);
|
handle_message(state, msg);
|
||||||
}
|
}
|
||||||
|
if let Some(ipc) = state.appd_ipc.as_mut() {
|
||||||
|
ipc.flush();
|
||||||
|
}
|
||||||
if eof {
|
if eof {
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
"weft-appd disconnected from compositor IPC; \
|
"weft-appd disconnected from compositor IPC; \
|
||||||
|
|
@ -378,6 +419,12 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
if let Err(e) = registered {
|
||||||
|
tracing::warn!(?e, "cannot watch the appd IPC connection");
|
||||||
|
if let Some(ipc) = state.appd_ipc.as_mut() {
|
||||||
|
ipc.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
|
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
|
||||||
Err(e) => tracing::warn!(?e, "accept error on compositor IPC socket"),
|
Err(e) => tracing::warn!(?e, "accept error on compositor IPC socket"),
|
||||||
|
|
@ -502,6 +549,54 @@ mod tests {
|
||||||
assert!(ipc.fds.is_empty());
|
assert!(ipc.fds.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_slow_appd_gets_whole_frames_later_instead_of_losing_its_apps() {
|
||||||
|
let (appd, compositor) = UnixStream::pair().unwrap();
|
||||||
|
compositor.set_nonblocking(true).unwrap();
|
||||||
|
let mut ipc = WeftAppdIpc::new(PathBuf::from("/nonexistent"));
|
||||||
|
ipc.write_stream = Some(compositor);
|
||||||
|
ipc.connected = true;
|
||||||
|
// More than the socket buffer holds while appd is not reading.
|
||||||
|
let count = 20_000u64;
|
||||||
|
for session_id in 0..count {
|
||||||
|
ipc.send(&CompositorToAppd::SurfaceReady { session_id });
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
ipc.write_stream.is_some(),
|
||||||
|
"a full buffer closed the connection"
|
||||||
|
);
|
||||||
|
assert!(!ipc.out_buf.is_empty());
|
||||||
|
|
||||||
|
// appd reads; every flush moves more, and every frame arrives whole.
|
||||||
|
appd.set_nonblocking(true).unwrap();
|
||||||
|
let mut received = Vec::new();
|
||||||
|
let mut buf = [0u8; 65536];
|
||||||
|
loop {
|
||||||
|
ipc.flush();
|
||||||
|
use std::io::Read;
|
||||||
|
match (&appd).read(&mut buf) {
|
||||||
|
Ok(n) => received.extend_from_slice(&buf[..n]),
|
||||||
|
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
|
||||||
|
if ipc.out_buf.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => panic!("{e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut frames = 0u64;
|
||||||
|
while !received.is_empty() {
|
||||||
|
let len = u32::from_le_bytes(received[..4].try_into().unwrap()) as usize;
|
||||||
|
let frame: CompositorToAppd = frame_decode(&received[..4 + len]).unwrap();
|
||||||
|
assert!(
|
||||||
|
matches!(frame, CompositorToAppd::SurfaceReady { session_id } if session_id == frames)
|
||||||
|
);
|
||||||
|
received.drain(..4 + len);
|
||||||
|
frames += 1;
|
||||||
|
}
|
||||||
|
assert_eq!(frames, count);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn an_undecodable_frame_ends_the_connection() {
|
fn an_undecodable_frame_ends_the_connection() {
|
||||||
let (appd, compositor) = UnixStream::pair().unwrap();
|
let (appd, compositor) = UnixStream::pair().unwrap();
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ pub struct SessionBinding {
|
||||||
pub session_id: u64,
|
pub session_id: u64,
|
||||||
pub app_id: String,
|
pub app_id: String,
|
||||||
/// Reports the session's disconnection to the event loop.
|
/// Reports the session's disconnection to the event loop.
|
||||||
pub disconnected: smithay::reexports::calloop::channel::Sender<u64>,
|
pub disconnected: smithay::reexports::calloop::channel::Sender<(u64, ClientId)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether `client` connected through the display socket rather than
|
/// Whether `client` connected through the display socket rather than
|
||||||
|
|
@ -77,9 +77,9 @@ pub fn outside_sessions(client: &Client) -> bool {
|
||||||
|
|
||||||
impl ClientData for WeftClientState {
|
impl ClientData for WeftClientState {
|
||||||
fn initialized(&self, _client_id: ClientId) {}
|
fn initialized(&self, _client_id: ClientId) {}
|
||||||
fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason) {
|
fn disconnected(&self, client_id: ClientId, _reason: DisconnectReason) {
|
||||||
if let Some(binding) = &self.session {
|
if let Some(binding) = &self.session {
|
||||||
let _ = binding.disconnected.send(binding.session_id);
|
let _ = binding.disconnected.send((binding.session_id, client_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF
|
||||||
|
|
||||||
## Wayland Surface Isolation
|
## Wayland Surface Isolation
|
||||||
|
|
||||||
Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused while appd is not connected to the compositor, rather than starting an app that could never show a window.
|
Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, before any process starts, rather than starting an app that could never show a window.
|
||||||
|
|
||||||
Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open.
|
Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue