Skip to content

fix(cpp): survive clangd crashes and degenerate ranges during parse#200

Merged
wzekin merged 1 commit into
cloudwego:mainfrom
wzekin:fix/cpp-lsp-crash-resilience
Jun 17, 2026
Merged

fix(cpp): survive clangd crashes and degenerate ranges during parse#200
wzekin merged 1 commit into
cloudwego:mainfrom
wzekin:fix/cpp-lsp-crash-resilience

Conversation

@wzekin

@wzekin wzekin commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

What type of PR is this?

fix

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo

(Optional) Translate the PR title into Chinese.

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:
Make large-workspace C++ parsing resilient to clangd faults instead of aborting the whole run on a single failure:

  • lsp/client.go: auto-restart the LSP server (C++ only) when the jsonrpc2 connection dies (clangd segfaults on pathological template typeHierarchy). Generation-counter guarding lets many goroutines observe the same dead conn but only one respawns; cached docs are marked not-opened so DidOpen re-notifies the fresh server. Call/Notify detect conn-closed and trigger it.
  • collect.go/export.go: runSafe() recovers per-symbol panics ("崩了就跳过") so one bad symbol is skipped, not fatal; wrap Collect/Export loops.
  • lsp_methods.go/utils.go: guard degenerate (inverted/out-of-bounds) clangd ranges in Locate/GetDistance/RelativePostionWithLines to avoid slice panics.
  • collect.go: collectCppBasesViaAST resolves dependent template bases (class Foo : public Tmpl<X,Y>) from textDocument/ast, which typeHierarchy cannot expose; cppSymbolAST adds a per-symbol AST path for .cpp out-of-line definitions. typeHierarchy crashes are counted/logged, not disabled.
  • cpp/lib.go: enable clangd --background-index for a persisted cross-TU index that survives restarts and speeds the per-symbol query storm.
    zh(optional):

(Optional) Which issue(s) this PR fixes:

(optional) The PR that updates user documentation:

Make large-workspace C++ parsing resilient to clangd faults instead of
aborting the whole run on a single failure:

- lsp/client.go: auto-restart the LSP server (C++ only) when the jsonrpc2
  connection dies (clangd segfaults on pathological template typeHierarchy).
  Generation-counter guarding lets many goroutines observe the same dead
  conn but only one respawns; cached docs are marked not-opened so DidOpen
  re-notifies the fresh server. Call/Notify detect conn-closed and trigger it.
- collect.go/export.go: runSafe() recovers per-symbol panics ("崩了就跳过")
  so one bad symbol is skipped, not fatal; wrap Collect/Export loops.
- lsp_methods.go/utils.go: guard degenerate (inverted/out-of-bounds) clangd
  ranges in Locate/GetDistance/RelativePostionWithLines to avoid slice panics.
- collect.go: collectCppBasesViaAST resolves dependent template bases
  (class Foo : public Tmpl<X,Y>) from textDocument/ast, which typeHierarchy
  cannot expose; cppSymbolAST adds a per-symbol AST path for .cpp out-of-line
  definitions. typeHierarchy crashes are counted/logged, not disabled.
- cpp/lib.go: enable clangd --background-index for a persisted cross-TU index
  that survives restarts and speeds the per-symbol query storm.
@wzekin wzekin merged commit 535eb66 into cloudwego:main Jun 17, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants