From c00c9d4151e2265f2c2d726cbd834cc45fc8aa30 Mon Sep 17 00:00:00 2001 From: agent Date: Sat, 19 Sep 2026 17:54:44 -0400 Subject: [PATCH] fix(cuprate): seed XDG so Android parse does not poison LazyLock Cuprate's serde defaults call ArtiConfig::default, which unwraps dirs::data_dir(). On Android HOME is unset and dirs-sys has no getpwuid fallback, so the first Start panics and a retry reports "LazyLock instance has previously been poisoned". Point XDG at the chain datadir before parse and set tor.arti.directory_path in TOML. --- src/cuprate.rs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 4 deletions(-) diff --git a/src/cuprate.rs b/src/cuprate.rs index e00d168..5ef6922 100644 --- a/src/cuprate.rs +++ b/src/cuprate.rs @@ -97,6 +97,39 @@ mod imp { } } + /// Cuprate's `#[serde(default)]` fills missing nested structs via `Default`. + /// `ArtiConfig::default` (and `FileSystemConfig::default`) dereference + /// `CUPRATE_DATA_DIR` / `CUPRATE_CACHE_DIR`, which `unwrap()` `dirs::*`. + /// On Android `dirs-sys` has no getpwuid fallback and `HOME` is unset, so + /// those return `None`, the unwrap panics, and a second Start then hits + /// "LazyLock instance has previously been poisoned". + /// + /// Point XDG at the chain datadir *before* parsing so those LazyLocks + /// initialize to a real path. Process-wide: OnionWire's own store is + /// `$ONIONWIRE_HOME`, not XDG. + fn seed_cuprate_xdg(datadir: &Path) { + let datadir = std::path::absolute(datadir).unwrap_or_else(|_| datadir.to_path_buf()); + let home_needs_fix = match std::env::var_os("HOME") { + None => true, + Some(h) => Path::new(&h).parent().is_none(), + }; + // SAFETY: called once on the dedicated cuprate launch thread before + // any other thread in this process reads these keys for Cuprate. + // OnionWire's store is `$ONIONWIRE_HOME`, not XDG. + unsafe { + // `dirs` ignores non-absolute XDG_* and falls through to HOME. + std::env::set_var("XDG_DATA_HOME", datadir.join("xdg-data")); + std::env::set_var("XDG_CACHE_HOME", datadir.join("xdg-cache")); + std::env::set_var("XDG_CONFIG_HOME", datadir.join("xdg-config")); + // HOME=/ (seen on some Androids) fails cuprate_helper::fs's + // "parent is some" assert even when XDG is set, if anything still + // reads HOME. Replace only a missing or root HOME. + if home_needs_fix { + std::env::set_var("HOME", &datadir); + } + } + } + fn run_on_launch_thread(f: F) -> Result where F: FnOnce() -> Result + Send + 'static, @@ -108,7 +141,16 @@ mod imp { .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)))); + .unwrap_or_else(|p| { + let msg = panic_message(&*p); + if msg.contains("previously been poisoned") { + Err(format!( + "cuprate panicked: {msg} — force-stop the app; a previous panic poisoned process-wide init and cannot recover in-process" + )) + } else { + Err(format!("cuprate panicked: {msg}")) + } + }); let _ = tx.send(result); }) .map_err(|e| format!("launch thread: {e}"))?; @@ -149,7 +191,9 @@ mod imp { prune: bool, ) -> Result { let text = config_toml(datadir, port, network, prune); - let (runtime, node) = run_on_launch_thread(move || launch_on_owned_runtime(&text))?; + let datadir = datadir.to_path_buf(); + let (runtime, node) = + run_on_launch_thread(move || launch_on_owned_runtime(&text, &datadir))?; Ok(Self { runtime: Some(runtime), node: Some(node), @@ -202,7 +246,11 @@ mod imp { /// /// Runs on the launch thread — never on a thread that is already inside a /// runtime, which is what makes `block_on` legal here. - fn launch_on_owned_runtime(text: &str) -> Result<(tokio::runtime::Runtime, Node), String> { + fn launch_on_owned_runtime( + text: &str, + datadir: &Path, + ) -> Result<(tokio::runtime::Runtime, Node), String> { + seed_cuprate_xdg(datadir); let mut config: Config = toml::from_str(text).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() @@ -269,7 +317,12 @@ enabled = false [tor] mode = "Auto" -"# + +# Explicit so serde does not call ArtiConfig::default(), which unwraps +# dirs::data_dir() (None on Android: no HOME, no getpwuid fallback). +[tor.arti] +directory_path = "{datadir}/arti" +"#, ) } @@ -313,6 +366,44 @@ mode = "Auto" "that string is only for a thread that never sent" ); } + + #[test] + fn the_toml_sets_arti_directory_so_default_does_not_touch_xdg() { + let text = config_toml(Path::new("/tmp/onionwire-chain"), 18081, "Mainnet", true); + assert!( + text.contains("[tor.arti]"), + "ArtiConfig::default unwraps dirs::data_dir(); on Android that is None" + ); + assert!( + text.contains("/tmp/onionwire-chain/arti"), + "directory_path must be the chain datadir, got {text}" + ); + } + + #[test] + fn seeding_xdg_lets_the_config_parse() { + let datadir = Path::new("/tmp/onionwire-chain"); + seed_cuprate_xdg(datadir); + let mut config: Config = toml::from_str(&config_toml(datadir, 18081, "Testnet", false)) + .expect("parse after xdg seed"); + resolve_max_memory(&mut config).expect("target_max_memory is set"); + assert_eq!( + config.fjall_directory(), + Path::new("/tmp/onionwire-chain/testnet/fjall") + ); + } + + #[test] + fn a_poisoned_lazylock_panic_tells_the_user_to_force_stop() { + let err = run_on_launch_thread(|| -> Result<(), String> { + panic!("LazyLock instance has previously been poisoned") + }) + .expect_err("a panic is an error"); + assert!( + err.contains("force-stop the app"), + "poison is unrecoverable in-process, got {err:?}" + ); + } } }