-
-
Notifications
You must be signed in to change notification settings - Fork 721
fix(allocator): remove unsound Send impl and tighten Sync requirement for HashMap
#13203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
graphite-app
merged 1 commit into
main
from
08-18-fix_allocator_remove_unsound_send_impl_and_tighten_sync_requirement_for_hashmap_
Aug 19, 2025
Merged
fix(allocator): remove unsound Send impl and tighten Sync requirement for HashMap
#13203
graphite-app
merged 1 commit into
main
from
08-18-fix_allocator_remove_unsound_send_impl_and_tighten_sync_requirement_for_hashmap_
Aug 19, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Aug 18, 2025
Member
Author
CodSpeed Instrumentation Performance ReportMerging #13203 will not alter performanceComparing Summary
Footnotes |
camc314
approved these changes
Aug 19, 2025
Contributor
Merge activity
|
…ment for `HashMap` (#13203) Same as #13041. It's unsound for `oxc_allocator::HashMap` to be `Send` because if you can move a `HashMap` to another thread, you can call `insert` on it and it may allocate in the arena. This is unsound because another thread may be simultaneously be doing the same with another `HashMap`, and `Bump` is not thread-safe. Remove the `Send` impl on `HashMap`. However, we do want `HashMap` to be `Sync`. There's no reason why you shouldn't have 2 `&HashMap` references on different threads, as `&HashMap` doesn't allow mutating the `HashMap` in any way. Tighten up the trait bounds so `HashMap<K, V>` is `Sync` only if `K` and `V` both are. This is not yet completely sound. There are a couple of holes I'm aware of: 1. `allocator` method which allows obtaining a `&Bump` from a `&HashMap`. 2. `clone` which allocates into arena, taking only an immutable `&self` reference. This PR closes those holes as much as possible without a major refactor. It's not *completely* sound, but it's at least now quite difficult to commit UB - it'd be unlikely to happen by accident. So, not perfect, but at least there's now only a crack in the window, rather than the front door swinging wide open. --- The reason these 2 impls were added originally was as a quick fix to make `oxc_semantic::Scoping` `Send` and `Sync`. That is addressed in the next PR in this stack.
bb6abbc to
0815a89
Compare
1b86fbb to
f490d27
Compare
Base automatically changed from
08-12-fix_allocator_remove_unsound_send_impl_and_tighten_sync_requirements_for_vec_
to
main
August 19, 2025 21:45
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Same as #13041.
It's unsound for
oxc_allocator::HashMapto beSendbecause if you can move aHashMapto another thread, you can callinserton it and it may allocate in the arena. This is unsound because another thread may be simultaneously be doing the same with anotherHashMap, andBumpis not thread-safe.Remove the
Sendimpl onHashMap.However, we do want
HashMapto beSync. There's no reason why you shouldn't have 2&HashMapreferences on different threads, as&HashMapdoesn't allow mutating theHashMapin any way.Tighten up the trait bounds so
HashMap<K, V>isSynconly ifKandVboth are.This is not yet completely sound. There are a couple of holes I'm aware of:
allocatormethod which allows obtaining a&Bumpfrom a&HashMap.clonewhich allocates into arena, taking only an immutable&selfreference.This PR closes those holes as much as possible without a major refactor. It's not completely sound, but it's at least now quite difficult to commit UB - it'd be unlikely to happen by accident.
So, not perfect, but at least there's now only a crack in the window, rather than the front door swinging wide open.
The reason these 2 impls were added originally was as a quick fix to make
oxc_semantic::ScopingSendandSync. That is addressed in the next PR in this stack.