python-3.xvisual-studio-codevscode-debuggerconnection-refused

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.


Solution

  • 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.