terraform-ls: High CPU usage since 0.25.0

Server Version

0.25.0

Terraform Version

v1.0.2

Client Version

VSCode 1.62.3

Terraform Configuration Files

N/A perhaps?

Log Output

There’s lot of 2021/12/03 10:09:32 provider_schema.go:196: PSS: getting provider schema in the vscode console log and it seems to be going over and over the repo again and again looking at the whole repo (which has a lot of Terraform in it spread out all over the place)

Expected Behavior

It should work without high CPU

Actual Behavior

image

The CPU varies from 50-100% consistently while in TF files.

The log when I last copied it had 184491 lines.

Steps to Reproduce

Load some TF files in vscode since the latest update

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 17
  • Comments: 39 (17 by maintainers)

Most upvoted comments

@ElvenSpellmaker Thanks for the report and sorry for the experience.

looking at the whole repo (which has a lot of Terraform in it spread out all over the place)

This indexing is generally by design e.g. to enable discovery of cross-module references.

It has a cap on parallelism, but that is mostly just to reduce the peak CPU usage, it just means the indexing will take longer at the cost of lower CPU usage. We have plans to potentially allow this cap to be configurable as per

I would say the easiest short-term solution, while we investigate, would be to open just a few relevant individual folders as opposed to the parent folder which contains many Terraform modules, or to set "requiredVersion": "0.24.0" in "terraform.languageServer" settings block in VS Code.

The log when I last copied it had 184491 lines.

I would be very interested in the log as it could show us the sequence of operations that happen as part of the indexing. Are you willing to share it? Feel free to encrypt it with my GPG key and send to radek<at>hashicorp.com if it’s sensitive and you don’t want to post it publicly.

I’d like to chime in to say I’m seeing a similar increase in CPU usage/degradation in performance since 0.25.0 got installed. For instance, I’m seeing this CPU usage while the Terraform Formatter is attempting to run on save on a variables file with ~200 LoC and ~60 variables defined in it. It hangs like this for minutes before finishing, unless I cancel it first.

Screen Shot 2021-12-06 at 2 56 17 PM

@radeksimko You’ll be receiving an email with the output data in a bit. As far as the CPU profile, I tried configuring the language server as seen in the code block below and something isn’t working correctly. The file gets created, but then nothing gets written to it. I tried a different directory in case there was something weird going on with permissions, and it made no difference.

    "terraform.languageServer": {
        "external": true,
        "args": [
            "serve",
            "-cpuprofile=/tmp/terraform-ls-cpu.prof"
        ]
    },

@radeksimko I emailed a copy of both my memory and cpu profiles

Is there any documentation on viewing the generated .prof files, or does that require tools we don’t have access to? I’d kinda like to be able to see what’s actually in the data I’d be sending in case there’s anything in there I wouldn’t want to send, even PGP-encrypted.

The best way to inspect the profile would be via go tool pprof, see https://pkg.go.dev/net/http/pprof for more.

It was the same time last time there was a CPU bug, the CPU profiles don’t generate while the hanging is present

I suspect this may be related to the signal that is sent to the editor and/or the language server. It will not have any chance to write the CPU profile or do anything if it receives SIGKILL - at that point it’s just being killed. You could try to find terraform-ls in the process list and try terminating it gracefully from there, or just open your terminal and do it from there:

$ ps -A | grep terraform-ls
78852 ??         0:04.11 /.../bin/terraform-ls serve
$ kill -INT 78852

On one of my files I notice I’m not getting the 1 references count etc

What you see on the top screenshot is the reference count “code lens”, which should be disabled (for all files) and not shown if you have set terraform.codelens.referenceCount to false.

Thank you for sending over the log.

I don’t yet entirely understand how that would relate to high CPU usage, but I have a hunch that the flood of the mentioned log messages may be related to the pre-computation of the reference count code lens where we basically attempt to find references across all (initialized) modules in the workspace. It’s worth noting that these lookups are in-memory lookups so they should not consume any significant CPU, but it would likely explain the log messages.

Can you try setting "terraform.codelens.referenceCount" to false in your VS Code settings (only available in the latest extension release 2.17.0) and check the log/CPU again?

If this continues to be a problem - or either way - can you try to obtain the CPU profile and send it over?

I’ll get the file over to you

EDIT: I’ve just captured a file using "-log-file=C:\\Users\\JackBlower\\Documents\\terraform-ls-{{pid}}.log", and will get that over, this one is 44k lines so far.