robotcodedev / robotcode

RobotFramework support for Visual Studio Code
https://robotcode.io
Apache License 2.0
168 stars 13 forks source link

[BUG] Memory leak when editing a project that uses a dynamic library #170

Closed picope closed 6 months ago

picope commented 9 months ago

Description

Noticed high CPU and memory usage when editing a project that uses a dynamic library. Trying out older versions of the extension, i noticed:

❌ version 0.51.1 and newer heve the issue. ✅ version 0.50.0 and older work as expected.

Steps to reproduce

  1. Open code.
  2. Open a project that makes use of a dynamic library.
  3. Change python interpreter if needed. In my case 3.11.5.
  4. Run htop or similar application, filter processes by 'robotcode'.
  5. Notice how process CPU usage stays at 100% and memory usage increases indefinitely.
  6. Another thing is that processes stay alive when code is closed.

Code snippet

I'm not allowed to share the code, but this is more or less how the import statement of the dynamic library looks like:

*** Settings ***

Library           MyDynamicLibrary
...                   header=header.xml
...                   payload=payload.xml
...                   config=config.xml
...                   option=option.xml
...                   show=name
...               WITH NAME    MDL
Library           String

Screenshots

278412742-aace4385-a8a7-46d6-b86f-a8f12125879d

Expected behavior

As in version 0.50.0 , i expect the processes to consume at most 200MB of memory .

Logs

Python Extension is active Invalid python version for workspace folder 'myworkspace'. Only python version >= 3.8 supported. Please update to a newer python version or select a valid python environment. create Language client: RobotCode Language Server mode-pipe for folder "myworkspace" trying to start Language client: RobotCode Language Server mode=pipe for folder "myworkspace" client for file:///home/user/dev/proy/gitdir/myworkspace starting. client for file:///home/user/dev/proy/gitdir/myworkspace running. client for file:///home/user/dev/proy/gitdir/myworkspace started. executeRobotCode: /home/user/dev/proy/gitdir/tcs-build-release/test/.temp/tcs-client/bin/python -u -X utf8 /home/user/.vscode/extensions/d-biehl.robotcode-0.61.7/bundled/tool/robotcode --format json --no-color --no-pager --default-path. discover --read-from-stdin all executeRobotCode: exit code 0

Desktop

d-biehl commented 9 months ago

unfortunately, i can't recreate it on my machine and would need a little more information.

What exactly does your dynamic library do in the constructor. Does it start any threads or processes? Or does it initialize anything that does that? As I understand it, it is a library you created, right? Can you create the library documentation on the console using the libdoc tool from RobotFramework?

I see you have an old RobotFramework version, now current is version 6.1, have you tried a newer RobotFramework version?

RobotCode starts internally several subprocesses, so for example every time a library has to be parsed, because Python is not so good at threading and on the other hand because a library has to be instantiated and you don't know what else the library instantiates. For the library documentation it is not necessary that everything is initialized in the constructor, but only what is needed to query the keywords.

from typing import Dict, Optional
from robot.running.context import EXECUTION_CONTEXTS

class DummyLibrary:
    def __init__(self, device_config: Optional[Dict[str, str]] = None) -> None:
        do_a_little_bit()
        if EXECUTION_CONTEXTS.current is not None:  # check if robot is running
            do_what_ever_is_needed_to_execute_keywords()

    def get_keywords(self):
        ...

    ....

The above code is an example of initializing a library correctly only when RobotFramework is running. So not when LibDoc is running, which RobotCode does in the background when it encounters a library import in the background. Maybe you can try this?

There is at least one second entry in the output view that starts with RobotCode Language Server, can you take a look at it or copy the output here?

If there is nothing, you can also increase the loglevel, just open the .vscode/settings.json (if it does not exist yet, please execute the command "Preferences: Open Workspace Settings (JSON)") and insert the following code:

    "robotcode.languageServer.extraArgs": [
        "--log",
        "--log-level",
        "DEBUG",  // DEBUG, INFO, WARN, ERROR, CRITICAL
    ]

And then look again in the Output View.

d-biehl commented 8 months ago

any news about this?

d-biehl commented 8 months ago

@picope do you have some more informations about this issue? If I don't hear from you again, I will unfortunately have to close this issue.

picope commented 8 months ago

Hi! Sorry for the delay @d-biehl , i was kinda busy last days.

Answering your questions, the constructor of this library does not create any threads or processes. I can run libdoc on the library and makes the doc just fine. By now we are forced to use RF4 due to project requirements, and we are not upgrading any soon. As you suggest ill try increasing log level, and reply this issue as soon as possible.

d-biehl commented 7 months ago

@picope do you have any news about this issue?

d-biehl commented 6 months ago

@picope ping

d-biehl commented 6 months ago

closed due to inactivity