Skip to content

Stop restarting the language server when it keeps crashing while initializing the workspace - #14892

Open
ZhaoXiaoLuo (zhaoM0) wants to merge 3 commits into
microsoft:mainfrom
zhaoM0:stuck-workspace-initialization
Open

ZhaoXiaoLuo (zhaoM0) wants to merge 3 commits into
microsoft:mainfrom
zhaoM0:stuck-workspace-initialization

Conversation

@zhaoM0

@zhaoM0 ZhaoXiaoLuo (zhaoM0) commented Dec 2, 2026 •

Copy link
Copy Markdown

Related issue: #14891

Motivation

The crash handler in client.ts stops 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. cpptools grew 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 on vscode). Tracks whether the server is in the workspace initialization phase and owns the crash history that used to be languageClientCrashTimes. "In the phase" means: workspace parsing is in progress, the last tag parse status is one of the four Initializing… templates of nativeStrings.json (not the later Initializing projects… ones), and parsing is neither paused nor requested to be paused.
  • Restart policy. After 3 crashes in a row in that phase the server is not restarted (failsafe mode, as for the existing rule). The message says why and points at C_Cpp.files.exclude. The 5-crashes-in-3-minutes rule is unchanged and takes precedence when both apply.
  • Notice (second commit, 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.ts only forwards events: the places in updateStatus that assign isParsingWorkspace, updateTagParseStatus, pauseParsing/resumeParsing, stopLanguageClient, the "reset UI state" block before a server starts, and the closed handler.

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

  • New: no restart after 3 crashes in a row during workspace initialization. A server that crashes quickly in that phase is therefore stopped at the 3rd crash instead of the 5th.
  • New: one information message per session after 2 minutes in that phase. A healthy workspace whose initialization really takes that long sees it too; the text only states the duration and suggests excluding folders.
  • Unchanged: crashes outside that phase, and crashes while parsing is paused or a pause has been requested. "Outside" starts 2 seconds after the server reports another status, see the notes.

What it does not cover

  • A workspace where enumeration finishes but produces an enormous file list. The 9-package repro is like that: it leaves the Initializing status after about 1 min 50 s and keeps growing in the parsing phase. A crash there is handled by the existing rule only.
  • Memory use itself. In my incident, three rounds would still have meant about an hour of memory pressure before the restarts stop. The 2-minute message is what can help earlier.

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 compile in Extension/) and the native binaries of the 1.36.0 release in Extension/bin/ (bin/* from cpptools-macOS-arm64.vsix). "main" is 1801fd0.

1. Create the workspaces. Save the two scripts below in /tmp/cpptools-pr, make them executable, then:

cd /tmp/cpptools-pr
./make-symlink-mesh.sh repro 9                     # 19 files, 72 symlinks: the problem case
./make-symlink-mesh.sh repro-nolinks 10 --no-links # 21 files, no symlinks: the control
make-symlink-mesh.sh: N tiny packages that link to each other the way pnpm workspace packages do
#!/usr/bin/env bash
# Usage: make-symlink-mesh.sh <dir> <N> [--no-links]
# Creates N tiny "packages". Each one links to every other one through
# node_modules/@repro/<name> -> ../../../<name>, as pnpm does for workspace packages that depend on each other.
set -euo pipefail
root=$1; n=$2; links=${3:-}
mkdir -p "$root/packages"
printf 'int main(void) { return 0; }\n' > "$root/main.c"
for i in $(seq 0 $((n-1))); do
  p="$root/packages/p$i"
  mkdir -p "$p/src" "$p/node_modules/@repro"
  printf '#pragma once\nint p%d(void);\n' "$i" > "$p/src/p$i.h"
  printf '#include "p%d.h"\nint p%d(void) { return %d; }\n' "$i" "$i" "$i" > "$p/src/p$i.c"
  if [ "$links" != "--no-links" ]; then
    for j in $(seq 0 $((n-1))); do
      [ "$i" = "$j" ] || ln -s "../../../p$j" "$p/node_modules/@repro/p$j"
    done
  fi
done
kill-server-loop.sh: a stand-in for the OOM killer

The 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.

#!/usr/bin/env bash
# Usage: kill-server-loop.sh <absolute path of Extension/bin/cpptools> <uptime in seconds before each kill> [max kills]
srv=$1; uptime=$2; max=${3:-0}; n=0; idle=0
running() { pgrep -f "^$srv( |\$)" | head -1; }
while [ "$idle" -lt 90 ]; do
  pid=$(running)
  if [ -z "$pid" ]; then sleep 1; idle=$((idle+1)); continue; fi
  idle=0; sleep "$uptime"
  kill -9 "$pid" 2>/dev/null && n=$((n+1)) && echo "$(date +%T) kill #$n (pid $pid)"
  if [ "$max" -gt 0 ] && [ "$n" -ge "$max" ]; then
    sleep 10
    if [ -n "$(running)" ]; then echo "$(date +%T) restarted after kill #$n as well; ending the experiment here"; else echo "$(date +%T) not restarted after kill #$n"; fi
    exit 0
  fi
  sleep 3
done
echo "$(date +%T) no language server for 90 s after $n kills"

With repro open, 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 -c

The grep prints 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
00:37:55 kill #1 (pid 81027)
00:38:58 kill #2 (pid 82139)
00:40:01 kill #3 (pid 83277)
00:41:04 kill #4 (pid 84375)
00:42:07 kill #5 (pid 85524)
00:43:10 kill #6 (pid 86627)
00:43:20 restarted after kill #6 as well; ending the experiment here
   6 [Error] Server process exited with signal SIGKILL.
   6 [Error] The language server crashed. Restarting...
Output on this branch: not restarted after the 3rd kill
11:27:10 kill #1 (pid 24527)
11:28:13 kill #2 (pid 25588)
11:29:16 kill #3 (pid 26808)
11:30:53 no language server for 90 s after 3 kills
   3 [Error] Server process exited with signal SIGKILL.
   1 [Error] The language server crashed 3 times in a row while initializing the workspace. It will not be restarted. If the workspace is very large or contains symbolic links that lead back into it, excluding folders with the "C_Cpp.files.exclude" setting may help.
   2 [Error] The language server crashed. Restarting...

3. The 2-minute message. The 9-package workspace leaves the Initializing status 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 the grep prints nothing. On this branch it prints the line that is also shown as an information message:

The C/C++ extension has been initializing this workspace for more than 2 minutes. If the workspace is very large or contains symbolic links that lead back into it, excluding folders with the "C_Cpp.files.exclude" setting may help.

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 6 on repro-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

  • Unit tests: 62 new tests in test/unit/workspaceInitializationMonitor.test.ts and test/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 is Command Lines [Posix] should parse "one $var", which also fails for me on main (264 passing, 1 failing).
  • eslint and tsc --build are clean. The first commit builds and passes its tests on its own.
  • End-to-end: a script does what steps 2 to 4 do with more control. It reads the status notifications the extension receives (through a logging line added to the compiled client.js of 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
Scenario A (main) B (this branch)
E1 repro; SIGKILL each time the server has been up 60 s (it is in Initializing) restarted after each of 6 kills over 309 s not restarted after kill 3, new message; host responds; opening another .c file 20 s later starts no server; nothing else happens in the next 150 s
E5 repro; SIGKILL as soon as the server reports Initializing (about 4 s uptime) not restarted after kill 5, existing message not restarted after kill 3, new message
E4 control; SIGKILL right after "Parsing done" not restarted after kill 5, existing message same
E3 control; SIGKILL after "Parsing done", 60 s apart, 3 times restarted 3 times restarted 3 times
E6 multi-root: control folder + repro folder; SIGKILL at 60 s uptime restarted after each of 5 kills not restarted after kill 3; same with the folder order reversed
E2 repro; SIGSTOP 15 s after Initializing is reported no output line in 132 s output line 2 minutes after the phase was entered (measured 120.1 s)
E8 repro; C_Cpp.PauseParsing 5 s into the phase, C_Cpp.ResumeParsing 130 s later nothing while paused (the server reports isPaused); output line 2 minutes after the phase is entered again (measured 121.2 s; the script polls about once a second)
E7 after E1, same profile: add "C_Cpp.files.exclude": {"**/node_modules": true} and reopen reaches "Parsing done", peak 31 MB; one SIGKILL afterwards is followed by a restart
E7b after E7, same profile: remove the setting and reopen not restarted after kill 3, as in a new session
E9 8-package workspace with --locale=en and with --locale=zh-cn the status payloads carry the English templates in both

Not 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

  • The thresholds (3 crashes, 2 minutes, and the 2-second grace period below) are judgment calls.
  • The phase is recognized by the unformatted English text of the tag parse status notification. I did not use stringId because it depends on the order of nativeStrings.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.
  • The server sends the C_Cpp: Parsing status and its first Initializing (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.
  • When files change during the phase, the server occasionally reports 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.
  • The monitor's isInitializingWorkspace is not the same thing as model.isInitializingWorkspace (which is the time before C_Cpp: Parsing). I am happy to rename it.

🤖 Generated with Claude Code

ZhaoXiaoLuo (zhaoM0) and others added 2 commits December 2, 2026 10:56
…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>
Comment thread Extension/CHANGELOG.md Resolved
@zhaoM0

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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>
@zhaoM0

Copy link
Copy Markdown
Author

Thanks Sean McManus (@sean-mcmanus), removed in 298be28. The PR no longer touches CHANGELOG.md.

}
}

this.consecutiveInitializationCrashes = crashedDuringInitialization ? this.consecutiveInitializationCrashes + 1 : 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Pull Request

Development

Successfully merging this pull request may close these issues.

3 participants