Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 201 additions & 7 deletions crates/openshell-sandbox/src/linux/workload_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ impl WorkloadLauncher {
let (result_tx, result_rx) = mpsc::sync_channel(1);
self.jobs
.send(Box::new(move || {
let _ = result_tx.send(operation());
let mut reply = PanicReply(Some(result_tx));
let result = operation();
if let Some(tx) = reply.0.take() {
let _ = tx.send(Ok(result));
}
}))
.map_err(|_| io::Error::new(io::ErrorKind::BrokenPipe, "workload launcher stopped"))?;
result_rx.recv().map_err(|_| {
io::Error::new(
result_rx.recv().unwrap_or_else(|_| {
Err(io::Error::new(
io::ErrorKind::BrokenPipe,
"workload launcher dropped the spawn result",
)
"workload launcher stopped before running the job",
))
})
}

Expand Down Expand Up @@ -92,7 +96,47 @@ pub fn start() -> io::Result<(WorkloadLauncher, NotificationListener)> {
}
}
while let Ok(job) = jobs_rx.recv() {
job();
// Contain a job panic the way the network broker contains a
// handler panic. The launcher thread owns the inherited
// listener filter, and its state past a mid-job panic is not
// known-good, so stop accepting work: the health flag turns
// false and every later spawn fails fast instead of hanging
// on a launcher that reports healthy.
if let Err(panic) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(job)) {
// Flip the health flag before reporting the panic, so
// every later spawn fails fast even if the reporting
// below were to go wrong.
thread_alive.store(false, Ordering::Release);
tracing::error!("workload launcher job panicked: {}", panic_message(&*panic));
// A dead launcher means no new workload process can ever
// start in this sandbox, which operators must be able to
// see in structured logs, not only in tracing output.
openshell_ocsf::ocsf_emit!(
openshell_ocsf::DetectionFindingBuilder::new(
openshell_ocsf::ctx::ctx()
)
.activity(openshell_ocsf::ActivityId::Open)
.severity(openshell_ocsf::SeverityId::Medium)
.confidence(openshell_ocsf::ConfidenceId::High)
.is_alert(true)
.finding_info(
openshell_ocsf::FindingInfo::new(
"workload-launcher-unavailable",
"Workload Launcher Unavailable",
)
.with_desc(&format!(
"The workload launcher thread stopped after a \
job panicked ({}), so no new workload process \
can start in this sandbox. Every later spawn \
fails fast instead of hanging.",
panic_message(&*panic),
)),
)
.message("workload launcher job panicked; no new workload processes can start")
.build()
);
break;
Comment thread
FrostGod marked this conversation as resolved.
Resolved
}
}
thread_alive.store(false, Ordering::Release);
})
Expand All @@ -113,21 +157,53 @@ pub fn start() -> io::Result<(WorkloadLauncher, NotificationListener)> {
))
}

/// Render a caught panic payload for a log message.
fn panic_message(panic: &(dyn std::any::Any + Send)) -> String {
panic
.downcast_ref::<String>()
.map(String::as_str)
.or_else(|| panic.downcast_ref::<&'static str>().copied())
.unwrap_or("non-string panic payload")
.to_string()
}

/// Reply channel guard for one launcher job.
///
/// If the operation panics, the reply sender is dropped while unwinding. The
/// guard turns that into a precise error for the waiting caller instead of a
/// bare closed-channel recv, which cannot distinguish a panic from any other
/// teardown. The normal path takes the sender out first, so the guard sends
/// exactly once either way.
struct PanicReply<T>(Option<mpsc::SyncSender<io::Result<T>>>);

impl<T> Drop for PanicReply<T> {
fn drop(&mut self) {
if let Some(tx) = self.0.take() {
let _ = tx.send(Err(io::Error::new(
io::ErrorKind::BrokenPipe,
"workload launcher job panicked before returning a result",
)));
}
}
}

#[cfg(test)]
#[allow(unsafe_code)]
mod tests {
use std::mem::size_of;
use std::os::fd::AsRawFd as _;
use std::process::Command;

use super::*;
use tracing_subscriber::layer::SubscriberExt as _;

#[test]
fn one_listener_mediates_launcher_and_inherited_child() {
let (launcher, listener) = start().expect("start launcher");
let executable = std::env::current_exe().expect("test executable");
let mut child = launcher
.execute(move || {
let mut command = std::process::Command::new(executable);
let mut command = Command::new(executable);
command
.arg("--exact")
.arg("linux::workload_launcher::tests::inherited_listener_child")
Expand Down Expand Up @@ -173,6 +249,124 @@ mod tests {
assert!(listener.as_raw_fd() >= 0);
}

#[test]
fn panicking_job_marks_the_launcher_unhealthy() {
const HELPER_ENV: &str = "OPENSHELL_LAUNCHER_PANIC_TEST_HELPER";
if std::env::var_os(HELPER_ENV).is_none() {
// The panic hook's backtrace symbolization opens files on the
// launcher thread, whose user-notify filter traps opens. Nothing
// services that listener here, so run with backtraces off; the
// parent environment must not leak RUST_BACKTRACE=1 into the
// child.
let output = Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"linux::workload_launcher::tests::panicking_job_marks_the_launcher_unhealthy",
"--nocapture",
])
.env(HELPER_ENV, "1")
.env("RUST_BACKTRACE", "0")
.output()
.expect("start isolated launcher panic test");
assert!(
output.status.success(),
"launcher panic test failed: {}",
String::from_utf8_lossy(&output.stderr)
);
return;
}

let (launcher, _listener) = start().expect("start launcher");
assert!(launcher.is_alive());

// The launcher thread emits the structured finding, so capture needs a
// process-global subscriber; this helper process runs one test only.
let capture = openshell_ocsf::test_support::OcsfCapture::default();
tracing::subscriber::set_global_default(
tracing_subscriber::registry().with(capture.clone()),
)
.expect("install global capture subscriber");

let failure = launcher
.execute(|| panic!("launcher job failed"))
.expect_err("a panicking job must fail its caller, not hang it");
assert_eq!(failure.kind(), io::ErrorKind::BrokenPipe);
assert!(
failure.to_string().contains("panicked"),
"the panicked job's error must say the job panicked: {failure}"
);

// The health flag flips when the launcher thread leaves its job loop,
// after the caller has already observed the failure.
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
while launcher.is_alive() {
assert!(
std::time::Instant::now() < deadline,
"the launcher never reported the job panic"
);
thread::sleep(std::time::Duration::from_millis(10));
}

// The health flag flips before the finding is reported, so wait for
// the capture itself with the same bounded poll.
let events = {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
loop {
let events = capture.json_events();
if !events.is_empty() {
break events;
}
assert!(
std::time::Instant::now() < deadline,
"the launcher never reported its structured finding"
);
thread::sleep(std::time::Duration::from_millis(10));
}
};
assert_eq!(
events.len(),
1,
"the dead launcher must emit one structured finding: {events:?}"
);
assert_eq!(
events[0]["finding_info"]["uid"],
"workload-launcher-unavailable"
);
assert_eq!(events[0]["severity_id"], 3);
assert_eq!(events[0]["is_alert"].as_bool(), Some(true));
assert!(
events[0]["message"]
.as_str()
.is_some_and(|message| message.contains("panicked")),
"finding message: {events:?}"
);

let failure = launcher
.execute(|| ())
.expect_err("the launcher must refuse work after a job panic");
assert_eq!(failure.kind(), io::ErrorKind::BrokenPipe);
assert!(
failure.to_string().contains("not running"),
"unexpected refusal error: {failure}"
);
}

#[test]
fn panic_message_renders_every_payload_shape() {
#[derive(Debug)]
struct CustomPayload;

// A job that panics through a format argument carries a String payload.
let formatted = "job failed after 3 attempts".to_string();
assert_eq!(panic_message(&formatted), "job failed after 3 attempts");

// `panic!` with a literal carries a &'static str payload.
assert_eq!(panic_message(&"literal panic"), "literal panic");

// Any other payload type falls back to the placeholder.
assert_eq!(panic_message(&CustomPayload), "non-string panic payload");
}

#[test]
fn inherited_listener_child() {
if std::env::var_os("OPENSHELL_WORKLOAD_LAUNCHER_CHILD").is_none() {
Expand Down
Loading
Loading