Andreas L.
Andreas L.

Reputation: 4541

VS-Code Python debugging - ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

I wanted to debug my local Python code as usual in VS-Code on Windows 10 via pressing F5:

debugging settings VS-Code side-bar

I started having this error already more than a year ago, but recently it became persistent.

The entire error traceback:

$  /usr/bin/env 'DEBUGPY_LOG_DIR=c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891' c:\\Users\\username\\Projects\\project-venv\\Scripts\\python.exe c:\\Users\\username\\.vscode\\extensions\\ms-python.python-2021.8.1105858891\\pythonFiles\\lib\\python\\debugpy\\launcher 56721 -- c:\\Users\\username\\Projects\\project\\test_files\\prediction_performance_monitoring\\modified_app_for_docker_testing.py
Traceback (most recent call last):
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.8.9\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.8.9\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\launcher\__main__.py", line 97, in
<module>
    main()
  File "c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\launcher\__main__.py", line 53, in
main
    launcher.connect(host, port)
  File "c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\launcher/../..\debugpy\launcher\__init__.py", line 34, in connect
    sock.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

The .vscode/launch.json debugging configuration for local file testing contains these settings:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "python": "C:\\Users\\andreas.luckert\\Projects\\project-venv\\Scripts\\python.exe",
            "redirectOutput": true,
            "justMyCode": false,
            "logToFile": true,
            "stopOnEntry": false,
        }
    ]
}

Searching for the issue on StackOverFlow there were similar questions, but the answers (similar issue here) mostly stated something akin to

WinError 10061 - means that the server side TCP is not accepting the connection. For there is no application above listening on that port that client is trying to connect. Please check if you have your server application started and that it is listening on the intended port.

Yet, this did not help me because the questions referred to some remote connection, but in my case the debugging process is conducted locally. Moreover, I have not changed anything in the above-mentioned configuration which had been working normally a week ago.

Other answers like here included firewall diffulties, e.g.

Is firewall running on the server? If so, that may be blocking connections. You could disable firewall or add an exception on the server side to allow connections on port 8000.

I could not figure out how my firewall should block this debugging process, especially because it was not a constant issue but came and went irregularly. At times I thought it was related to a temporary shortage of free RAM, but this was proven a false assumption.

By and large, I cannot work sensibly with VS-Code Python debugging anymore. As this is an integral part of my workflow, I need to find out how to get rid of this problem.

Upvotes: 12

Views: 15739

Answers (3)

Nate Anderson
Nate Anderson

Reputation: 21216

If you get this error (ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it) accompanied by the message "Timeout waiting for debugger connection"... VS Code modal warning when user clicks "debug" button warning says "Timeout waiting for debugger connection"

...it might mean that the VS Code debugging server is waiting for the debugpy debug client, but the debugpy client takes too long because the "integratedTerminal" you're using takes a long time to open (my default terminal, Git Bash, takes a long time to open), or a long time to activate its environment (I use Python virtual environments)...

For me, I could fix it by waiting longer; specifically increasing the DEBUGPY_PROCESS_SPAWN_TIMEOUT value (also mentioned here and here)

To do that in Windows; try the steps below (screenshot follows)

  1. Open "Edit the system Environment Variables" (search the start menu for "Environment Variable")
  2. A "System Properties" dialog opens; click the "Environment Variables" button
  3. Under "User variables" , click the "New" button; give the Variable name: DEBUGPY_PROCESS_SPAWN_TIMEOUT, and the Variable value something high, like 500 (you can always decrease later)
  4. Click "Ok", and "Ok"
  5. Close and re-open Visual Studio Code (so it recognizes the new variable; using "Reload Window" won't work)

a screenshot showing how to add an environment variable DEBUGPY_PROCESS_SPAWN_TIMEOUT with a value of 180 (or higher, like 500) to increase from the default value if no variable specified (15), which might not be enough time for Git Bash to launch , create a debugger client, & connect to the VS Code debugger server

Upvotes: 5

Andreas L.
Andreas L.

Reputation: 4541

I needed to set my default VS-Code internal terminal profile to Command Prompt:

screenshot of VS Code showing to open the Terminal, and choose the dropdown arrow to click "Select Default Profile" and change to Command Prompt (cmd)

(Alternatively type F1 (or Ctrl+Shift+P) and type "Terminal: Select Default Profile", type Enter, and up/down-arrow to the Command Prompt choice, then type Enter again, as shown here)

This way, the VS-Code interactive debugger chooses the "cmd"-shell instead of previously "git-bash".

The culprit was the /usr/bin/env in the beginning of the auto-generated python.exe - call, which is prepended only in the git-bash - shell but not in cmd:

$  /usr/bin/env 'DEBUGPY_LOG_DIR=c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891' c:\\Users\\username\\Projects\\project-venv\\Scripts\\python.exe c:\\Users\\username\\.vscode\\extensions\\ms-python.python-2021.8.1105858891\\pythonFiles\\lib\\python\\debugpy\\launcher 56721 -- c:\\Users\\username\\Projects\\project\\test_files\\prediction_performance_monitoring\\modified_app_for_docker_testing.py

This was being blocked by the local firewall for some unknown reason; both on my Ubuntu 20.04 and my Windows 10 machines.

I suppose there is a way to resolve this problem, but for now I don't mind having the Windows-native Command Prompt as the default internal terminal in VS-Code. I can have several terminal types open at the same time and use their different capabilities to my advantage:

enter image description here

In fact, finding this solution was a game changer as beforehand this WinError: 10061 was often preventing me from working effectively with VS-Code.

Upvotes: 13

fishtrees
fishtrees

Reputation: 241

I don't know why, the Debug Adapter didn't listening on 127.0.0.1, so adding host directive into launch.json like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "host": "127.0.0.1"
        }
    ]
}

it's works for my windows 10 machine.

see also: https://github.com/microsoft/ptvsd/issues/2104

Upvotes: 1

Related Questions