Skip to content

Commit 7d0e49e

Browse files
committed
Flush traces before dsc exits
1 parent 61d53a5 commit 7d0e49e

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

dsc/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rust_i18n::{i18n, t};
1010
use std::{io, process::exit};
1111
use sysinfo::{Process, RefreshKind, System, get_current_pid, ProcessRefreshKind};
1212
use tracing::{error, info, warn, debug};
13+
use util::flush_and_shutdown_tracing;
1314

1415
use crate::util::{EXIT_INVALID_INPUT, get_input};
1516

@@ -68,6 +69,7 @@ fn main() {
6869
Ok(merged) => Some(merged),
6970
Err(err) => {
7071
error!("{}: {err}", t!("main.failedMergingParameters"));
72+
flush_and_shutdown_tracing();
7173
exit(EXIT_INVALID_INPUT);
7274
}
7375
}
@@ -88,8 +90,10 @@ fn main() {
8890
SubCommand::Mcp => {
8991
if let Err(err) = start_mcp_server() {
9092
error!("{}", t!("main.failedToStartMcpServer", error = err));
93+
flush_and_shutdown_tracing();
9194
exit(util::EXIT_MCP_FAILED);
9295
}
96+
flush_and_shutdown_tracing();
9397
exit(util::EXIT_SUCCESS);
9498
}
9599
SubCommand::Resource { subcommand } => {
@@ -101,13 +105,15 @@ fn main() {
101105
Ok(json) => json,
102106
Err(err) => {
103107
error!("JSON: {err}");
108+
flush_and_shutdown_tracing();
104109
exit(util::EXIT_JSON_ERROR);
105110
}
106111
};
107112
util::write_object(&json, output_format.as_ref(), false);
108113
},
109114
}
110115

116+
flush_and_shutdown_tracing();
111117
exit(util::EXIT_SUCCESS);
112118
}
113119

@@ -119,15 +125,18 @@ fn ctrlc_handler() {
119125
info!("{}: {}", t!("main.foundProcesses"), sys.processes().len());
120126
let Ok(current_pid) = get_current_pid() else {
121127
error!("{}", t!("main.failedToGetPid"));
128+
flush_and_shutdown_tracing();
122129
exit(util::EXIT_CTRL_C);
123130
};
124131
info!("{}: {}", t!("main.currentPid"), current_pid);
125132
let Some(current_process) = sys.process(current_pid) else {
126133
error!("{}", t!("main.failedToGetProcess"));
134+
flush_and_shutdown_tracing();
127135
exit(util::EXIT_CTRL_C);
128136
};
129137

130138
terminate_subprocesses(&sys, current_process);
139+
flush_and_shutdown_tracing();
131140
exit(util::EXIT_CTRL_C);
132141
}
133142

@@ -192,6 +201,7 @@ fn check_store() {
192201
eprintln!("{}", t!("main.storeMessage"));
193202
// wait for keypress
194203
let _ = io::stdin().read(&mut [0u8]).unwrap();
204+
flush_and_shutdown_tracing();
195205
exit(util::EXIT_INVALID_ARGS);
196206
}
197207
}

dsc/src/util.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use std::env;
5353
use std::io::{IsTerminal, Read, stdout, Write};
5454
use std::path::Path;
5555
use std::process::exit;
56+
use std::sync::Once;
5657
use syntect::{
5758
easy::HighlightLines,
5859
highlighting::ThemeSet,
@@ -690,3 +691,23 @@ pub fn merge_parameters(file_params: &str, inline_params: &str) -> Result<String
690691
let merged = Value::Object(file_map);
691692
Ok(serde_json::to_string(&merged)?)
692693
}
694+
695+
static FLUSH_ONCE: Once = Once::new();
696+
697+
/// Flush and shutdown tracing to ensure all traces are written before exit.
698+
/// This function ensures that any pending trace writes are completed and
699+
/// background writer threads have time to finish their work.
700+
pub fn flush_and_shutdown_tracing() {
701+
FLUSH_ONCE.call_once(|| {
702+
// Force any pending writes to complete
703+
if let Err(e) = std::io::stderr().flush() {
704+
eprintln!("Failed to flush stderr: {}", e);
705+
}
706+
if let Err(e) = std::io::stdout().flush() {
707+
eprintln!("Failed to flush stdout: {}", e);
708+
}
709+
710+
// Small delay to ensure async writes complete
711+
std::thread::sleep(std::time::Duration::from_millis(50));
712+
});
713+
}

0 commit comments

Comments
 (0)