Skip to content

Commit

Permalink
Auto merge of #7737 - Mark-Simulacrum:cache-not-mem, r=ehuss
Browse files Browse the repository at this point in the history
Read cached output line-by-line

This avoids loading potentially gigabytes of output into memory, which
can cause OOMs.

Fixes #7736.

This does not add a test as I don't really want to generate gigabytes of output (that seems like a bad idea) -- and it's unclear how to test other than by causing OOM on (most) CI systems, and it's unlikely that we'll actually regress here.
  • Loading branch information
bors committed Dec 22, 2019
2 parents 741f0e5 + 5e64197 commit de4a5c9
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod unit_dependencies;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs::{self, File};
use std::io::Write;
use std::io::{BufRead, Write};
use std::path::PathBuf;
use std::sync::Arc;

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

0 comments on commit de4a5c9

Please sign in to comment.