fix(cuprate): catch launch panics and give the thread 8 MiB stack
All checks were successful
ci / test (pull_request) Successful in 5m25s
ci / sdk (pull_request) Successful in 1m12s
android-ci / android (pull_request) Successful in 10m50s
ci / fuzz (pull_request) Successful in 4m59s

Android's default pthread stack is ~1 MiB. Cuprate+RandomX overflow it,
the launch thread dies before tx.send, and the UI only says "the launch
thread died before answering". Catch unwind so a Rust panic is the error
string, and size the launch thread plus tokio workers at 8 MiB.
This commit is contained in:
agent 2026-09-19 15:47:51 -04:00
commit a7daf2c768
No known key found for this signature in database

View file

@ -60,8 +60,8 @@ mod imp {
use std::path::Path; use std::path::Path;
use std::sync::mpsc; use std::sync::mpsc;
use cuprated::config::{resolve_max_memory, Config};
use cuprated::Node; use cuprated::Node;
use cuprated::config::{Config, resolve_max_memory};
/// The network this daemon runs. Testnet, deliberately — see the module /// The network this daemon runs. Testnet, deliberately — see the module
/// docs. Cuprate spells this `Testnet`. /// docs. Cuprate spells this `Testnet`.
@ -81,6 +81,41 @@ mod imp {
/// wallet child's RPC is the only client). /// wallet child's RPC is the only client).
const RUNTIME_WORKERS: usize = 2; 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. /// A running in-process chain daemon.
/// ///
/// Owning it *is* the lifecycle: `cuprated::Node` implements `Drop` and asks /// Owning it *is* the lifecycle: `cuprated::Node` implements `Drop` and asks
@ -114,16 +149,7 @@ mod imp {
prune: bool, prune: bool,
) -> Result<Self, String> { ) -> Result<Self, String> {
let text = config_toml(datadir, port, network, prune); let text = config_toml(datadir, port, network, prune);
let (tx, rx) = mpsc::channel(); let (runtime, node) = run_on_launch_thread(move || launch_on_owned_runtime(&text))?;
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())??;
Ok(Self { Ok(Self {
runtime: Some(runtime), runtime: Some(runtime),
node: Some(node), node: Some(node),
@ -181,6 +207,7 @@ mod imp {
resolve_max_memory(&mut config).map_err(|e| format!("config: {e}"))?; resolve_max_memory(&mut config).map_err(|e| format!("config: {e}"))?;
let runtime = tokio::runtime::Builder::new_multi_thread() let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(RUNTIME_WORKERS) .worker_threads(RUNTIME_WORKERS)
.thread_stack_size(LAUNCH_STACK)
.enable_all() .enable_all()
.build() .build()
.map_err(|e| format!("runtime: {e}"))?; .map_err(|e| format!("runtime: {e}"))?;
@ -272,6 +299,20 @@ mode = "Auto"
"the chain database is a separate store under the chain datadir" "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; pub use imp::CuprateNode;
#[cfg(feature = "cuprate-daemon")] #[cfg(feature = "cuprate-daemon")]
pub use imp::{config_toml, NETWORK}; pub use imp::{NETWORK, config_toml};