This is an archive of the discontinued LLVM Phabricator instance.

[clangd][SwapIndex] ensure that the old index is alive while we are using it via the function returned by `SwapIndex::indexedFiles()` call
ClosedPublic

Authored by ArcsinX on Jan 21 2021, 11:20 PM.

Details

Summary

Without this patch the old index could be freed, but there still could be tries to access it via the function returned by SwapIndex::indexedFiles() call.
This leads to hard to reproduce clangd crashes at code completion.
This patch keeps the old index alive until the function returned by SwapIndex::indexedFiles() call is alive.

Diff Detail

Event Timeline

ArcsinX created this revision.Jan 21 2021, 11:20 PM
ArcsinX requested review of this revision.Jan 21 2021, 11:20 PM
ArcsinX updated this revision to Diff 318442.Jan 22 2021, 12:28 AM

Fix format

sammccall accepted this revision.Jan 22 2021, 1:30 AM

Thanks, I'd seen these crashes but not manage to track down yet!

LG with one fix

clang-tools-extra/clangd/index/Index.cpp
81–87

Calling snapshot() twice introduces a race - we may keep the wrong one alive.

Evaluate snapshot first into a variable, then call indexedFiles, then move both into the lambda?

This revision is now accepted and ready to land.Jan 22 2021, 1:30 AM
ArcsinX updated this revision to Diff 318470.Jan 22 2021, 2:21 AM

Call snapshot() only once to avoid possible race.

ArcsinX marked an inline comment as done.Jan 22 2021, 2:22 AM
ArcsinX updated this revision to Diff 318474.Jan 22 2021, 2:48 AM

Fix format

sammccall accepted this revision.Jan 22 2021, 5:17 AM