'VSCode python debugger fails to attach to local port with ECONNREFUSED

I'm trying to debug locally a project that is deployed on Microsoft Azure.

I have a copy of the repository on my PC and setup the launch.json file as:

{
    "version": "0.2.0",
    "configurations": [


        {
            "name": "Attach to Python Functions",
            "type": "python",
            "request": "attach",
            "port": 9091,
            "preLaunchTask": "func: host start",
            "justMyCode": false,
        }
    ]
}

This has been working for days, but today is suddenly giving me problems. When in VSCode I click "Start debugging" I can see VSCode opening a new terminal, installing the dependencies specified in the virtual environment and executing func: host start as requested.

However, without a clear reason, today VSCode started showing an error message saying

connect ECONNREFUSED 127.0.0.1:9091

How can I fix this?

PS: I'm not an expert of VSCode, so please let me know if I omitted any useful information and I will add it.



Solution 1:[1]

I have been receiving the same error quite a few times while I'm trying to run the azure function from Run -> Run without Debugging One of the workarounds that worked for me is to use the command func host start from the terminal.

enter image description here

Alternatively, you can update the .vscode/tasks.json file. For more information on this, you can check VSCode is not starting Func Host Start properly

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1