Search code examples
python-3.xazure-functionsvscode-debuggerazure-functions-core-toolsazure-http-trigger

How to completely stop locally running Azure Function? Azure Function [Python V2] continues previous execution when restarting debugger[VS Code]?


I am running an HTTP-Triggered Azure Durable Function with a nested orchestrator structure. A single execution of the function can run for multiple hours and make hundreds of database operations.

During development and testing, I am running the function locally in my VS Code [MacOS] in debugging mode. In order to stop the process from running, I simply hit Ctrl + C to kill the process.

The problem: When I restart the debugger, the function continues where it left off and executes extremely long-running processes. I am unable to figure out how to fully stop the previously started processes. I can tell they are running by the logs I see in my terminal.

When starting an HTTP Triggered Function, I get the "statusQueryGetUri" and "terminatePostUri" which I would usually use to stop the function, however I have no idea which execution is currently running, and I am not aware of how I could retrieve the ID which would allow me to terminate the run which is still active.

This is what my launch.json file looks like:

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

My function is connected to my dev database and to the dev blob storage.

azure-functions==1.17.0
azure-functions-durable==1.2.6

VS Code: Version: 1.90.2
OS: Darwin arm64 23.3.0

I tried:

  • restart my VS Code and my computer.
  • searched for any locally stored state but couldn't find any files.
  • tried to kill the process via terminal ps aux | grep python then kill -9 ID

I am expecting:
I am expecting to start my debugger and see my azure function idling until I start the HTTP trigger via postman. Then when I run a function but stop it again by killing the terminal or process, I am expecting it to not continue the next time I start the debugger again.


Solution

    1. To stop the Azure durable functions, you can use terminatePostUri of your function.

    You can get the InstanceID of the function from terminatePostUri.

    http://localhost:7071/runtime/webhooks/durabletask/instances/<Instance_ID>/terminate?reason={text}&taskHub=TestHubName&connection=Storage&code=rzKXAu_HfN3-JqjaYvOzZon5ACBt8Ew7lVvou6zQNMjDAzFuFAtHMw==
    

    enter image description here

    • Run the terminatePostUri by updating the reason in the URI, it stops the function execution using the command:
    func durable terminate --id <Instance_ID> --reason <reason to terminate>
    
    1. Disconnect the debugger to stop the function in the Debug bar.

    2. To completely stop a locally running Azure Function, you should stop the process that is running the function in the Activity Monitor on macOS.

    • Navigate to Applications => Utilities=>Start Activity Monitor.
    • Select the running process=>Quit/Force Quit
    • Once the process is stopped, all the running instances of the function will be terminated.
    1. Debug the function using the function core tools func host start --debug and use Ctrl+C to stop running the function:

    enter image description here

    References:

    Completely Stopping Locally Running Azure Function: Azure Function (Python V2) Continues Previous Execution Restarting Debugger in VSCode? (trycatchdebug.net)