Merge pull request 'fix(cuprate): catch launch panics and give the thread 8 MiB stack' (#127) from onionwire/cuprate-launch-panic into main
All checks were successful
ci / test (push) Successful in 5m16s
ci / sdk (push) Successful in 1m9s
android-ci / android (push) Successful in 10m46s
ci / fuzz (push) Successful in 5m1s

Reviewed-on: #127
This commit is contained in:
sirius 2026-09-19 19:47:40 +00:00
commit fb87903137

View file

@ -60,8 +60,8 @@ mod imp {
use std::path::Path;
use std::sync::mpsc;
use cuprated::config::{resolve_max_memory, Config};
use cuprated::Node;
use cuprated::config::{Config, resolve_max_memory};
/// The network this daemon runs. Testnet, deliberately — see the module
/// docs. Cuprate spells this `Testnet`.
@ -81,6 +81,41 @@ mod imp {
/// wallet child's RPC is the only client).
const RUNTIME_WORKERS: usize = 2;
/// Android's default pthread stack is ~1 MiB; Cuprate + RandomX overflow it
/// and the launch thread dies before `tx.send`, which the UI reports as
/// "the launch thread died before answering". Same size on the tokio
/// workers that actually run consensus.
const LAUNCH_STACK: usize = 8 * 1024 * 1024;
fn panic_message(payload: &(dyn std::any::Any + Send)) -> String {
if let Some(s) = payload.downcast_ref::<&str>() {
(*s).to_string()
} else if let Some(s) = payload.downcast_ref::<String>() {
s.clone()
} else {
"unknown panic".into()
}
}
fn run_on_launch_thread<F, T>(f: F) -> Result<T, String>
where
F: FnOnce() -> Result<T, String> + Send + 'static,
T: Send + 'static,
{
let (tx, rx) = mpsc::channel();
std::thread::Builder::new()
.name("onionwire-cuprate".into())
.stack_size(LAUNCH_STACK)
.spawn(move || {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f))
.unwrap_or_else(|p| Err(format!("cuprate panicked: {}", panic_message(&*p))));
let _ = tx.send(result);
})
.map_err(|e| format!("launch thread: {e}"))?;
rx.recv()
.map_err(|_| "the launch thread died before answering".to_string())?
}
/// A running in-process chain daemon.
///
/// Owning it *is* the lifecycle: `cuprated::Node` implements `Drop` and asks
@ -114,16 +149,7 @@ mod imp {
prune: bool,
) -> Result<Self, String> {
let text = config_toml(datadir, port, network, prune);
let (tx, rx) = mpsc::channel();
std::thread::Builder::new()
.name("onionwire-cuprate".into())
.spawn(move || {
let _ = tx.send(launch_on_owned_runtime(&text));
})
.map_err(|e| format!("launch thread: {e}"))?;
let (runtime, node) = rx
.recv()
.map_err(|_| "the launch thread died before answering".to_string())??;
let (runtime, node) = run_on_launch_thread(move || launch_on_owned_runtime(&text))?;
Ok(Self {
runtime: Some(runtime),
node: Some(node),
@ -181,6 +207,7 @@ mod imp {
resolve_max_memory(&mut config).map_err(|e| format!("config: {e}"))?;
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(RUNTIME_WORKERS)
.thread_stack_size(LAUNCH_STACK)
.enable_all()
.build()
.map_err(|e| format!("runtime: {e}"))?;
@ -272,6 +299,20 @@ mode = "Auto"
"the chain database is a separate store under the chain datadir"
);
}
#[test]
fn a_panic_on_the_launch_thread_is_returned_as_an_error() {
let err = run_on_launch_thread(|| -> Result<(), String> { panic!("boom") })
.expect_err("a panic is an error, not a dead channel");
assert!(
err.contains("boom"),
"the panic payload has to reach the caller, got {err:?}"
);
assert!(
!err.contains("died before answering"),
"that string is only for a thread that never sent"
);
}
}
}
@ -312,4 +353,4 @@ mod imp {
pub use imp::CuprateNode;
#[cfg(feature = "cuprate-daemon")]
pub use imp::{config_toml, NETWORK};
pub use imp::{NETWORK, config_toml};