Skip to content
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

Locate targets under mutex lock #2976

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vinistock
Copy link
Member

@vinistock vinistock commented Dec 12, 2024

Motivation

I'm hoping this is the solution for #2446

My hypothesis is that we're seeing a concurrency issue where threads switch in the middle of locating targets and then the document gets mutated. That would render the location we're currently searching for invalid, which can then lead to infinite loops.

Implementation

My idea is to move our mutex into the global state so that we can use it in more places. Then we lock the mutex while locating targets to avoid having any document edits be applied in the middle.

Note: I experimented with passing the mutex down to the document instances, but it was a bit messier.

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@vinistock vinistock added bugfix This PR will fix an existing bug server This pull request should be included in the server gem's release notes labels Dec 12, 2024 — with Graphite App
@vinistock vinistock marked this pull request as ready for review December 12, 2024 17:46
@vinistock vinistock requested a review from a team as a code owner December 12, 2024 17:46
@vinistock vinistock force-pushed the 12-11-locate_targets_under_mutex_lock branch from 006f5b1 to 6980656 Compare December 16, 2024 14:54
@document = document
@position = T.let(position, T::Hash[Symbol, Integer])
end

sig { override.returns(T.nilable(Interface::Range)) }
def perform
char_position = @document.create_scanner.find_char_position(@position)
char_position = @global_state.synchronize do
@document.create_scanner.find_char_position(@position)
Copy link
Member

Choose a reason for hiding this comment

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

Should we find a way to make the mutex mandatory when calling scanner? Like:

@document.create_scanner(@global_state.mutex).find_char_position(@position)

And the scanner will do the mutax wrapping around find_char_position. This will making the requirement clear but also simplify the usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This PR will fix an existing bug server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants