Repository navigation
Stop restarting the language server when it keeps crashing while initializing the workspace - #14892
ZhaoXiaoLuo (zhaoM0) wants to merge 3 commits into
Conversation
…ializing the workspace The crash handler gives up only when 5 crashes fall within 3 minutes. A language server that never finishes enumerating the workspace grows until the OS terminates it, which can take much longer than that, so it was restarted indefinitely. Track whether the server is in the workspace initialization phase (workspace parsing in progress, last tag parse status is one of the "Initializing" statuses, parsing not paused) and stop restarting after 3 consecutive crashes in that phase. The existing rule is unchanged and takes precedence when both apply. When the server stays in the phase for 2 minutes, a line is written to the C/C++ output channel. The logic lives in a module without a dependency on vscode so that it can be unit tested with the notification sequences recorded from the language server. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…kspace for 2 minutes Stopping the restarts only helps after the language server has crashed several times, which can take a long time. Tell the user earlier that excluding folders may help, in the same way as the existing message about workspaces with a very large number of files. The message is shown once per session and has its own "Don't Show Again" state. Every occurrence is still written to the C/C++ output channel. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the initialization-crash safeguards and notice changes.
No open issues 🎉
What changed in this PR
Adds safeguards against repeated C/C++ language-server crashes during workspace initialization, mitigating #14891 without fixing the underlying native memory growth.
Changes:
- Stops restarts after three consecutive initialization crashes while preserving the existing crash-frequency rule.
- Adds a two-minute initialization notice with documentation and workspace-level suppression.
- Adds unit tests for phase tracking, crash handling, timers, and notice behavior.
| File | Description |
|---|---|
| Extension/test/unit/workspaceInitializationMonitor.test.ts | Tests initialization tracking and restart policy. |
| Extension/test/unit/stuckInitializationNotice.test.ts | Tests notice actions and suppression. |
| Extension/src/LanguageServer/workspaceInitializationMonitor.ts | Tracks initialization, timers, and crash history. |
| Extension/src/LanguageServer/stuckInitializationNotice.ts | Manages logging and once-per-session notices. |
| Extension/src/LanguageServer/client.ts | Connects server events, restart decisions, and notifications. |
| Extension/CHANGELOG.md | Documents the notice and restart limit. |
Review effort: Balanced
The changelog is updated in a separate pull request per release. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Thanks Sean McManus (@sean-mcmanus), removed in 298be28. The PR no longer touches CHANGELOG.md. |
| } | ||
| } | ||
|
|
||
| this.consecutiveInitializationCrashes = crashedDuringInitialization ? this.consecutiveInitializationCrashes + 1 : 0; |
There was a problem hiding this comment.
✨Copilot (devbox7-wsl/agent37): [Moderate] Should the streak reset after the replacement finishes a workspace parsing cycle? The counter resets only on a crash outside initialization, so it survives a successful Parsing done. If a later cycle in the same window can re-enter initialization, three widely separated crashes can switch the clients to failsafe for the session even though each recovery completed. Could you confirm that case and add a test for an initialization crash, successful parsing, then a later initialization crash, resetting the streak on success if appropriate?
Related issue: #14891
Motivation
The crash handler in
client.tsstops restarting the language server only when 5 crashes fall within 3 minutes. A server that dies while it is still initializing the workspace usually takes longer than that to die, so it is restarted indefinitely.This happened to me with 1.35.6 on macOS, in a workspace that contains a pnpm monorepo.
cpptoolsgrew to about 90 GB, was killed by the kernel after 23 minutes (memorystatus: killing cpptools [pid] due to low-swap), was restarted by the extension, and did the same again: at least 8 rounds in 3.5 hours. The only trace was "The language server crashed. Restarting..." in the output channel.The memory growth itself is in the native language server: folders that symlink to each other (
packages/a/node_modules/@scope/b -> ../../../b, the layout pnpm creates for workspace packages that depend on each other) are processed once per chain of links. With 7 such packages, 15 real files become 27,399 files to parse; with 9 packages the process passes 5 GB in about two minutes. That is #14891, and this PR does not fix it. It makes the extension stop feeding the loop and tell the user what to try.Changes
workspaceInitializationMonitor.ts(new, no dependency onvscode). Tracks whether the server is in the workspace initialization phase and owns the crash history that used to belanguageClientCrashTimes. "In the phase" means: workspace parsing is in progress, the last tag parse status is one of the fourInitializing…templates ofnativeStrings.json(not the laterInitializing projects…ones), and parsing is neither paused nor requested to be paused.C_Cpp.files.exclude. The 5-crashes-in-3-minutes rule is unchanged and takes precedence when both apply.stuckInitializationNotice.ts). After 2 minutes in that phase, a line is written to the C/C++ output channel and an information message is shown once per session, with "Learn More" (the link from Warn users about too many files processed in the workspace #13889) and "Don't Show Again" (its own workspace state key).client.tsonly forwards events: the places inupdateStatusthat assignisParsingWorkspace,updateTagParseStatus,pauseParsing/resumeParsing,stopLanguageClient, the "reset UI state" block before a server starts, and theclosedhandler.The first commit works without the second; without it the 2-minute event only writes the line to the C/C++ output channel.
Behavior changes
What it does not cover
Initializingstatus after about 1 min 50 s and keeps growing in the parsing phase. A crash there is handled by the existing rule only.How to reproduce
Run on macOS 27.2 (Apple Silicon), VS Code 1.134.0, Node 24.11.0, with the extension built from source (
yarn install,yarn compileinExtension/) and the native binaries of the 1.36.0 release inExtension/bin/(bin/*fromcpptools-macOS-arm64.vsix). "main" is 1801fd0.1. Create the workspaces. Save the two scripts below in
/tmp/cpptools-pr, make them executable, then:make-symlink-mesh.sh: N tiny packages that link to each other the way pnpm workspace packages dokill-server-loop.sh: a stand-in for the OOM killerThe real thing took 23 minutes and 90 GB per round. This sends SIGKILL to the language server each time it has been running for a given time, and ends when no new server shows up for 90 seconds.
With
reproopen, the server is at 1 to 3 GB after a minute and passes 5 GB after about two minutes, so do not leave it running unattended. #14891 has the details of what the server does with this workspace.2. The restart loop. In
Extension/of the build under test, with a fresh profile:code --user-data-dir /tmp/cpptools-pr/profile --extensions-dir /tmp/cpptools-pr/exts --disable-workspace-trust \ --extensionDevelopmentPath="$PWD" /tmp/cpptools-pr/repro /tmp/cpptools-pr/repro/main.c /tmp/cpptools-pr/kill-server-loop.sh "$PWD/bin/cpptools" 60 6 grep -rhE "language server|Server process exited" /tmp/cpptools-pr/profile/logs/*/window1/exthost/output_logging_*/ \ | sed -E 's/\[Error - [^]]+\]/[Error]/' | sort | uniq -cThe
grepprints what the extension wrote to the "cpptools" output channel. Close the window afterwards; on main the server is still running then.Output on main: restarted after each of 6 kills
Output on this branch: not restarted after the 3rd kill
3. The 2-minute message. The 9-package workspace leaves the
Initializingstatus just before the 2 minutes are over, and a bigger one needs more memory than is comfortable. So the server is frozen with SIGSTOP once it is in that status: the extension then sees a server that stays in the phase and reports nothing more, and memory stays where it was.code --user-data-dir /tmp/cpptools-pr/profile2 --extensions-dir /tmp/cpptools-pr/exts2 --disable-workspace-trust \ --extensionDevelopmentPath="$PWD" /tmp/cpptools-pr/repro /tmp/cpptools-pr/repro/main.c until pid=$(pgrep -f "^$PWD/bin/cpptools( |\$)" | head -1); [ -n "$pid" ]; do sleep 1; done sleep 20; kill -STOP "$pid" sleep 125 grep -rh "initializing this workspace" /tmp/cpptools-pr/profile2/logs/*/window1/exthost/output_logging_*/Then close the window and
kill -9 "$pid". On main thegrepprints nothing. On this branch it prints the line that is also shown as an information message:4. The existing rule, for comparison. Step 2 with the control workspace and a kill every 12 seconds (
kill-server-loop.sh "$PWD/bin/cpptools" 12 6onrepro-nolinks). Both builds stop after the 5th kill with "The language server crashed 5 times in the last 3 minutes. It will not be restarted."I ran these commands as written on both builds, except that I started VS Code in the background with
open -g -n -a "Visual Studio Code" --args …and the same arguments.Validation
test/unit/workspaceInitializationMonitor.test.tsandtest/unit/stuckInitializationNotice.test.ts. The monitor is driven with notification sequences recorded from the 1.36.0 language server (small folder, single file, healthy multi-root, slow but finite, runaway single-root and multi-root, runaway with file activity), plus transitions, timers, pause, server lifetime and the crash policy.mocha "dist/test/unit/**/*.test.js": 326 passing, 1 failing. The failing test isCommand Lines [Posix] should parse "one $var", which also fails for me on main (264 passing, 1 failing).eslintandtsc --buildare clean. The first commit builds and passes its tests on its own.client.jsof both builds), so it can kill the server at a known point, and it loads a test-only extension that writes a heartbeat, so that "the extension host still responds" is observed and not assumed. A is main, B is this branch; everything else is identical.End-to-end results
repro; SIGKILL each time the server has been up 60 s (it is inInitializing).cfile 20 s later starts no server; nothing else happens in the next 150 srepro; SIGKILL as soon as the server reportsInitializing(about 4 s uptime)reprofolder; SIGKILL at 60 s uptimerepro; SIGSTOP 15 s afterInitializingis reportedrepro;C_Cpp.PauseParsing5 s into the phase,C_Cpp.ResumeParsing130 s laterisPaused); output line 2 minutes after the phase is entered again (measured 121.2 s; the script polls about once a second)"C_Cpp.files.exclude": {"**/node_modules": true}and reopen--locale=enand with--locale=zh-cnNot covered: other platforms; a real out-of-memory kill (SIGKILL stands in for it, SIGSTOP for a server that stops reporting); the information message and its two buttons on screen (covered by unit tests with injected UI functions, while the end-to-end runs check the output channel line written by the same callback); whether "Don't Show Again" persists under its own key across a reload (verified by reading the code only); the case where the 2-minute timer and the 2-second grace timer are due in the same millisecond; the existing scenario tests, which I did not run. I did not add a scenario test because the behavior only shows when the server process is killed.
Notes for reviewers
textof the tag parse status notification. I did not usestringIdbecause it depends on the order ofnativeStrings.json. If the templates are renamed, the new behavior silently turns off and everything works as before. A dedicated notification from the server would be more robust; I could not add one from here.C_Cpp: Parsingstatus and its firstInitializing (n of m)status 0.1 to 0.4 s apart and then nothing for a long time, so the monitor does not depend on which of the two arrives first.Parsing files (n of n)and, in the same tenth of a second,Initializing (n of n)again (once in about 40 file operations in my runs). Another status therefore ends the phase only when it lasts 2 seconds. Until then the phase counts as not left: a crash in those 2 seconds counts as a crash in the phase, and the 2-minute message can still appear.isInitializingWorkspaceis not the same thing asmodel.isInitializingWorkspace(which is the time beforeC_Cpp: Parsing). I am happy to rename it.🤖 Generated with Claude Code