mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
Merge fix/ipc-relay-framing: keep component messages whole in the session IPC relay (#32)
This commit is contained in:
commit
8e874714ad
1 changed files with 80 additions and 4 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter};
|
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter};
|
||||||
use weft_ipc_types::AppdToCompositor;
|
use weft_ipc_types::AppdToCompositor;
|
||||||
|
|
||||||
use crate::Registry;
|
use crate::Registry;
|
||||||
|
|
@ -33,6 +33,11 @@ impl Drop for IpcRelay {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The longest message from a component, its line break included: the
|
||||||
|
/// runtime refuses longer ones, so a longer line ends the relay.
|
||||||
|
#[cfg(unix)]
|
||||||
|
const MAX_RELAY_FRAME: usize = 64 * 1024 + 2;
|
||||||
|
|
||||||
/// Listens on `socket_path` for the session's component and relays
|
/// Listens on `socket_path` for the session's component and relays
|
||||||
/// newline-delimited messages between it and the returned sender (towards
|
/// newline-delimited messages between it and the returned sender (towards
|
||||||
/// the component) and the broadcast channel (from the component).
|
/// the component) and the broadcast channel (from the component).
|
||||||
|
|
@ -65,14 +70,29 @@ pub(crate) fn spawn_ipc_relay(
|
||||||
let (reader, writer) = tokio::io::split(stream);
|
let (reader, writer) = tokio::io::split(stream);
|
||||||
let mut reader = BufReader::new(reader);
|
let mut reader = BufReader::new(reader);
|
||||||
let mut writer = BufWriter::new(writer);
|
let mut writer = BufWriter::new(writer);
|
||||||
|
// The frame being read lives outside the loop: `read_until` keeps
|
||||||
|
// what it has read when the other branch wins, so a message from the
|
||||||
|
// page never splits a message from the component.
|
||||||
|
let mut frame = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
let mut line = String::new();
|
let mut limited = (&mut reader).take((MAX_RELAY_FRAME + 1 - frame.len()) as u64);
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
n = reader.read_line(&mut line) => {
|
n = limited.read_until(b'\n', &mut frame) => {
|
||||||
match n {
|
match n {
|
||||||
Ok(0) | Err(_) => break,
|
Ok(0) | Err(_) => break,
|
||||||
|
Ok(_) if frame.len() > MAX_RELAY_FRAME => {
|
||||||
|
tracing::warn!(session_id, "IPC relay: message over the size limit");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// The rest of the message is still to come.
|
||||||
|
Ok(_) if !frame.ends_with(b"\n") => {}
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let payload = line.trim_end().to_owned();
|
let line = std::mem::take(&mut frame);
|
||||||
|
let Ok(text) = String::from_utf8(line) else {
|
||||||
|
tracing::warn!(session_id, "IPC relay: message is not UTF-8");
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
let payload = text.trim_end_matches(['\n', '\r']).to_owned();
|
||||||
let _ = broadcast.send(Response::IpcMessage { session_id, payload });
|
let _ = broadcast.send(Response::IpcMessage { session_id, payload });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -795,6 +815,62 @@ async fn drain_stderr(stderr: tokio::process::ChildStderr, session_id: u64, proc
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::SYSTEMD_SCOPE_ARGS;
|
use super::SYSTEMD_SCOPE_ARGS;
|
||||||
|
|
||||||
|
fn relay_socket(name: &str) -> std::path::PathBuf {
|
||||||
|
std::env::temp_dir().join(format!("weft-relay-{name}-{}.sock", std::process::id()))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn a_message_from_the_page_does_not_split_one_from_the_component() {
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
let path = relay_socket("split");
|
||||||
|
let (broadcast, mut messages) = tokio::sync::broadcast::channel(16);
|
||||||
|
let (to_component, mut relay) = super::spawn_ipc_relay(7, path.clone(), broadcast).unwrap();
|
||||||
|
let mut component = tokio::net::UnixStream::connect(&path).await.unwrap();
|
||||||
|
component.write_all(b"{\"half\":").await.unwrap();
|
||||||
|
// The relay starts reading the component's message, then the page's
|
||||||
|
// message wins the select.
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||||
|
to_component.send("from the page".to_owned()).await.unwrap();
|
||||||
|
let mut received = vec![0; 14];
|
||||||
|
component.read_exact(&mut received).await.unwrap();
|
||||||
|
assert_eq!(received, b"from the page\n");
|
||||||
|
component.write_all(b"\"whole\"}\n").await.unwrap();
|
||||||
|
let message = tokio::time::timeout(std::time::Duration::from_secs(5), messages.recv())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
matches!(&message, crate::ipc::Response::IpcMessage { session_id: 7, payload } if payload == "{\"half\":\"whole\"}"),
|
||||||
|
"{message:?}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// A message at the limit, ending in a carriage return and line
|
||||||
|
// feed, arrives whole.
|
||||||
|
let mut at_limit = vec![b'y'; super::MAX_RELAY_FRAME - 2];
|
||||||
|
at_limit.extend_from_slice(b"\r\n");
|
||||||
|
component.write_all(&at_limit).await.unwrap();
|
||||||
|
let message = tokio::time::timeout(std::time::Duration::from_secs(5), messages.recv())
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
assert!(
|
||||||
|
matches!(&message, crate::ipc::Response::IpcMessage { payload, .. } if payload.len() == super::MAX_RELAY_FRAME - 2),
|
||||||
|
"a message at the limit did not arrive whole"
|
||||||
|
);
|
||||||
|
|
||||||
|
// A line longer than any message ends the relay, even when its line
|
||||||
|
// break comes in the same read.
|
||||||
|
let mut over = vec![b'x'; super::MAX_RELAY_FRAME];
|
||||||
|
over.push(b'\n');
|
||||||
|
component.write_all(&over).await.unwrap();
|
||||||
|
tokio::time::timeout(std::time::Duration::from_secs(5), relay.ended())
|
||||||
|
.await
|
||||||
|
.expect("the relay did not end on an overlong line");
|
||||||
|
assert!(messages.try_recv().is_err());
|
||||||
|
drop(relay);
|
||||||
|
assert!(!path.exists());
|
||||||
|
}
|
||||||
|
|
||||||
/// systemd-run checks its options and their combinations before it
|
/// systemd-run checks its options and their combinations before it
|
||||||
/// connects to the service manager. Run against an empty runtime
|
/// connects to the service manager. Run against an empty runtime
|
||||||
/// directory and bus address, it must get as far as the connection
|
/// directory and bus address, it must get as far as the connection
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue