Don't propagate overwritten RUST_{,LIB_}BACKTRACE

This commit is contained in:
Ivan Molodetskikh
2023-11-24 21:42:37 +04:00
parent a2ad7b0854
commit 7d797336aa
2 changed files with 20 additions and 1 deletions
+8 -1
View File
@@ -39,6 +39,8 @@ use tracing_subscriber::EnvFilter;
use utils::spawn;
use watcher::Watcher;
use crate::utils::{REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
@@ -51,9 +53,14 @@ struct Cli {
}
fn main() {
env::set_var("RUST_BACKTRACE", "1");
// Set backtrace defaults if not set.
if env::var_os("RUST_BACKTRACE").is_none() {
env::set_var("RUST_BACKTRACE", "1");
REMOVE_ENV_RUST_BACKTRACE.store(true, Ordering::Relaxed);
}
if env::var_os("RUST_LIB_BACKTRACE").is_none() {
env::set_var("RUST_LIB_BACKTRACE", "0");
REMOVE_ENV_RUST_LIB_BACKTRACE.store(true, Ordering::Relaxed);
}
let is_systemd_service = env::var_os("NOTIFY_SOCKET").is_some();
+12
View File
@@ -5,6 +5,7 @@ use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::ptr::null_mut;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use anyhow::{ensure, Context};
@@ -53,6 +54,9 @@ pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>>
Ok(Some(path))
}
pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false);
pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);
/// Spawns the command to run independently of the compositor.
pub fn spawn(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
let _span = tracy_client::span!();
@@ -66,6 +70,14 @@ pub fn spawn(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsR
.stdout(Stdio::null())
.stderr(Stdio::null());
// Remove RUST_BACKTRACE and RUST_LIB_BACKTRACE from the environment if needed.
if REMOVE_ENV_RUST_BACKTRACE.load(Ordering::Relaxed) {
process.env_remove("RUST_BACKTRACE");
}
if REMOVE_ENV_RUST_LIB_BACKTRACE.load(Ordering::Relaxed) {
process.env_remove("RUST_LIB_BACKTRACE");
}
// Double-fork to avoid having to waitpid the child.
unsafe {
process.pre_exec(|| {