microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.43k stars 29.34k forks source link

Debug console is very slow with a single long line #206854

Open Austint30 opened 8 months ago

Austint30 commented 8 months ago

Does this issue occur when all extensions are disabled?: Yes

Steps to Reproduce:

  1. Write a Python script that outputs text to the console every iteration of an infinite while loop.
  2. Execute the script using debugpy. Example: python -m debugpy --listen 5678 --wait-for-client ./script.py
  3. Attach the debugger.

VSCode will slow down and eventually freeze up.

This also affects scripts that output a lot of text that overwrites the current line. In my case I have a script that sifts through hundreds of thousands of records and displays a count indicating its progress.

Here is an example script and launch.json file:

Script:

def main():

    loops = 0

    while(True):
        loops += 1

        print(f"This script has looped {loops} times.", end="\r", flush=True)

if __name__ == "__main__":
    main()

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
              "host": "localhost",
              "port": 5678
            }
          }
    ]
}

Note: the slowdown can be temporarily fixed by clearing the debug console. But if you have a script constantly outputting text you're going to need to be clicking it constantly to prevent VSCode from freezing.

This GitHub issue was closed because it was fixed apparently https://github.com/microsoft/vscode/issues/19678. But, it seems it was fixed by clearing the debug console when the debug session starts. I think this needs to be further improved upon and clear the oldest N lines so VSCode doesn't lock up.

roblourens commented 8 months ago

Is this about writing output really quickly, or writing huge amounts of text to the console, not necessarily quickly? A simple test script would help

Austint30 commented 8 months ago

Upon testing an example script I wrote, it seems the slowdown issues does not happen when using the Python Debugger: Python File configuration as nothing is written to the Debug Console. However, using debugpy does trigger this to happen. I've updated my original post adding an example script, launch.json and adding the debugpy step.

Austint30 commented 8 months ago

@roblourens Just in case you didn't see it, the script I have added to my post reproduces the issue and causes vscode to slow down and then freeze. Make sure you use debugpy when running the script so you can attach a debugger to it. I've also updated the steps to reproduce.

Austint30 commented 7 months ago

@roblourens Hey Rob, have you been able to reproduce this issue yet?

roblourens commented 7 months ago

I see that- it's about having a really long line. If I change the \r to \n so the output is on different lines, then it seems fine. Profiled just a little, seems like this line is slow https://github.com/microsoft/vscode/blob/8098a783f2a35e28f36d67522bbe7e45140528b8/src/vs/workbench/contrib/debug/common/replModel.ts#L307-L308

and also I wonder whether this async queue slows event handling down https://github.com/microsoft/vscode/blob/8098a783f2a35e28f36d67522bbe7e45140528b8/src/vs/workbench/contrib/debug/browser/debugSession.ts#L1132 maybe it can execute sync when the queue is empty, or maybe it can merge multiple accumulated output events?