4

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": []
        }
        
    ]
}

4 Answers4

3

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:

  • Tired option 2, 3 & approach 4 is already set, it worked only first time. new VS code session triggers same error. apart from that tried checking netstat -p tcp -ano | findstr 127.0.0.1:9091 command, no process is running with 9091. Please help us fix this issue – Dyaneshwaran S Mar 25 '22 at 14:50
  • Tried approach 1, its not working. – Dyaneshwaran S Mar 25 '22 at 14:56
  • Could you try with a different port setting in the `host.json`! –  Mar 25 '22 at 14:58
  • below is my host.json settings & launch.json settings { "version": "2.0", "logging": { "applicationInsights": {"samplingSettings": {"isEnabled": true,"excludedTypes": "Request"}}}, "extensionBundle": {"id": Microsoft.Azure.Functions.ExtensionBundle","version": "[3.3.0, 4.0.0)" }, "functionTimeout": "20:00:00", "extensions": {"durableTask": { "maxConcurrentActivityFunctions": 1 } }} { "version": "0.2.0", "configurations": [ { "name": "Attach to Python Functions", "type": "python", "request": "attach", "port": 9091, "preLaunchTask": "func: host start" } ]} – Dyaneshwaran S Mar 28 '22 at 16:19
  • Tried changing different port in launch.json, but no luck – Dyaneshwaran S Mar 28 '22 at 16:22
  • Could you please include the above host.json code in good code format in the question so that it helpful to understand well and answer! –  Mar 28 '22 at 16:23
  • Updated host.json in question section – Dyaneshwaran S Mar 30 '22 at 01:51
1

I had the same problem getting ECONNREFUSED 127.0.0.1 in the VSC debugger every SECOND time I ran the debugger (I'm a newbie!). After a lot of trial and error (also the above tricks) I found the solution to reset the error situations which was just to refresh the debugger window:

enter image description here

And when you click refresh, the error goes away! :-) enter image description here

0

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

HLChucky
  • 11
  • 1
0

In order to run Azure Function locally, you need the "Function Host", which is the func utility.

This utility is known to VS Code, therefore, there's a special setting to override its location:

azureFunctions.funcCliPath

The actual "launch" configuration is in 3 parts:

  • The debugger, which depends on
  • The Function Host (func), which may depend on
  • Python Virtual Env bootstrap

Only the debugger is specified in the launch.json, the other two are specified in tasks.json, using a dependency chain via dependsOn setting.

ECONNREFUSED should be taken to mean "nothing accepts the connection", in other words, the Function Host is not running.

That is because either the Core Tools or Azure Functions CLI are installed in non standard directory OR the setting mentioned above is set to a wrong directory.

Try running the "func: host start" TASK by itself, and you can see where it looks for the Function Host utility.

(Also, notice that the command is "host start"; that's because the taks type is already func, which is built into VS Code via the extension)

It is perfectly possible to run the function host on its own, via the terminal, but you will lack debugging features.

Ate Somebits
  • 287
  • 1
  • 18