-
Notifications
You must be signed in to change notification settings - Fork 1.3k
highlighter: Fix last open racy access in the *Highlight*() functions
#3242
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
Open
JoeKar
wants to merge
1
commit into
zyedidia:master
Choose a base branch
from
JoeKar:bugfix/highlighter-race
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But
h.lastRegionis modified also byhighlightRegion()andhighlightEmptyRegion(), which are also called from both goroutines, so we still have the race, right?I was actually thinking about removing this
lastRegionfield from theHighlighterstruct, making it just a local variable, passing it tohighlightRegion()andhighlightEmptyRegion()(like we already do), and modifyinghighlightRegion()andhighlightEmptyRegion()so that they don't set this globalh.lastRegionbut just return the updated value oflastRegionto the caller.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
h.lastRegionis touched by the re-highlighting too, but only in case it can obtain the lock, which is in the moment of async access toh.lastRegionheld by the initial highlighting. So the lines should be processed consistent each after an other.In case
h.lastRegionis problematic, then this would be valid for every other member inside theHighlighterstruct too, which is set inside the highlighting process. Maybe you saw it already...I extend this structure in my rework.I know. Regarding the usage of
h.lastRegionoutside ofh.highlight*()I'm on your side. It looks ugly and should be prevent, but I wasn't able to remove it without breaking anything. I will try to get it working in the rework first and then we can see if it's feasible for the old approach too, when the minimal invasive change here isn't enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the lock is held only while processing a single line.
h.lastRegionand unlocks the lockh.lastRegionand unlocks the lockh.lastRegionvalue set by rehighlighting for line 50, not for line 2000 .....Or am I missing something that ensures that this will not happen?
Hmm, no I didn't. (I still had no time to get really familiar with your #3127, nor with the original code inside
highlightRegion()andhighlightEmptyRegion(), for that matter.) Now I see you extended it. Ok, yes, ifh.lastRegionis problematic, those extended fields are problematic too, for the same reason. Well, why not just pack those extended fields into a "local state" structure, and pass this structure (or a pointer to it) between highlighter functions, within a single goroutine, instead of it being a global state shared between goroutines, - just like in the case oflastRegionalone in the approach I've been suggesting?Although I realize that this approach (passing of local
lastRegionor "local state" structure between functions) might be not as easy as it seems, sincehighlightRegion()andhighlightEmptyRegion()are recursive, so need to figure out what exactly to pass between those recursive calls...Sure, it's up to you what you do first. Maybe it's indeed better to focus on #3127 first, after all this race we are trying to tackle here is purely theoretical so far, we don't even know how to reproduce it, while #3127 fixes some actual observed issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I didn't oversee something, then
h.lastRegionshould be set before the usage again with the previous state.The better style will be applied in #3127.