Microsoft.VSCode.CPP.IntelliSense.Msvc.linux at 100% CPU for hours
(I already posted this in another bug, but was kindly asked to file it differently. I hope this is good; it's my first report re. vscode.)
Ubuntu 14.04. VSCode 1.17.1. C++ extension 0.13.1. Disabling other extensions doesn't seem to affect problem.
I work with a source tree of around 1500 files of mixed C/C++.
Problem: ~/.vscode/extensions/ms-vscode.cpptools-0.13.1/bin/Microsoft.VSCode.CPP.IntelliSense.Msvc.linux runs at 100% continuously, and IntelliSense barely works.
I've now resorted to a small script that'll kill all such processes. Then it'll run and IntelliSense will be usable for a few minutes, and then we're back at 100%
I finally got annoyed enough to attach gdb to the offending thread and got this trace; maybe it inspires some ideas. It smells like endless looping, perhaps due to wrong error handling in file I/O. I've frequently seen code that doesn't get error handling entirely right when, for example, a file disappears under its feet and where it just keeps looping, trying to read from a file that no longer provides data. Particularly with sub-processes.
In my usecase I compile frequently, and part of our build process generates new C++ files that are then compiled into the final images. I wouldn't be surprised if VSCode got surprised about this "flapping" of files.
All the other threads sit seemingly idle in futex waits. Some of them probably waiting for this one.
#0 0x000000000073b3f9 in source_file_for_seq ()
#1 0x000000000089982d in an_error_ptr_cmp::operator()(an_error* const&, an_error* const&) const ()
#2 0x000000000089ae52 in an_error_manager::get_errors(std::__1::vector<an_error*, std::__1::allocator<an_error*> >&) ()
#3 0x000000000053c6bd in edge::scoped_query_manager::get_filtered_errors_for_tu(edge::list<edge::error*>*, bool (*)(edge::error const&)) ()
#4 0x000000000054484d in edge::an_edge_translation_unit_impl::get_filtered_errors_for_tu(edge::allocator*, edge::snapshot_session const*, bool (*)(edge::error const&), edge::isense_error_array*) ()
#5 0x000000000050c2f0 in edge_intellisense_server_impl::handle_get_errors(microsoft::cpp::intellisense::GetErrorsRequest const&) ()
#6 0x0000000000530c0d in work_queue::worker_proc() ()
#7 0x000000000052d323 in std::__1::__function::__func<microsoft::vc::thread_pool::enqueue(std::__1::function<void ()>, std::__1::future<void>*)::{lambda()#1}, std::__1::allocator<{lambda()#1}>, void ()>::operator()() ()
#8 0x00000000008bddbd in microsoft::vc::thread_pool::thread_pool(int)::{lambda()#1}::operator()() const ()
#9 0x00000000008bdbad in void* std::__1::__thread_proxy<std::__1::tuple<microsoft::vc::thread_pool::thread_pool(int)::{lambda()#1}> >(std::__1::tuple<microsoft::vc::thread_pool::thread_pool(int)::{lambda()#1}>) ()
#10 0x00007f058bdcc184 in start_thread (arg=0x7f0583fff700) at pthread_create.c:312
#11 0x00007f058baf1ffd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
From what I've seen so far, this "source_file_for_seq" crash only occurs if headers can't be found and/or there are too many errors (i.e. due to missing headers), so one potential work around is to make sure the includePaths can be found by IntelliSense. However, we still want to fix this crash, since it's our highest hitter, and we have a repro internally (the fixed corrupted il_header bug I mentioned previously turned out to be a different crash).
@deepskyblue86 @eskild Changing line
"--exceptions",
to
"--exceptions",
"--error_limit",
"2147483647",
in msvc.json in the bin folder of the cpptools extension should fix the crash, although it's possible we might come up with a better fix later. If the crash still occurs after this change, that would mean our parsing is failing for another reason and we'd have to do a better fix.
@sean-mcmanus Sounds good, thank you. I really hope it goes away now; I've added the params to msvc.json, and then we'll see.
Please forgive me for saying this, because I haven't done the debugging and therefore I'm ignorant, but doesn't this smell a bit more of workaround than fix? I mean, given sufficiently "hostile" input, we might end up with the problem manifesting itself again. Or?
It's not easy for us to determine if the IntelliSense engine is stuck in an infinite loop or legitimately taking a long time, but we are considering adding logic to terminate and disable it if the QoS is sufficiently bad for a given code file. I think it would be safe to say that if parsing or IntelliSense operations take more than ## seconds/minutes each, then the engine is not serving you in any meaningful way and we should probably use the fuzzy IntelliSense for that file instead.
@eskild Yeah, it's a workaround to avoid the crash. If we see this is still a problem, we'll investigate fixing the root cause (error limit aborting causes a data structure to be missing expected data, leading to a crash). The buggy code is "owned" by another team in VS so maybe they'll fix the root issue later. It sounds like @bobbrow is talking about a different issue (intelliSense getting stuck).
@sean-mcmanus Thanks for elaborating, appreciate it! :+1:
You can try a preview .vsix at https://github.com/Microsoft/vscode-cpptools/releases/tag/v0.15.0-insiders (our final release is planned for next week). We suspect it will solve most of the crashes, but the root cause of the crash when the error limit is exceeded is still being investigated by another team (it repros in VS 2017 too).
Cool, installed .vsix. I noticed that the --error-limit is set at 25000, so I'll leave it there as opposed to the earlier suggestion of 2^31. Will keep you posted if I encounter anything.
Yeah, we lowered the error limit to avoid the possibility of an infinite loop of error processing. The previous limit was only 250.
Hm... I'm seeing runaway CPU on macos right now, 1.20.1, cpptools-0.15.0-insiders.
I'm on a somewhat slow, but stable, VPN from home, and was just shutting down vscode for the day.
I have several left-over processes, and one run-away from the first startup of vscode earlier today. The other ones appear to come from subsequent startups.
me 77002 99.7 0.0 4307212 2724 ?? R 10:00AM 302:03.41 /Users/me/.vscode/extensions/ms-vscode.cpptools-0.15.0-insiders/bin/Microsoft.VSCode.CPP.IntelliSense.Msvc.darwin 76846 4
me 87325 0.0 0.1 4315232 13204 ?? S 4:55PM 0:00.30 /Users/me/.vscode/extensions/ms-vscode.cpptools-0.15.0-insiders/bin/Microsoft.VSCode.CPP.Extension.darwin
me 87236 0.0 0.1 4315040 13488 ?? S 4:52PM 0:00.35 /Users/me/.vscode/extensions/ms-vscode.cpptools-0.15.0-insiders/bin/Microsoft.VSCode.CPP.Extension.darwin
me 78596 0.0 0.0 4315040 11484 ?? S 12:04PM 0:00.34 /Users/me/.vscode/extensions/ms-vscode.cpptools-0.15.0-insiders/bin/Microsoft.VSCode.CPP.Extension.darwin
I tried attaching lldb, but I'm really not an expert in debugging on macos, so... But here it is:
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
* frame #0: 0x00007fff531b18be libsystem_kernel.dylib`swtch_pri + 10
frame #1: 0x00007fff532f7307 libsystem_pthread.dylib`sched_yield + 11
frame #2: 0x00000001024ceefe Microsoft.VSCode.CPP.IntelliSense.Msvc.darwin`intellisense_server_base::shutdown() + 62
frame #3: 0x00000001024cb820 Microsoft.VSCode.CPP.IntelliSense.Msvc.darwin`edge_intellisense_server_impl::~edge_intellisense_server_impl() + 32
frame #4: 0x00000001024cb7f9 Microsoft.VSCode.CPP.IntelliSense.Msvc.darwin`edge_intellisense_server_impl::~edge_intellisense_server_impl() + 9
frame #5: 0x00000001024cb7c3 Microsoft.VSCode.CPP.IntelliSense.Msvc.darwin`std::__1::__shared_ptr_pointer<edge_intellisense_server_impl*, std::__1::default_delete<edge_intellisense_server_impl>, std::__1::allocator<edge_intellisense_server_impl> >::__on_zero_shared() + 19
frame #6: 0x00007fff510d38fd libc++.1.dylib`std::__1::__shared_weak_count::__release_shared() + 43
frame #7: 0x00000001024cbb8f Microsoft.VSCode.CPP.IntelliSense.Msvc.darwin`main + 351
frame #8: 0x00007fff5306b115 libdyld.dylib`start + 1
Sorry I can't be more helpful, but it appears we're not entirely home free yet.
Our main process will kill the IntelliSense process when it's shutdown, but it's possible our main process may have crashed beforehand. The IntelliSense process is stuck waiting for another thread to shutdown -- we've added code that aborts the waiting if it takes too long. Are you able to get call stacks for the dangling main processes? I've witnessed some random dangling main processes and crashes on Mac too so we'll try do more investigation on Mac for March.
@eskild @ihalip @deepskyblue86 0.15.0 fixed the source_file_for_seq crash (at least for most scenarios, an additional fix for remaining scenarios may occur later). The dangling IntelliSense processes should be reduced as well. Can you file a new bug for any additional crashes or dangling processes? We've seen some remaining cases happening randomly that we'll continue to investigate later.
@sean-mcmanus I'm using the 0.15.0 preview since when you asked us to try it. I'm happy with it, no issue at all. I'll try the official release today!