Drop the query result memmap before serializing it back.

This commit is contained in:
Camille GILLOT 2021-03-17 22:49:16 +01:00
parent 6b47e1ece8
commit 98007e2ce6
4 changed files with 44 additions and 26 deletions

View file

@ -52,7 +52,10 @@ where
// Delete the old file, if any.
// Note: It's important that we actually delete the old file and not just
// truncate and overwrite it, since it might be a shared hard-link, the
// underlying data of which we don't want to modify
// underlying data of which we don't want to modify.
//
// We have to ensure we have dropped the memory maps to this file
// before performing this removal.
match fs::remove_file(&path_buf) {
Ok(()) => {
debug!("save: remove old file");
@ -114,6 +117,12 @@ pub fn read_file(
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(err),
};
// SAFETY: This process must not modify nor remove the backing file while the memory map lives.
// For the dep-graph and the work product index, it is as soon as the decoding is done.
// For the query result cache, the memory map is dropped in save_dep_graph before calling
// save_in and trying to remove the backing file.
//
// There is no way to prevent another process from modifying this file.
let mmap = unsafe { Mmap::map(file) }?;
let mut file = io::Cursor::new(&*mmap);

View file

@ -42,6 +42,11 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
join(
move || {
sess.time("incr_comp_persist_result_cache", || {
// Drop the memory map so that we can remove the file and write to it.
if let Some(odc) = &tcx.on_disk_cache {
odc.drop_serialized_data(tcx);
}
file_format::save_in(sess, query_cache_path, "query cache", |e| {
encode_query_cache(tcx, e)
});