Skip to content

Commit 17f7202

Browse files
committed
chore: use Box::leak() to hold logging guard for the remainder of the program’s life.
A guard using `let _guard = ...` will be dropped when the main function returns, while there are other threads running and depending the logging guard to output logs, which result in an error output to stderr. chore: use `Box::leak()` to ensure logging guard lives for the program's lifetime Previously, the logging guard was held with `let _guard = ...`, which caused it to be dropped when the `main` function returned. This could lead to an error when other threads, still running and depending on the logging guard for log output, attempt to log after the guard has been dropped. By using `Box::leak()`, the logging guard is ensured to remain valid for the entire lifetime of the program, preventing such errors. - Fix: #16676
1 parent 34be4e4 commit 17f7202

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

src/meta/binaries/meta/entry.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ pub async fn entry(conf: Config) -> anyhow::Result<()> {
7171
"cluster_name".to_string(),
7272
conf.raft_config.cluster_name.clone(),
7373
);
74-
let _guards = init_logging(&app_name_shuffle, &conf.log, log_labels);
74+
let guards = init_logging(&app_name_shuffle, &conf.log, log_labels);
75+
Box::new(guards).leak();
7576

7677
info!("Databend Meta version: {}", METASRV_COMMIT_VERSION.as_str());
7778
info!(

src/meta/binaries/metactl/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ async fn main() -> anyhow::Result<()> {
375375
},
376376
..Default::default()
377377
};
378-
let _guards = init_logging("metactl", &log_config, BTreeMap::new());
378+
let guards = init_logging("metactl", &log_config, BTreeMap::new());
379+
Box::new(guards).leak();
379380

380381
match app.command {
381382
Some(ref cmd) => match cmd {

src/meta/binaries/metaverifier/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ async fn main() -> Result<()> {
9494
..Default::default()
9595
};
9696

97-
let _guards = init_logging("databend-metaverifier", &log_config, BTreeMap::new());
97+
let guards = init_logging("databend-metaverifier", &log_config, BTreeMap::new());
98+
Box::new(guards).leak();
9899

99100
println!("config: {:?}", config);
100101
if config.grpc_api_address.is_empty() {

src/meta/process/src/examples.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ impl Default for RaftConfig {
6161
async fn upgrade_09() -> anyhow::Result<()> {
6262
let config = Config::parse();
6363

64-
let _guards = init_logging(
64+
let guards = init_logging(
6565
"databend-meta-upgrade-09",
6666
&LogConfig::default(),
6767
BTreeMap::new(),
6868
);
69+
Box::new(guards).leak();
6970

7071
eprintln!("config: {}", pretty(&config)?);
7172

0 commit comments

Comments
 (0)