Skip to content

Commit 246b6e3

Browse files
committed
encapsulate flush into exit()
1 parent 6f8afb8 commit 246b6e3

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

dsc/src/main.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use clap_complete::generate;
77
use dsc_lib::progress::ProgressFormat;
88
use mcp::start_mcp_server;
99
use rust_i18n::{i18n, t};
10-
use std::{io, process::exit};
10+
use std::io;
1111
use sysinfo::{Process, RefreshKind, System, get_current_pid, ProcessRefreshKind};
1212
use tracing::{error, info, warn, debug};
13-
use util::flush_and_shutdown_tracing;
13+
use util::exit;
1414

1515
use crate::util::{EXIT_INVALID_INPUT, get_input};
1616

@@ -69,7 +69,6 @@ fn main() {
6969
Ok(merged) => Some(merged),
7070
Err(err) => {
7171
error!("{}: {err}", t!("main.failedMergingParameters"));
72-
flush_and_shutdown_tracing();
7372
exit(EXIT_INVALID_INPUT);
7473
}
7574
}
@@ -90,10 +89,8 @@ fn main() {
9089
SubCommand::Mcp => {
9190
if let Err(err) = start_mcp_server() {
9291
error!("{}", t!("main.failedToStartMcpServer", error = err));
93-
flush_and_shutdown_tracing();
9492
exit(util::EXIT_MCP_FAILED);
9593
}
96-
flush_and_shutdown_tracing();
9794
exit(util::EXIT_SUCCESS);
9895
}
9996
SubCommand::Resource { subcommand } => {
@@ -105,15 +102,13 @@ fn main() {
105102
Ok(json) => json,
106103
Err(err) => {
107104
error!("JSON: {err}");
108-
flush_and_shutdown_tracing();
109105
exit(util::EXIT_JSON_ERROR);
110106
}
111107
};
112108
util::write_object(&json, output_format.as_ref(), false);
113109
},
114110
}
115111

116-
flush_and_shutdown_tracing();
117112
exit(util::EXIT_SUCCESS);
118113
}
119114

@@ -125,18 +120,15 @@ fn ctrlc_handler() {
125120
info!("{}: {}", t!("main.foundProcesses"), sys.processes().len());
126121
let Ok(current_pid) = get_current_pid() else {
127122
error!("{}", t!("main.failedToGetPid"));
128-
flush_and_shutdown_tracing();
129123
exit(util::EXIT_CTRL_C);
130124
};
131125
info!("{}: {}", t!("main.currentPid"), current_pid);
132126
let Some(current_process) = sys.process(current_pid) else {
133127
error!("{}", t!("main.failedToGetProcess"));
134-
flush_and_shutdown_tracing();
135128
exit(util::EXIT_CTRL_C);
136129
};
137130

138131
terminate_subprocesses(&sys, current_process);
139-
flush_and_shutdown_tracing();
140132
exit(util::EXIT_CTRL_C);
141133
}
142134

@@ -201,7 +193,6 @@ fn check_store() {
201193
eprintln!("{}", t!("main.storeMessage"));
202194
// wait for keypress
203195
let _ = io::stdin().read(&mut [0u8]).unwrap();
204-
flush_and_shutdown_tracing();
205196
exit(util::EXIT_INVALID_ARGS);
206197
}
207198
}

dsc/src/util.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ use std::collections::HashMap;
5252
use std::env;
5353
use std::io::{IsTerminal, Read, stdout, Write};
5454
use std::path::Path;
55-
use std::process::exit;
56-
use std::sync::Once;
5755
use syntect::{
5856
easy::HighlightLines,
5957
highlighting::ThemeSet,
@@ -692,22 +690,17 @@ pub fn merge_parameters(file_params: &str, inline_params: &str) -> Result<String
692690
Ok(serde_json::to_string(&merged)?)
693691
}
694692

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-
}
693+
/// Exit the process with the given code after flushing and shutting down tracing.
694+
pub fn exit(code: i32) -> ! {
695+
// Force any pending writes to complete
696+
if let Err(e) = std::io::stderr().flush() {
697+
eprintln!("Failed to flush stderr: {}", e);
698+
}
699+
if let Err(e) = std::io::stdout().flush() {
700+
eprintln!("Failed to flush stdout: {}", e);
701+
}
709702

710-
// Small delay to ensure async writes complete
711-
std::thread::sleep(std::time::Duration::from_millis(50));
712-
});
703+
// Small delay to ensure async writes complete
704+
std::thread::sleep(std::time::Duration::from_millis(50));
705+
std::process::exit(code);
713706
}

0 commit comments

Comments
 (0)