Skip to content

Fix a case of concurrent access to KTextEditor, and add asserts to catch other such cases in the future

In fixitsForDiagnostic(), the code to get the original text for the replacement proposed by libclang called doc->text(docRange) without holding the foreground lock. Since that function is called from a worker thread, this means a potential concurrent access to a KTextEditor object with the main (Qt) thread, which may crash ( even if only accessed with concurrent reads, without writes. See: https://phabricator.kde.org/D23721 ). I think that this might be the reason for an infrequent crash that I observed, occurring about one or two times per month when using KDevelop daily.

This merge request changes the way in which the original text is retrieved. In order to avoid the need for locking, the text is now retrieved from libclang instead of the document.

In addition, to avoid cases where the foreground lock is forgotten in the future, asserts are added to some member functions of TextDocument which check for either the calling thread being the main thread, or the foreground lock being held.

Notes:

  • I am not sure which functions in TextDocument should all receive the assert.
  • I am not sure which encoding to use to convert the const char* buffer from libclang to a QString (currently fromLocal8Bit() is used).

Merge request reports