Garbage-collect old incremental compilation sessions

This commit is contained in:
Charlie Marsh
2026-09-03 07:43:04 -04:00
parent 4fcf39725a
commit bc21abbd79
3 changed files with 41 additions and 2 deletions

View File

@@ -702,8 +702,10 @@ pub(crate) fn garbage_collect_session_directories(
lock_file_to_session_dir.items().filter_map(|(lock_file_name, directory_name)| {
debug!("garbage_collect_session_directories() - inspecting: {}", directory_name);
if directory_name.as_str() == current_session_directory_name {
// Skipping our own directory is, unfortunately, important for correctness.
if directory_name.as_str() == current_session_directory_name
&& !is_finalized(directory_name)
{
// Skipping our own active directory is important for correctness.
//
// To summarize #147821: we will try to lock directories before deciding they can be
// garbage collected, but the ability of `flock::Lock` to detect a lock held *by the
@@ -722,6 +724,8 @@ pub(crate) fn garbage_collect_session_directories(
// It's not clear that `flock::Lock` can be fixed for this in general, and our own
// incremental session directory is the only one which this process may own, so skip
// it here and avoid the problem. We know it's not garbage anyway: we're using it.
// Once finalized, its lock is released. Include it in collection so we keep only
// the newest completed session.
return None;
}

View File

@@ -0,0 +1,2 @@
#![feature(no_core)]
#![no_core]

View File

@@ -0,0 +1,33 @@
//! Successful sequential builds should retain only the newest incremental session.
//! The current session must participate in garbage collection once it is finalized.
use std::path::PathBuf;
use run_make_support::{rfs, rustc, shallow_find_directories};
fn main() {
let compile = || {
rustc().input("empty.rs").crate_type("rlib").emit("metadata").incremental("incr").run();
};
compile();
let mut previous = session_dir();
rfs::write(previous.join("sentinel"), "previous session");
for _ in 0..2 {
compile();
let current = session_dir();
assert_ne!(previous, current);
assert!(!previous.exists(), "superseded session was not collected: {previous:?}");
assert_eq!(rfs::read_to_string(current.join("sentinel")), "previous session");
previous = current;
}
}
fn session_dir() -> PathBuf {
let crate_dirs = shallow_find_directories("incr", |_| true);
assert_eq!(crate_dirs.len(), 1);
let sessions = shallow_find_directories(&crate_dirs[0], |_| true);
assert_eq!(sessions.len(), 1, "expected only the newest completed session: {sessions:?}");
sessions.into_iter().next().unwrap()
}