Skip to content

cpptools memory explodes when workspace folders symlink to each other (pnpm workspace layout): 19-file repro, 1.35.6 and 1.36.0 #14891

Backlog

Description

@zhaoM0

Environment

  • OS and Version: macOS 27.2, Apple Silicon, 16 GB RAM
  • VS Code Version: 1.134.0
  • C/C++ Extension Version: 1.35.6 (latest release) and 1.36.0 (latest pre-release); both reproduce
  • If using SSH remote, specify OS of remote machine: not used

Bug Summary and Steps to Reproduce

Bug Summary: When a workspace contains directories that reach each other through symbolic links, the language server treats every chain of links as a separate set of files. That is the layout pnpm creates for workspace packages that depend on each other (packages/a/node_modules/@scope/b -> ../../../b). With 7 packages that link to each other, 15 real files become 27,399 files to parse. With 9 packages the cpptools process passes 5 GB within about two minutes. On a real pnpm monorepo it reached about 90 GB, was killed by the OS, was restarted by the extension, and did the same again, for hours.

This looks related to #14689 (fixed in 1.34.2 for symlinks that point to ancestor directories). Here no link points to an ancestor. The links point to sibling directories, so a cycle only closes after two or more hops. It may also be what the second reporter in #14856 is seeing.

Steps to reproduce:

  1. Save this as make-symlink-mesh.sh and run ./make-symlink-mesh.sh repro 9:
#!/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
  1. Open the repro folder in VS Code (fresh profile, no other extensions, default settings) and open main.c.
  2. Watch the memory of the cpptools process.

The generated workspace has 19 regular files, 38 real directories and 72 symlinks. Use N = 7 for a harmless version (about 110 MB). Be careful with N of 10 or more.

Expected behavior: Each real file is processed once. Memory is comparable to the same tree without links (about 30 MB).

Observed behavior:

Peak phys_footprint of the cpptools process. "Files to parse" is the total in the Parsing files (n of m) status that the server reports.

Workspace Regular files Symlinks Files to parse, as reported Peak memory 1.35.6 Peak memory 1.36.0
10 packages, --no-links (control) 21 0 21 30 MB 31 MB
N = 5 11 20 651 33 MB not measured
N = 6 13 30 3,913 48 MB not measured
N = 7 15 42 27,399 112 MB 113 MB
N = 8 17 56 219,201 653–734 MB 747 MB
N = 9 19 72 not seen (stopped earlier) over 3.1 GB after 2 min 45 s, still growing over 3.3 GB after 2 min, still growing

For N = 9 I killed the process at a 3 GB safety cap. In another run with the 1.36.0 binaries and a 5 GB cap, it spent the first 1 min 50 s in the Initializing (n of m) tag parser status and passed 5 GB after 2 min 8 s.

The reported totals are exactly 2 × P(N) + 1, where P(N) = N × Σₖ (N−1)! / (N−1−k)! is the number of paths through the link graph that do not visit the same package twice (2 files per package, plus main.c): 325, 1,956, 13,699 and 109,600 paths for N = 5 to 8. For N = 9 that is 986,409 paths, and for N = 10 almost 10 million.

Two more observations:

  • "C_Cpp.files.exclude": { "**/node_modules": true } avoids it completely (checked with 1.36.0 on the repro and with 1.35.6 on the real workspace described below).
  • Executing the C_Cpp.PauseParsing command stops the growth while parsing is paused (the server reports "Workspace parsing paused"). After C_Cpp.ResumeParsing the initialization starts over.

Configuration and Logs

No c_cpp_properties.json. All C_Cpp.* settings are at their defaults, except C_Cpp.loggingLevel for the log below.

Language server log for N = 7 on 1.36.0 with "C_Cpp.loggingLevel": "Debug". It contains 27,399 Parsed file (initial) lines, which resolve to the 15 real files. p3.c alone appears under 1,957 different paths. No path contains the same package twice, and the longest ones go through all 7:

Symbol index configuration: config=1025 new_config=1 added_files=27399 removed_files=0 changed_file_options=0 changed_forced_includes=0
Parsed file (initial): /tmp/cpptools-pr/repro7/main.c
Parsed file (initial): /tmp/cpptools-pr/repro7/packages/p0/src/p0.c
...
Parsed file (initial): /tmp/cpptools-pr/repro7/packages/p6/node_modules/@repro/p5/node_modules/@repro/p4/node_modules/@repro/p3/node_modules/@repro/p2/node_modules/@repro/p1/node_modules/@repro/p0/src/p0.c
Parsed file (initial): /tmp/cpptools-pr/repro7/packages/p6/node_modules/@repro/p5/node_modules/@repro/p4/node_modules/@repro/p3/node_modules/@repro/p2/node_modules/@repro/p0/node_modules/@repro/p1/src/p1.h

C/C++: Log Diagnostics for the same workspace:

-------- Diagnostics - 2026/12/2 00:33:20
Version: 1.36.0
Current Configuration:
{
    "name": "Mac",
    "includePath": [
        "/tmp/cpptools-pr/repro7/**"
    ],
    "defines": [],
    "compilerPath": "/usr/bin/clang",
    "cStandard": "gnu17",
    "cppStandard": "gnu++14",
    "intelliSenseMode": "macos-clang-arm64",
    "compilerPathInCppPropertiesJson": "/usr/bin/clang",
    "intelliSenseModeIsExplicit": false,
    "cStandardIsExplicit": false,
    "cppStandardIsExplicit": false,
    "mergeConfigurations": false,
    "recursiveIncludes": {},
    "recursiveIncludesPriorityIsExplicit": false,
    "recursiveIncludesOrderIsExplicit": false,
    "compilerPathIsExplicit": false,
    "browse": {
        "limitSymbolsToIncludedHeaders": true
    }
}
Modified Settings:
{
    "C_Cpp.loggingLevel": "Debug"
}
cpptools version (native): 1.36.0.0
Translation Unit Mappings:
[ /tmp/cpptools-pr/repro7/main.c - source TU]:
Translation Unit Configurations:
[ /tmp/cpptools-pr/repro7/main.c ]
    Process ID: 75302
    Memory Usage: 3 MB
    Compiler Path: /usr/bin/clang
    Language: C
    Standard Version: c17
    IntelliSense Mode: macos-clang-arm64
Total Memory Usage: 3 MB

(Shortened: tracked editor settings and the system include paths removed.)

Other Extensions

None. Everything above was measured in a fresh profile with only the C/C++ extension.

Additional context

Call stack. While N = 9 is in the Initializing status, sample shows one busy thread with this stack. It is the same in the real workspace described below (1.35.6, macOS arm64). No directory-reading system calls appear during the sample:

initialize_configuration_work_item_t::work(queue_e)
initialize_configuration_work_item_t::initialize_configuration()
workspace_folder_context::get_hash_code(long long*)
workspace_folder_context::get_intellisense_files(intellisense_files_info_t&, std::shared_ptr<msvc::cancel_state_t> const&)
workspace_folder_context::get_intellisense_files(...)::$_1::operator()(directory_cache const&, bool) const
directory_cache::traverse(directory_cache::visit_mode_e, bool, bool, bool, std::function<...>&&, std::shared_ptr<msvc::cancel_state_t> const&) const
  cpptools_core_system_host::file_type_flags_for_file(msvc::basic_zstring_view<char>)
    msvc::canonicalize_file_path(msvc::basic_zstring_view<char>)
  msvc::is_path_equal_or_under(...)
  msvc::flyweight_string_factory<char>::make(std::basic_string_view<char>)

What I think is happening. This part is inference, since I can only see the server from the outside. The traversal stops when a link leads back to a directory that is already on the current path, which is what #14689 needed and which is why no logged path contains a package twice. It does not seem to remember directories it has already visited through a different chain of links. Remembering visited directories by canonical path (or device and inode) for the whole traversal would make the work proportional to the number of real directories.

Real-world impact. This came from an ordinary multi-project folder (about 306,000 files) that contains one pnpm monorepo (about 107,000 files and 9,000 directory symlinks; its workspace packages depend on each other, some of them in cycles through dev dependencies). Opening any .c file activated the extension, and then:

  • cpptools grew by roughly 4 GB per minute at about 80% of one core. In a separate 26-second observation of that workspace on 1.36.0 it was in the Initializing (n of m) status and had reached 2.5 GB.
  • After about 23 minutes the kernel killed it: memorystatus: cpptools [pid] has greater than 50% of compressed memory (87082 MiB) followed by memorystatus: killing cpptools [pid] due to low-swap. The same pair of lines appears at 89719 MiB and 91411 MiB for two other rounds.
  • The extension logged "The language server crashed. Restarting..." and it started again, at least 8 times over 3.5 hours, including across a VS Code restart.

Second, smaller problem: the restart loop. The crash handler in Extension/src/LanguageServer/client.ts gives up only when 5 crashes fall within 3 minutes. A server that is killed for memory every 20 minutes never meets that condition, so it is restarted indefinitely. #14892 is a TypeScript-only change for that part. It does not touch the traversal.

Related (state as of 2026-12-02):

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    • Status
      Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions