Skip to content

[clangd] Explicitly block until async task completes #130077

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

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,18 +460,24 @@ void ClangdServer::codeComplete(PathRef File, Position Pos,
CodeCompleteResult Result = clangd::codeComplete(
File, Pos, IP->Preamble, ParseInput, CodeCompleteOpts,
SpecFuzzyFind ? &*SpecFuzzyFind : nullptr);
// We don't want `codeComplete` to wait for the async call if it doesn't use
// the result (e.g. non-index completion, speculation fails), so that `CB`
// is called as soon as results are available.
{
clang::clangd::trace::Span Tracer("Completion results callback");
CB(std::move(Result));
}
if (SpecFuzzyFind && SpecFuzzyFind->NewReq) {
if (!SpecFuzzyFind)
return;
if (SpecFuzzyFind->NewReq) {
std::lock_guard<std::mutex> Lock(CachedCompletionFuzzyFindRequestMutex);
CachedCompletionFuzzyFindRequestByFile[File] = *SpecFuzzyFind->NewReq;
}
// SpecFuzzyFind is only destroyed after speculative fuzzy find finishes.
// We don't want `codeComplete` to wait for the async call if it doesn't use
// the result (e.g. non-index completion, speculation fails), so that `CB`
// is called as soon as results are available.
// Explicitly block until async task completes, this is fine as we've
// already provided reply to the client and running as a preamble task
// (i.e. won't block other preamble tasks).
if (SpecFuzzyFind->Result.valid())
SpecFuzzyFind->Result.wait();
};

// We use a potentially-stale preamble because latency is critical here.
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clangd/CodeComplete.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ struct SpeculativeFuzzyFind {
/// Set by `codeComplete()`. This can be used by callers to update cache.
std::optional<FuzzyFindRequest> NewReq;
/// The result is consumed by `codeComplete()` if speculation succeeded.
/// NOTE: the destructor will wait for the async call to finish.
std::future<std::pair<bool /*Incomplete*/, SymbolSlab>> Result;
};

Expand Down
Loading