From 4374da616e1fa764b9850a24b1774caaf12af753 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Tue, 30 Jul 2013 11:18:37 -0700 Subject: [PATCH] Fix crash: posting Vim message from thread Vim is not thread-safe so posting a message to Vim from a non-GUI thread causes a crash *sometimes*. I was aware of this problem before, but didn't catch this instance of it in code review. Fixes #479. --- python/ycm/completers/cs/cs_completer.py | 7 +++---- python/ycm/vimsupport.py | 4 ++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/python/ycm/completers/cs/cs_completer.py b/python/ycm/completers/cs/cs_completer.py index 3570998c..f6ef8a1a 100755 --- a/python/ycm/completers/cs/cs_completer.py +++ b/python/ycm/completers/cs/cs_completer.py @@ -167,10 +167,9 @@ class CsharpCompleter( ThreadedCompleter ): try: response = urllib2.urlopen( target, parameters ) return json.loads( response.read() ) - except Exception as e: - if not silent: - vimsupport.PostVimMessage( - 'OmniSharp : Could not connect to ' + target + ': ' + str( e ) ) + except Exception: + # TODO: Add logging for this case. We can't post a Vim message because Vim + # crashes when that's done from a no-GUI thread. return None diff --git a/python/ycm/vimsupport.py b/python/ycm/vimsupport.py index 40082676..bd13041d 100644 --- a/python/ycm/vimsupport.py +++ b/python/ycm/vimsupport.py @@ -79,6 +79,10 @@ def NumLinesInBuffer( buffer ): def PostVimMessage( message ): + # TODO: Check are we on the main thread or not, and if not, force a crash + # here. This should make it impossible to accidentally call this from a + # non-GUI thread which *sometimes* crashes Vim because Vim is not thread-safe. + # A consistent crash should force us to notice the error. vim.command( "echohl WarningMsg | echomsg '{0}' | echohl None" .format( EscapeForVim( message ) ) )