'Unable to debug Python Azure function in VS Code IDE. Getting connect ECONNREFUSED 127.0.0.1:9091 error

I am trying to debug Azure functions python code using VS code IDE.
Local.settings.json is updated with below config

"AzureWebJobsStorage": "UseDevelopmentStorage=true"

Things I tried so far :-

  • I reinstalled VS code,
  • Downgraded Azure Function Core Tools from 4.0 to 3.0
  • Any pointers to solve this issue will be super helpful.

Below is the error on VS Code IDE when trying to debug Azure function written in Python:

enter image description here

Host.json below

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  },
  "functionTimeout": "20:00:00",
  "extensions": {
    "durableTask": {
      "maxConcurrentActivityFunctions": 1
    }
  }
}

launch.json below

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Python Functions",
            "type": "python",
            "request": "attach",
            "port": 9091,
            "preLaunchTask": "func: host start"
        }
    ]
}

task.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cmd host start",
            "type": "shell",
            "dependsOn": "pip install (functions)",
            "windows": {
                "command": ". ${config:azureFunctions.pythonVenv}\\Scripts\\activate && func host start --verbose"
            },
            "isBackground": true,
            "problemMatcher": "$func-python-watch"
        },
        {
            "label": "pipInstall",
            "type": "shell",
            "osx": {
                "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
            },
            "windows": {
                "command": ". ${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
            },
            "linux": {
                "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
            },
            "problemMatcher": []
        },
        {
            "type": "func",
            "command": "host start",
            "problemMatcher": "$func-python-watch",
            "isBackground": true,
            "dependsOn": "func: extensions install"
        },
        
        {
            "type": "func",
            "command": "extensions install",
            "dependsOn": "pip install (functions)",
            "problemMatcher": []
        },
        {
            "label": "pip install (functions)",
            "type": "shell",
            "osx": {
                "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
            },
            "windows": {
                "command": ". ${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
            },
            "linux": {
                "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
            },
            "problemMatcher": []
        }
        
    ]
}


Solution 1:[1]

There were many workarounds to resolve ECONNREFUSED 127.0.0.1:9091 in Visual Studio Code - Stack: Python Azure Functions:

Approach 1:

Modify the tasks.json (available in .vscode folder in VS Code) like the below one:

Tasks Json Modify


Approach 2:

If you don't want to use the above modifications of task.json in every new project whenever this error ECONNREFUSED 127.0.0.1:9091 occurs, then you can use this workaround given in the GitHub vsCode-AzureFunctions Issue No: 760.

  1. VS Code > Your Python Azure Functions Project > View Menu > Open the Command Palette or Ctrl + Shift + P.
  2. Type User Settings and Select: Go to Features in User Menu > Select Terminal > Make the setting "terminal.integrated.shell.windows" value should be powershell.exe.

Debugworksfinewithpowershell

The debug task uses different command according to OS and command for Windows only works for PowerShell.

Note:

  • As per my research, the error message is too generic. Different Systems/Projects of same type (Azure Functions Python) are having different solutions as I was experienced in.
  • For Few Systems/Projects, changing the port works. As 7071 is the default port for executing HTTP functions in the tooling whereas the debugger needs to attach over a different port.
  • While changing the debug port, it should be changed in both launch.json and tasks.json.

There are two distinct ports at work when the host is started with Python debugging capabilities: . 7071 is the default port where the HTTP endpoint is exposed by the host.

Where is this set?

  1. 9091 is the default port used for starting the debugging endpoint for the Python worker. This is needed for remote attaching to the worker. This needs to be the same in tasks.json (-m ptvsd --host 127.0.0.1 --port 9091) and launch.json. These are set to 9091 Both of these need to be distinct from each other but, other than that, it doesn't matter what values they have. These settings should be handled by the VSCode function creation experience so that conflicts do not arise.

Approach 3:

As specified in the MSFT Q&A for the error ECONNREFUSED 127.0.0.1:9091 in Azure Functions Python, could you please explicit the Azure Functions Extension Bindings/Bundles explicitly as python is Non .NET Language and run/debug the Function.

Approach 4:

As mentioned in the GitHub - Azure Functions - Issue No 1016, two other workarounds of this kind of issue exists longback were:

Solution 2:[2]

I have also had this issue, and have tried all of the solutions provided by HariKrishnaRajoli-MT. What worked for me was to uninstall the Azure Functions extension and re-install, and it worked for a time. I still get the issue occasionally and have to repeat the process.

Whilst you mentioned that you re-installed VS Code, it is also worth mentioning that you should remove all extensions as well if that is desired (and if the installer does not remove it for you)

The following post has some instructions on where to find the extensions (depending on OS).

Completely uninstall VS Code extensions

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 HariKrishnaRajoli-MT
Solution 2 HLChucky