Skip to content

Commit 7d7c522

Browse files
committed
fix(metadata): lock metadata file to protect against concurrent access
Cargo may launch concurrent compilation once the rmeta file is generated by Rust, before the full build completes. This is non-desirable for Klint when it yet have the chance to finish writing metadata file. Use sqlite's locking_mode to protect against this race.
1 parent 4838a48 commit 7d7c522

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/ctxt.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,21 @@ impl<'tcx> AnalysisCtxt<'tcx> {
305305
Ok(())
306306
})
307307
.unwrap();
308-
conn.execute("begin immediate", ()).unwrap();
308+
309+
// Don't create separate -jorunal files, which makes gitignore harder.
310+
conn.pragma_update(None, "journal_mode", "MEMORY").unwrap();
311+
312+
// Cargo may launch a dependent compilation once metadata has been generated
313+
// but not yet fully compiled. For various reasons this is bad for klint as it wants to
314+
// do some analysis at later stages and possibly want to preserve them inside metadata.
315+
//
316+
// Set locking mode to exclusive so that sqlite takes file lock and thus no dependent
317+
// compilations can observe unwritten metadata.
318+
//
319+
// If we move away from sqlite in the future, take a manual flock instead.
320+
conn.pragma_update(None, "locking_mode", "EXCLUSIVE").unwrap();
321+
322+
conn.execute("begin exclusive", ()).unwrap();
309323
conn.pragma_update(None, "user_version", SCHEMA_VERSION)
310324
.unwrap();
311325

0 commit comments

Comments
 (0)