8000 Always capture mutable GlobalState with as const by jez · Pull Request #8792 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Always capture mutable GlobalState with as const #8792

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
merged 3 commits into from
Apr 24, 2025
Merged

Conversation

jez
Copy link
Collaborator
@jez jez commented Apr 24, 2025

Motivation

These multiplexJob calls are places where we could accidentally
concurrently mutate GlobalState. Luckily we're not doing that in any of
these places today, but I want to make sure it stays that way by explicitly
using as_const.

In particular, this revealed that mergeIndexResults technically was using a
MutableContext instead of a Context in a multiplexJob, but we were only
calling const methods (if any) on ctx so it happened to be fine.

Test plan

Existing tests

jez added 3 commits April 24, 2025 15:52
These `multiplexJob` calls are places where we could accidentally
concurrently mutate `GlobalState`. Luckily we're not doing that in any
of these places today, but I want to make sure it stays that way by
explicitly using `as_const`.
@jez jez requested a review from a team as a code owner April 24, 2025 22:54
@jez jez requested review from froydnj and removed request for a team April 24, 2025 22:54
// Unconditionally update the total to avoid starving the consumer thread
numTreesProcessed += job.numTreesProcessed;
workers.multiplexJob(
"substituteTrees", [&cgs = as_const(cgs), &logger = cgs.tracer(), batchq, resultq, cancelable]() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am on my phone, so it's hard to be 100% sure, but it looks like we don't mutate GlobalState in this function, so we could just take it as a const in this function from the start?

Copy link
Collaborator Author

Choose a reason for hiding this com 8000 ment

The reason will be displayed to describe this comment to others. Learn more.

I think that the earlier block is using cgs mutably (I think the earlier block is where we actually merge the GlobalStates.

That's probably worth another look just to be sure, but I think we can't change mergeIndexResults to take a const GlobalState &

@jez jez merged commit 1fd105d into master Apr 24, 2025
15 checks passed
@jez jez deleted the jez-gs-as-const branch April 24, 2025 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0