Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9131bdc

Browse filesBrowse files
authored
fix(debug): prevent multiple writes on the same file (#4117)
<!-- Thank you for contributing! --> ### Description Has comfirmed with vite-devtool team. This PR fix their problems. <!-- Please insert your description here and provide especially info about the "what" this PR is solving -->
1 parent e3ab62a commit 9131bdc
Copy full SHA for 9131bdc

File tree

Expand file treeCollapse file tree

3 files changed

+29
-13
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+29
-13
lines changed

‎Cargo.lock

Copy file name to clipboardExpand all lines: Cargo.lock
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/rolldown_debug/Cargo.toml

Copy file name to clipboardExpand all lines: crates/rolldown_debug/Cargo.toml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license.workspace = true
77
repository.workspace = true
88

99
[dependencies]
10+
dashmap = { workspace = true }
1011
rolldown_debug_action = { workspace = true }
1112
serde = { workspace = true, features = ["derive", "rc"] }
1213
serde_json = { workspace = true }

‎crates/rolldown_debug/src/debug_formatter.rs

Copy file name to clipboardExpand all lines: crates/rolldown_debug/src/debug_formatter.rs
+27-13Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
time::{SystemTime, UNIX_EPOCH},
55
};
66

7+
use dashmap::DashMap;
78
use serde::ser::{SerializeMap, Serializer as _};
89
use tracing::{Event, Subscriber};
910
use tracing_serde::AsSerde;
@@ -14,6 +15,9 @@ use tracing_subscriber::{
1415

1516
use crate::build_id_propagate_layer::BuildId;
1617

18+
static OPEN_FILES: std::sync::LazyLock<DashMap<String, std::fs::File>> =
19+
std::sync::LazyLock::new(DashMap::new);
20+
1721
pub struct DevtoolFormatter;
1822

1923
impl<S, N> FormatEvent<S, N> for DevtoolFormatter
@@ -53,19 +57,29 @@ where
5357
format!(".rolldown/{}/log.json", build_id.0)
5458
});
5559

56-
let mut file = match OpenOptions::new().create(true).append(true).open(&log_filename) {
57-
Ok(v) => v,
58-
Err(e) => match e.kind() {
59-
std::io::ErrorKind::ReadOnlyFilesystem => {
60-
// WASI environment, we can't write to the filesystem
61-
return Ok(());
62-
}
63-
_ => {
64-
// Other errors, we just return the error
65-
return Err(std::fmt::Error);
66-
}
67-
},
68-
};
60+
// TODO(hyf0): While this prevents memory leaks, we should come up with a better solution.
61+
if OPEN_FILES.len() > 10 {
62+
let oldest_file = OPEN_FILES.iter().next();
63+
if let Some(oldest_file) = oldest_file {
64+
// If we have more than 10 open files, we need to close the oldest one.
65+
OPEN_FILES.remove(oldest_file.key());
66+
}
67+
}
68+
69+
if !OPEN_FILES.contains_key(&log_filename) {
70+
let file = OpenOptions::new()
71+
.create(true)
72+
.append(true)
73+
.open(&log_filename)
74+
.map_err(|_| std::fmt::Error)?;
75+
// Ensure for each file, we only have one unique file handle to prevent multiple writes.
76+
OPEN_FILES.insert(log_filename.clone(), file);
77+
}
78+
79+
let mut file =
80+
OPEN_FILES.get_mut(&log_filename).unwrap_or_else(|| panic!("{log_filename} not found"));
81+
let mut file = file.value_mut();
82+
6983
let visit = || {
7084
let mut serializer = serde_json::Serializer::new(&mut file);
7185
let mut serializer = serializer.serialize_map(None)?;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.