dapr / python-sdk

Dapr SDK for Python
Apache License 2.0
232 stars 127 forks source link

[WORKFLOW SDK BUG] Get workflow state raising exception for instances that do not exist #743

Closed ajstewart closed 1 month ago

ajstewart commented 1 month ago

Expected Behavior

When I run:

state = wf_client.get_workflow_state(instance_id=my_id)

I expect to receive a None response when the instance does not exist, as defined by the docs:

The current state of the workflow instance, or None if the workflow instance does not exist.

Actual Behavior

I am getting an Exception when the instance does not exist:

== APP == grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
== APP ==   status = StatusCode.UNKNOWN
== APP ==   details = "error from internal actor: no such instance exists"
== APP ==   debug_error_string = "UNKNOWN:Error received from peer ipv4:127.0.0.1:55008 {grpc_message:"error from internal actor: no such instance exists", grpc_status:2, created_time:"2024-10-23T10:46:36.084357+01:00"}"

Steps to Reproduce the Problem

The following small app can reproduce by calling the endpoint

import dapr.ext.workflow as wf
import uvicorn

from fastapi import FastAPI

def hello_world(ctx: wf.WorkflowActivityContext):
    """Hello world workflow."""
    return "Hello world!"

def dummy_workflow(ctx: wf.DaprWorkflowContext):
    """Dummy workflow."""
    result = yield ctx.call_activity(hello_world)
    return result

wf_runtime = wf.WorkflowRuntime()
wf_runtime.register_workflow(dummy_workflow)
wf_runtime.register_activity(hello_world)

wf_client = wf.DaprWorkflowClient()

wf_runtime.start()

app = FastAPI()

@app.get("/workflow/{instance_id}")
def get_workflow_state(instance_id: str):
    """Get the workflow state."""
    state = wf_client.get_workflow_state(instance_id=instance_id)

    if state is None:
        return {"message": "Workflow not found"}

    return {"status": state.runtime_status}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=5001)

Release Note

RELEASE NOTE:

ajstewart commented 1 month ago

Closing this as I used the wrong template.