Skip to content

Awaiting ctx.run results from several asyncio tasks loses wakeups: the handler stalls until inactivity_timeout #315

Open

Description

@cakir-enes

When a handler awaits several durable futures from different asyncio tasks, for example asyncio.gather over tasks that each await ctx.run_typed(...), the handler regularly hangs. All run results are proposed, acknowledged by the runtime, and delivered to the SDK. Even so, one task stays blocked until the service's inactivity_timeout (60 s by default) ends the attempt. The retry replays the journal and finishes, so the invocation succeeds, but it pays the full timeout each time.

The cause is that every waiting task runs its own create_poll_or_cancel_coroutine loop, and all of those loops read from the one ReceiveChannel queue. The runtime's completion for task A's handle can be read by task B. B feeds it to the VM, checks only its own handle, and goes back to receive(). Nobody re-checks A, so A sleeps until the next input frame, which never comes.

restate.gather(...) over the same futures, awaited from one task, is not affected.

import asyncio
import random

import restate

svc = restate.Service("Repro")


@svc.handler()
async def run(ctx: restate.Context, n: int) -> str:
    # Await n ctx.run results from separate asyncio tasks, as pydantic-ai does for parallel tool calls.
    async def step(i: int) -> str:
        async def work() -> str:
            await asyncio.sleep(random.uniform(0.001, 0.05))
            return str(i)

        return await ctx.run_typed(f"step {i}", work)

    for _ in range(3):
        await asyncio.gather(*(asyncio.create_task(step(i)) for i in range(n)))
    return "ok"


app = restate.app(services=[svc])
docker run -d --name restate --network host docker.restate.dev/restatedev/restate:1.8.0
hypercorn repro:app --bind 127.0.0.1:9080
curl localhost:9070/deployments -H content-type:application/json -d '{"uri":"http://127.0.0.1:9080"}'
curl -X PATCH localhost:9070/services/Repro -H content-type:application/json -d '{"inactivity_timeout":"5s"}'

for n in 1 3; do
  seq 1 40 | xargs -P 10 -I{} curl -s -o /dev/null -w '%{time_total}\n' \
    localhost:8080/Repro/run -H content-type:application/json -d $n \
    | awk -v n=$n '{t++; if ($1>4) s++} END {print "n=" n ": " s "/" t " took >4s"}'
done

Activity

  1. kirillsergeev-workato commented on Nov 24, 2026

    @kirillsergeev-workato

    We hit this at Workato too, running pydantic-ai agents with parallel tool calls on Restate. I've opened #325 with a fix: a single input reader, which wakes the other pollers after each event. Feedback welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions