samples-python icon indicating copy to clipboard operation
samples-python copied to clipboard

[Bug] RestrictedWorkflowAccessError on async context manager in return close of an activity.

Open Natim opened this issue 7 months ago • 2 comments

What are you really trying to do?

When using

@register_activity
@activity.defn
async def get_http_response(URL: str) -> dict:
    async with httpx.AsyncClient() as client:
        return await client.get(url)

Describe the bug

We get the following error:

RestrictedWorkflowAccessError Cannot access threading.local.mro_entries from inside a workflow. If this is code from a module not used in a workflow or known to only be used deterministically from a workflow, mark the import as pass through.

Minimal Reproduction

I believe the workflow sandbox is reactivated before the asynclient __aexit__ code. Which leads to Temporal thinking its execution is happening in the workflow scope rather than within the activity one.

Environment/Versions

temporal==1.10.0

Additional context

I'm trying to split it in two calls to make it work:

@register_activity
@activity.defn
async def get_http_response(URL: str) -> dict:
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
    return response

Natim avatar Jun 09 '25 09:06 Natim