Don't propagate overwritten RUST_{,LIB_}BACKTRACE
Browse files at this commit7d797336aa800dd9cf4a2b476df6a11965275760Parent: a2ad7b02 changed files +20 -1
- src/main.rs +8 -1
- src/utils.rs +12 -0
src/main.rsmodified+8 -1
| @@ -39,6 +39,8 @@ use tracing_subscriber::EnvFilter; | |||
| 39 | use utils::spawn; | 39 | use utils::spawn; |
| 40 | use watcher::Watcher; | 40 | use watcher::Watcher; |
| 41 | 41 | ||
| 42 | +use crate::utils::{REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE}; | ||
| 43 | + | ||
| 42 | #[derive(Parser)] | 44 | #[derive(Parser)] |
| 43 | #[command(author, version, about, long_about = None)] | 45 | #[command(author, version, about, long_about = None)] |
| 44 | struct Cli { | 46 | struct Cli { |
| @@ -51,9 +53,14 @@ struct Cli { | |||
| 51 | } | 53 | } |
| 52 | 54 | ||
| 53 | fn main() { | 55 | fn main() { |
| 54 | - env::set_var("RUST_BACKTRACE", "1"); | 56 | + // Set backtrace defaults if not set. |
| 57 | + if env::var_os("RUST_BACKTRACE").is_none() { | ||
| 58 | + env::set_var("RUST_BACKTRACE", "1"); | ||
| 59 | + REMOVE_ENV_RUST_BACKTRACE.store(true, Ordering::Relaxed); | ||
| 60 | + } | ||
| 55 | if env::var_os("RUST_LIB_BACKTRACE").is_none() { | 61 | if env::var_os("RUST_LIB_BACKTRACE").is_none() { |
| 56 | env::set_var("RUST_LIB_BACKTRACE", "0"); | 62 | env::set_var("RUST_LIB_BACKTRACE", "0"); |
| 63 | + REMOVE_ENV_RUST_LIB_BACKTRACE.store(true, Ordering::Relaxed); | ||
| 57 | } | 64 | } |
| 58 | 65 | ||
| 59 | let is_systemd_service = env::var_os("NOTIFY_SOCKET").is_some(); | 66 | let is_systemd_service = env::var_os("NOTIFY_SOCKET").is_some(); |
src/utils.rsmodified+12 -0
| @@ -5,6 +5,7 @@ use std::os::unix::process::CommandExt; | |||
| 5 | use std::path::PathBuf; | 5 | use std::path::PathBuf; |
| 6 | use std::process::{Command, Stdio}; | 6 | use std::process::{Command, Stdio}; |
| 7 | use std::ptr::null_mut; | 7 | use std::ptr::null_mut; |
| 8 | +use std::sync::atomic::{AtomicBool, Ordering}; | ||
| 8 | use std::time::Duration; | 9 | use std::time::Duration; |
| 9 | 10 | ||
| 10 | use anyhow::{ensure, Context}; | 11 | use anyhow::{ensure, Context}; |
| @@ -53,6 +54,9 @@ pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> | |||
| 53 | Ok(Some(path)) | 54 | Ok(Some(path)) |
| 54 | } | 55 | } |
| 55 | 56 | ||
| 57 | +pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false); | ||
| 58 | +pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false); | ||
| 59 | + | ||
| 56 | /// Spawns the command to run independently of the compositor. | 60 | /// Spawns the command to run independently of the compositor. |
| 57 | pub fn spawn(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) { | 61 | pub fn spawn(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) { |
| 58 | let _span = tracy_client::span!(); | 62 | let _span = tracy_client::span!(); |
| @@ -66,6 +70,14 @@ pub fn spawn(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsR | |||
| 66 | .stdout(Stdio::null()) | 70 | .stdout(Stdio::null()) |
| 67 | .stderr(Stdio::null()); | 71 | .stderr(Stdio::null()); |
| 68 | 72 | ||
| 73 | + // Remove RUST_BACKTRACE and RUST_LIB_BACKTRACE from the environment if needed. | ||
| 74 | + if REMOVE_ENV_RUST_BACKTRACE.load(Ordering::Relaxed) { | ||
| 75 | + process.env_remove("RUST_BACKTRACE"); | ||
| 76 | + } | ||
| 77 | + if REMOVE_ENV_RUST_LIB_BACKTRACE.load(Ordering::Relaxed) { | ||
| 78 | + process.env_remove("RUST_LIB_BACKTRACE"); | ||
| 79 | + } | ||
| 80 | + | ||
| 69 | // Double-fork to avoid having to waitpid the child. | 81 | // Double-fork to avoid having to waitpid the child. |
| 70 | unsafe { | 82 | unsafe { |
| 71 | process.pre_exec(|| { | 83 | process.pre_exec(|| { |