diff options
| author | Laurenz <laurmaedje@gmail.com> | 2024-05-30 14:46:47 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-30 12:46:47 +0000 |
| commit | 8a9c45e7d4e9f8d7e155c3a90ca07afc65e87238 (patch) | |
| tree | 7cdc80d1d421b172f6c4431b3c722b71f9488ebf /crates/typst-syntax | |
| parent | fa7fbb82749f6e8a150fb81c1cdd0efeb80aba14 (diff) | |
Fix race condition in interners (#4300)
Diffstat (limited to 'crates/typst-syntax')
| -rw-r--r-- | crates/typst-syntax/src/file.rs | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/crates/typst-syntax/src/file.rs b/crates/typst-syntax/src/file.rs index b76cb9e3..356337f3 100644 --- a/crates/typst-syntax/src/file.rs +++ b/crates/typst-syntax/src/file.rs @@ -36,17 +36,20 @@ impl FileId { #[track_caller] pub fn new(package: Option<PackageSpec>, path: VirtualPath) -> Self { // Try to find an existing entry that we can reuse. + // + // We could check with just a read lock, but if the pair is not yet + // present, we would then need to recheck after acquiring a write lock, + // which is probably not worth it. let pair = (package, path); - if let Some(&id) = INTERNER.read().unwrap().to_id.get(&pair) { + let mut interner = INTERNER.write().unwrap(); + if let Some(&id) = interner.to_id.get(&pair) { return id; } - let mut interner = INTERNER.write().unwrap(); - let num = interner.from_id.len().try_into().expect("out of file ids"); - // Create a new entry forever by leaking the pair. We can't leak more // than 2^16 pair (and typically will leak a lot less), so its not a // big deal. + let num = interner.from_id.len().try_into().expect("out of file ids"); let id = FileId(num); let leaked = Box::leak(Box::new(pair)); interner.to_id.insert(leaked, id); |
