llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.91k stars 11.52k forks source link

[clangd] large memory leak in 17.0.3 #75105

Open jswillard opened 9 months ago

jswillard commented 9 months ago

I use clangd from the vscode-clangd extension and noticed a memory leak when I open specific files. Within just a couple minutes clangd will use up 50gb of memory and I get a full system crash shortly thereafter.

I wasn't able to create a simple reproducible but I did find that it has something to do with precompiled headers. If I disable pchs, the issue went away. Also, it only happens when I target iOS, targeting macOS shows no issues.

If there's anything I should look at that would help in investing, lmk. Thanks for you time.

"/Users/john/Library/Application Support/Code/User/globalStorage/llvm-vs-code-extensions.vscode-clangd/install/17.0.3/clangd_17.0.3/bin/clangd" --version
clangd version 17.0.3 (https://github.com/llvm/llvm-project 888437e1b60011b8a375dd30928ec925b448da57)
Features: mac+grpc+xpc
Platform: arm64-apple-darwin23.1.0; target=x86_64-apple-darwin23.1.0
clang --version
Apple clang version 15.0.0 (clang-1500.1.0.2.5)
Target: arm64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
cmake --version
cmake version 3.28.0

CMake suite maintained and supported by Kitware (kitware.com/cmake).
VSCode
Version: 1.85.0
Commit: af28b32d7e553898b2a91af498b1fb666fdebe0c
Date: 2023-12-06T18:17:50.719Z
Electron: 25.9.7
ElectronBuildId: 25551756
Chromium: 114.0.5735.289
Node.js: 18.15.0
V8: 11.4.183.29-electron.0
OS: Darwin arm64 23.1.0
llvmbot commented 9 months ago

@llvm/issue-subscribers-clangd

Author: John Willard (jswillard)

I use clangd from the vscode-clangd extension and noticed a memory leak when I open specific files. Within just a couple minutes clangd will use up 50gb of memory and I get a full system crash shortly thereafter. I wasn't able to create a simple reproducible but I did find that it has something to do with precompiled headers. If I disable pchs, the issue went away. Also, it only happens when I target iOS, targeting macOS shows no issues. If there's anything I should look at that would help in investing, lmk. Thanks for you time. ``` "/Users/john/Library/Application Support/Code/User/globalStorage/llvm-vs-code-extensions.vscode-clangd/install/17.0.3/clangd_17.0.3/bin/clangd" --version clangd version 17.0.3 (https://github.com/llvm/llvm-project 888437e1b60011b8a375dd30928ec925b448da57) Features: mac+grpc+xpc Platform: arm64-apple-darwin23.1.0; target=x86_64-apple-darwin23.1.0 ``` ``` clang --version Apple clang version 15.0.0 (clang-1500.1.0.2.5) Target: arm64-apple-darwin23.1.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin ``` ``` cmake --version cmake version 3.28.0 CMake suite maintained and supported by Kitware (kitware.com/cmake). ``` ``` VSCode Version: 1.85.0 Commit: af28b32d7e553898b2a91af498b1fb666fdebe0c Date: 2023-12-06T18:17:50.719Z Electron: 25.9.7 ElectronBuildId: 25551756 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Darwin arm64 23.1.0 ```
kadircet commented 9 months ago

hi @jswillard , what does it mean to disable pchs? clangd builds its own preambles, and there is no mode in which it can work without them. can you provide clangd logs at least, so that maintainers can at least see the compile flags being used?

jswillard commented 9 months ago

what does it mean to disable pchs?

We use the target_precompile_headers detective from cmake. By commenting out that directive in the relevant CMakeLists.txt and regenerating compile_commands.json, clangd works as expected. With target_precompile_headers enabled, clangd leaks memory when analyzing SetecState.h.

Here are logs from the moment I opened the offending document SetecState.h until I had to kill -9 clangd to avoid a system crash.

I[11:02:19.987] Reloaded compilation database from /Users/john/ars/build/compile_commands.json
I[11:02:19.987] <-- textDocument/semanticTokens/full(1143)
I[11:02:19.988] ASTWorker building file /Users/john/ars/src/apps/gear/SetecState.h version 1 with command inferred from /Users/john/ars/src/apps/gear/SetecVisualizer.cpp
[/Users/john/ars/build]
/usr/bin/clang++ --driver-mode=g++ --target=arm64-apple-ios17.0 -DAL_LIBTYPE_STATIC -DARS_DISABLE_CHROMIUM -DARS_WITH_CMAKE=1 -DBUILDING_PYKLV -DCMS_NO_REGISTER_KEYWORD -DCN_API_STATIC -DCPPREST_FORCE_HTTP_CLIENT_ASIO -DCPPREST_FORCE_HTTP_LISTENER_ASIO -DCURL_STATICLIB -DLIBKLV_STATIC -DNNG_STATIC_LIB -DONLY_C_LOCALE=0 -DOPENCV_DISABLE_EIGEN_TENSOR_SUPPORT -DURI_STATIC_BUILD -DUSE_OS_TZDB=1 -D_FILE_OFFSET_BITS=64 -D_TURN_OFF_PLATFORM_STRING -I/Users/john/ars/src/apps/gear -I/Users/john/ars/src/apps/gear/concave -I/Users/john/ars/src/athena/core0/.. -I/Users/john/ars/src/athena/core1/.. -I/Users/john/ars/src/athena/can/.. -I/Users/john/ars/src/athena/cnlib/core/../.. -I/Users/john/ars/src/athena/debug/.. -I/Users/john/ars/src/athena/json/.. -I/Users/john/ars/ext -I/Users/john/ars/ext/MXML/.. -I/Users/john/ars/scripts/cmake_modules/../../ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/lib/pkgconfig/../../include/fribidi -I/Users/john/ars/ext/dxvk/include/native/directx -I/Users/john/ars/ext/dxvk/include/native/windows -I/Users/john/ars/src/athena/cnlib/scenepipe/../.. -I/Users/john/ars/src/athena/cnlib/rendervk/vez -I/Users/john/ars/scripts/cmake_modules/../../ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/spirv/1.0 -I/Users/john/ars/ext/DirectXTex/DirectXTex -I/Users/john/ars/ext/DirectXTex -I/Users/john/ars/ext/DirectXTex/DirectXTexLinux -I/Users/john/ars/ext/DirectXTex/../dxvk/include/native/directx -I/Users/john/ars/ext/DirectXTex/../dxvk/include/native/windows -I/Users/john/ars/ext/DirectXTex/../dxvk/include/com -I/Users/john/ars/src/athena -I/Users/john/ars/src/earthscapelib -I/Users/john/ars/ext/agg/include -I/Users/john/ars/scripts/cmake_modules/../../ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/minizip -I/Users/john/ars/src/athena/esapplibs/esappcommon/.. -I/Users/john/ars/src/athena/esapplibs/esrender -I/Users/john/ars/src/athena/esapplibs/esfilter -I/Users/john/ars/ext/gmscript/src/gm -I/Users/john/ars/ext/gmscript/src/binds -I/Users/john/ars/src/graphlib -I/Users/john/ars/src/athena/sockets/.. -I/Users/john/ars/src/athena/ip/.. -I/Users/john/ars/src/graphlib/nucleus/.. -I/Users/john/ars/src/athena/opencv/.. -I/Users/john/ars/scripts/cmake_modules/../../ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/opencv2 -I/Users/john/ars/src/athena/tsapplibs/tsplayback/.. -I/Users/john/ars/src/athena/applibs -I/Users/john/ars/src/athena/klv_churchill/.. -I/Users/john/ars/src/athena/esapplibs/esprojectorfx -I/Users/john/ars/src/athena/esapplibs/esprojectorfx/.. -I/Users/john/ars/src/motion_control_suite/.. -I/Users/john/ars/src/coordinate_suite/.. -I/Users/john/ars/src -I/Users/john/ars/src/athena/laser_control/.. -I/Users/john/ars/src/image_processing_suite/.. -I/Users/john/ars/src/camera_suite -I/Users/john/ars/scripts/cmake_modules/../../ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/lib/pkgconfig/../../include -I/Users/john/ars/scripts/cmake_modules/../../ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/lib/pkgconfig/../../include/srt -I/Users/john/ars/src/athena/smart_rtp/.. -I/Users/john/ars/src/athena/audio -I/Users/john/ars/src/athena/audio/.. -I/Users/john/ars/src/athena/audio/xplatform -I/Users/john/ars/src/athena/esapplibs/essim -I/Users/john/ars/src/athena/geodb/../../../ext -I/Users/john/ars/src/athena/webserver/.. -I/Users/john/ars/src/athena/libgdl90/.. -I/Users/john/ars/src/athena/libklv/misb/../.. -I/Users/john/ars/src/drivers/base/.. -I/Users/john/ars/src/graphlib/node_python/pynucleus/../.. -I/Users/john/ars/scripts/cmake_modules/../../ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/python3.11 -I/Users/john/ars/src/graphlib/connection_telemetryframe/.. -I/Users/john/ars/src/graphlib/connection_dataframe/.. -I/Users/john/ars/src/graphlib/connection_klvframe/.. -I/Users/john/ars/src/graphlib/connection_videoframe/.. -I/Users/john/ars/src/apps/ion/nodes/ion_ui -I/Users/john/ars/src/graphlib/connection_videoframe_y16/.. -I/Users/john/ars/src/athena/raster/.. -I/Users/john/ars/src/graphlib/connection_arinc429/.. -I/Users/john/ars/src/graphlib/connection_atcframe/.. -I/Users/john/ars/src/graphlib/connection_audioframe/.. -I/Users/john/ars/src/graphlib/connection_vancframe/.. -isystem /Users/john/ars/ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include -isystem /Users/john/ars/scripts/cmake_modules/../../ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include -isystem /Users/john/ars/ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/hash-library -isystem /Users/john/ars/ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/minizip -isystem /Users/john/ars/ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/eigen3 -isystem /Users/john/ars/ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/AL -isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk/usr/include/libxml2 -isystem /Users/john/ars/ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/webp -isystem /Users/john/ars/ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/OpenEXR -isystem /Users/john/ars/ext/vcpkg_installed_arm64-ios-cross/arm64-ios-cross/include/Imath -mcpu=apple-m1 -stdlib=libc++ -fvisibility-inlines-hidden -mcpu=apple-m1 -stdlib=libc++ -fvisibility-inlines-hidden -g -O0 -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk -miphoneos-version-min=17.0 -fPIE -fvisibility=default -fpch-instantiate-templates -Wno-error=cast-align -Wno-logical-op-parentheses -Werror=nonportable-include-path -Wnonportable-system-include-path -Werror=invalid-source-encoding -Wno-extern-c-compat -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-float-conversion -glldb -fstandalone-debug -Werror=return-type -DGM_PTR_SIZE_64 -DNO_GM_CRT_DEBUG -DGM_LITTLE_ENDIAN=1 -DGM_DEFAULT_ALLOC_ALIGNMENT=16 -DGM_SYSTEM_LIB=0 -Winvalid-pch -Xarch_arm64 -include/Users/john/ars/build/src/apps/gear/CMakeFiles/gear.dir/cmake_pch_arm64.hxx -c "-resource-dir=/Users/john/Library/Application Support/Code/User/globalStorage/llvm-vs-code-extensions.vscode-clangd/install/17.0.3/clangd_17.0.3/lib/clang/17" -x c++-header -std=gnu++20 -- /Users/john/ars/src/apps/gear/SetecState.h
I[11:02:19.989] <-- textDocument/documentLink(1144)
I[11:02:19.990] --> textDocument/clangd.fileStatus
I[11:02:20.005] <-- textDocument/inlayHint(1145)
I[11:02:20.019] Reloaded compilation database from /Users/john/ars/build/compile_commands.json
I[11:02:20.033] --> window/workDoneProgress/create(37)
I[11:02:20.033] Enqueueing 2921 commands for indexing
I[11:02:20.034] <-- reply(37)
I[11:02:20.034] --> $/progress
I[11:02:20.034] --> $/progress
I[11:02:20.222] <-- textDocument/codeAction(1146)
I[11:02:20.223] <-- $/cancelRequest
I[11:02:20.267] <-- textDocument/foldingRange(1147)
I[11:02:20.268] --> reply:textDocument/foldingRange(1147) 0 ms
I[11:02:21.996] Built preamble of size 49952032 for file /Users/john/ars/src/apps/gear/SetecState.h version 1 in 2.01 seconds
I[11:02:21.996] --> workspace/semanticTokens/refresh(38)
I[11:02:21.997] <-- reply(38)
I[11:02:22.073] --> textDocument/publishDiagnostics
I[11:02:22.073] --> textDocument/inactiveRegions
I[11:02:22.073] --> reply:textDocument/codeAction(1141) 2102 ms, error: Task was cancelled.
[Error - 11:02:22 AM] Request textDocument/codeAction failed.
[object Object]
I[11:02:22.074] --> reply:textDocument/documentSymbol(1142) 2102 ms
I[11:02:22.075] --> reply:textDocument/semanticTokens/full(1143) 2087 ms
I[11:02:22.075] --> reply:textDocument/documentLink(1144) 2086 ms
I[11:02:22.075] --> reply:textDocument/inlayHint(1145) 2070 ms
I[11:02:22.075] --> reply:textDocument/codeAction(1146) 1852 ms
I[11:02:22.075] --> textDocument/clangd.fileStatus
I[11:02:22.123] <-- textDocument/inlayHint(1148)
I[11:02:22.123] --> reply:textDocument/inlayHint(1148) 0 ms
I[11:02:22.123] --> textDocument/clangd.fileStatus
I[11:02:22.326] <-- textDocument/codeAction(1149)
I[11:02:22.326] --> reply:textDocument/codeAction(1149) 0 ms
I[11:02:22.326] --> textDocument/clangd.fileStatus
I[11:02:22.531] <-- textDocument/semanticTokens/full/delta(1150)
I[11:02:22.532] --> reply:textDocument/semanticTokens/full/delta(1150) 0 ms
I[11:02:22.532] --> textDocument/clangd.fileStatus
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.267] --> $/progress
I[11:02:25.980] Indexed /Users/john/ars/src/athena/cnlib/rendervk/matgentypesvk.cpp (432 symbols, 29883 refs, 930 files)
I[11:02:25.994] --> $/progress
I[11:02:25.994] --> $/progress
I[11:02:26.289] Indexed /Users/john/ars/src/athena/cnlib/rendervk/texturevk.cpp (439 symbols, 30905 refs, 945 files)
I[11:02:26.306] --> $/progress
I[11:02:26.306] --> $/progress
I[11:02:26.335] Indexed /Users/john/ars/src/athena/esapplibs/esappcommon/cev/cevview.cpp (625 symbols, 56347 refs, 1033 files)
I[11:02:26.365] --> $/progress
I[11:02:26.365] --> $/progress
I[11:02:26.486] Indexed /Users/john/ars/src/earthscapelib/localserver/tile_composite_request.cpp (3661 symbols, 42792 refs, 1010 files)
I[11:02:26.507] Indexed /Users/john/ars/src/graphlib/node_python/pynucleus/icd2superclass.cpp (413 symbols, 35838 refs, 1117 files)
I[11:02:26.528] --> $/progress
I[11:02:26.528] --> $/progress
I[11:02:26.598] --> $/progress
I[11:02:26.598] --> $/progress
I[11:02:26.700] Indexed /Users/john/ars/src/graphlib/node_common.cpp (101 symbols, 27147 refs, 927 files)
I[11:02:26.746] --> $/progress
I[11:02:26.747] --> $/progress
I[11:02:26.879] Indexed /Users/john/ars/src/athena/cnlib/rendervk/renderdevicevk.cpp (443 symbols, 33837 refs, 990 files)
I[11:02:26.907] --> $/progress
I[11:02:26.907] --> $/progress
I[11:02:27.302] Indexed /Users/john/ars/src/graphlib/node_camera_sim/node_camera_sim.cpp (552 symbols, 43515 refs, 1220 files)
I[11:02:27.332] --> $/progress
I[11:02:27.332] --> $/progress
I[11:02:27.453] Indexed /Users/john/ars/src/apps/gear/waypoint_navigation_tool.cpp (1203 symbols, 70420 refs, 2386 files)
I[11:02:27.497] --> $/progress
I[11:02:27.497] --> $/progress
I[11:02:27.668] Indexed /Users/john/ars/src/athena/cnlib/rendervk/vezhlp.cpp (108 symbols, 30683 refs, 932 files)
I[11:02:27.691] --> $/progress
I[11:02:27.691] --> $/progress
I[11:02:27.878] Indexed /Users/john/ars/src/apps/gear/entitypicker.cpp (1035 symbols, 68725 refs, 2335 files)
I[11:02:27.929] --> $/progress
I[11:02:27.929] --> $/progress
I[11:02:28.020] Indexed /Users/john/ars/src/apps/gear/SelectorTool.cpp (1090 symbols, 69852 refs, 2378 files)
I[11:02:28.081] --> $/progress
I[11:02:28.081] --> $/progress
I[11:02:28.926] Indexed /Users/john/ars/src/apps/gear/pagequickmenus.cpp (1815 symbols, 76334 refs, 2633 files)
I[11:02:28.983] Indexed /Users/john/ars/src/apps/gear/takvisualentobj.cpp (984 symbols, 67201 refs, 2233 files)
I[11:02:29.009] --> $/progress
I[11:02:29.062] --> $/progress
I[11:02:29.072] Indexed /Users/john/ars/src/apps/gear/markertool.cpp (2543 symbols, 75883 refs, 2504 files)
I[11:02:29.136] --> $/progress
I[11:02:29.241] Indexed /Users/john/ars/src/apps/gear/object_info_tool.cpp (1452 symbols, 70762 refs, 2405 files)
I[11:02:29.296] --> $/progress
I[11:02:29.471] Indexed /Users/john/ars/src/apps/gear/overlaymanager.cpp (668 symbols, 69115 refs, 2341 files)
I[11:02:29.510] --> $/progress
I[11:02:29.587] Indexed /Users/john/ars/src/apps/gear/gearregistry.cpp (1172 symbols, 82291 refs, 2800 files)
I[11:02:29.631] --> $/progress
I[11:02:29.812] Indexed /Users/john/ars/src/apps/gear/commonvisualentobj.cpp (505 symbols, 67946 refs, 2269 files)
I[11:02:29.859] --> $/progress
I[11:02:30.150] Indexed /Users/john/ars/src/apps/gear/layerconfig2.cpp (480 symbols, 71036 refs, 2365 files)
I[11:02:30.190] Indexed /Users/john/ars/src/apps/gear/appmenu.cpp (7363 symbols, 123272 refs, 2895 files)
I[11:02:30.195] --> $/progress
I[11:02:30.240] Indexed /Users/john/ars/src/apps/gear/TAKOverlay.cpp (1265 symbols, 96155 refs, 2491 files)
I[11:02:30.284] --> $/progress
I[11:02:30.384] --> $/progress
I[11:02:30.532] Indexed /Users/john/ars/src/athena/earcommon/DeviceServiceGimbal.cpp (286 symbols, 67636 refs, 2300 files)
I[11:02:30.569] --> $/progress
I[11:02:31.569] Indexed /Users/john/ars/src/apps/gear/applogic.cpp (8617 symbols, 153388 refs, 3071 files)
I[11:02:33.139] --> $/progress
I[11:02:35.033] <-- textDocument/didClose
I[11:02:35.033] --> textDocument/publishDiagnostics
[Info  - 11:02:38 AM] Connection to server got closed. Server will restart.
automainint commented 6 months ago

I had similar problem in clangd 14. When I open miniaudio.h in helix, clangd consumes all memory (11 GB) in 1 minute, and my system crashes.

Ubuntu clangd version 14.0.0-1ubuntu1.1
Features: linux+grpc
Platform: x86_64-pc-linux-gnu
helix 23.10 (f6021dd0)

2024-02-28_20-04 helix.log

HighCommander4 commented 6 months ago

@automainint the miniaudio.h issue is a known problem, see https://github.com/llvm/llvm-project/issues/70945. I'm trying to fix it in https://github.com/llvm/llvm-project/pull/78925.