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

Merged
merged 2 commits into from
Jan 6, 2025

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
@vinistock vinistock force-pushed the 12-11-locate_targets_under_mutex_lock branch from 6980656 to b51049e Compare January 6, 2025 20:08
@vinistock vinistock requested a review from st0012 January 6, 2025 20:09
@vinistock
Copy link
Member Author

@st0012 I made a new attempt at this one and split by commit. I realized that documents needed more than one piece of the global state (encoding + mutex), so we can actually pass the state down without making it too messy. The benefit is that now there's no way to locate targets bypassing the lock, as you had suggested.

Let me know what you think!

Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@vinistock vinistock force-pushed the 12-11-locate_targets_under_mutex_lock branch from b51049e to a227dd4 Compare January 6, 2025 22:49
@vinistock vinistock merged commit 413eaea into main Jan 6, 2025
42 checks passed
Copy link
Member Author

Merge activity

  • Jan 6, 6:14 PM EST: A user merged this pull request with Graphite.

@vinistock vinistock deleted the 12-11-locate_targets_under_mutex_lock branch January 6, 2025 23:14
vinistock added a commit that referenced this pull request Jan 9, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
### Motivation

Closes #3027

The deadlock bug was introduced in #2976. Essentially, if the editor opens a URI for a non-existing file, we try to acquire the same lock twice.

Once [here](https://github.com/Shopify/ruby-lsp/blob/17d5fc733b758bbbedaf36fb17ac3914548086bf/lib/ruby_lsp/base_server.rb#L53) to verify if the request requires a document to be parsed and the second time [here](https://github.com/Shopify/ruby-lsp/blob/17d5fc733b758bbbedaf36fb17ac3914548086bf/lib/ruby_lsp/store.rb#L68) when we read the file from disk and save it in the store.

Trying to acquire the same mutex lock twice raises the deadlock error.

### Implementation

I stopped synchronizing anything inside basic store operations. It just increases the chance of mistakes like these. I moved the responsibility of synchronizing back to the server.

### Automated Tests

To catch this regression, we need a client/server integration test because the first synchronization happens only by reading a request from the STDIN pipe. I added one to ensure we don't hit the same issue again.
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.

None yet

2 participants