Skip to content

Commit 4b3df69

Browse files
committed
Handle possible errors from RangeVisibleInBuffer()
First and more common error is that by the time we execute buffer = vim.buffers[ bufnr ] the buffer might not be there any more. This is because `RangeVisibleInBuffer()` is called asynchronously and the user may bwipeout a buffer in between polls. This regularly happens in our vim tests. In such a case, we get a nasty traceback from `vimsupport` module. The solution is to catch the KeyError and return None. However, `ScrollingBufferRange()` also was not ready to handle None values from `RangeVisibleInBuffer()`, even though `RangeVisibleInBuffer()` could return None even before, if a visible window for `bufnr` can not be found.
1 parent bf0dbea commit 4b3df69

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

python/ycm/scrolling_range.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ def Request( self, force=False ):
5555
# - look up the actual visible range, then call this function
5656
# - if not overlapping, do the factor expansion and request
5757
self._last_requested_range = vimsupport.RangeVisibleInBuffer( self._bufnr )
58+
# If this is false, either the self._bufnr is not a valid buffer number or
59+
# the buffer is not visible in any window.
60+
# Since this is called asynchronously, a user may bwipeout a buffer with
61+
# self._bufnr number between polls.
62+
if self._last_requested_range is None:
63+
return False
64+
5865
self._tick = vimsupport.GetBufferChangedTick( self._bufnr )
5966

6067
# We'll never use the last response again, so clear it

python/ycm/vimsupport.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ class Range:
206206
start: Location = Location()
207207
end: Location = Location()
208208

209-
buffer = vim.buffers[ bufnr ]
209+
try:
210+
buffer = vim.buffers[ bufnr ]
211+
except KeyError:
212+
return None
210213

211214
if not windows:
212215
return None

0 commit comments

Comments
 (0)