Skip to content

Commit 5e64197

Browse files
Read cached output line-by-line
This avoids loading potentially gigabytes of output into memory, which can cause OOMs.
1 parent 7c2cadd commit 5e64197

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub mod unit_dependencies;
1919
use std::env;
2020
use std::ffi::{OsStr, OsString};
2121
use std::fs::{self, File};
22-
use std::io::Write;
22+
use std::io::{BufRead, Write};
2323
use std::path::PathBuf;
2424
use std::sync::Arc;
2525

@@ -1252,9 +1252,19 @@ fn replay_output_cache(
12521252
// No cached output, probably didn't emit anything.
12531253
return Ok(());
12541254
}
1255-
let contents = fs::read_to_string(&path)?;
1256-
for line in contents.lines() {
1257-
on_stderr_line(state, line, package_id, &target, &mut options)?;
1255+
// We sometimes have gigabytes of output from the compiler, so avoid
1256+
// loading it all into memory at once, as that can cause OOM where
1257+
// otherwise there would be none.
1258+
let file = fs::File::open(&path)?;
1259+
let mut reader = std::io::BufReader::new(file);
1260+
let mut line = String::new();
1261+
loop {
1262+
let length = reader.read_line(&mut line)?;
1263+
if length == 0 {
1264+
break;
1265+
}
1266+
on_stderr_line(state, line.as_str(), package_id, &target, &mut options)?;
1267+
line.clear();
12581268
}
12591269
Ok(())
12601270
})

0 commit comments

Comments
 (0)