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

5

8

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": []
        }
        
    ]
}
Bowser answered 18/3, 2022 at 20:40 Comment(0)
G
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:

Gillis answered 19/3, 2022 at 2:34 Comment(7)
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 issueBowser
Tried approach 1, its not working.Bowser
Could you try with a different port setting in the host.json!Gillis
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" } ]}Bowser
Tried changing different port in launch.json, but no luckBowser
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!Gillis
Updated host.json in question sectionBowser
G
2

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

Goeselt answered 18/2, 2023 at 22:25 Comment(1)
Good to know, But on refreshing are you able to hit the breakpoint ?Bowser
C
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

Cull answered 3/5, 2022 at 2:46 Comment(0)
M
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.

Melisamelisande answered 31/8, 2023 at 15:22 Comment(0)
P
-1

For me, this error occurred just after setting a Function App Project for the first time on my Windows machine. I tinkered around and got another error that let me to this answer. The problem was that ExecutionPolicy was not defined and functions-core-tools (or VS Code?) was not able to execute scripts due to security restrictions.

You can check your current ExecutionPolicy by writing this in your Powershell (in VS Code):

Get-ExecutionPolicy -List

Setting it to RemoteSigned resolved my issue:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
Pustule answered 30/3 at 21:8 Comment(2)
Which one? What's the difference? Why was this necessary? How did this happen?Soupy
You linked to a question, not an answer. Which specific answer? Try clicking "share" under the answer.Soupy

© 2022 - 2024 — McMap. All rights reserved.