Repository navigation
MSGraphAsyncOperator fails to call API #54350
Copy link
Copy link
Closed
Labels
area:providerskind:bugThis is a clearly a bugThis is a clearly a bugneeds-triagelabel for new issues that we didn't triage yetlabel for new issues that we didn't triage yetprovider:microsoft-azureAzure-related issuesAzure-related issues
Description
Apache Airflow Provider(s)
microsoft-azure
Versions of Apache Airflow Providers
apache-airflow-providers-microsoft-azure==12.6.0
Apache Airflow version
3.0.4
Operating System
Debian GNU/Linux 12 (bookworm)
Deployment
Official Apache Airflow Helm Chart
Deployment details
Deployed in AKS using an extended docker image containing authorization logic and extra provider packages.
The KubernetesExecutor is used for running Tasks.
What happened
The MsGraphAsync operator fails to get an connection. It errors out with the following exception.
[2025-08-11, 09:33:08] ERROR - An error occurred: You cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly.: source="airflow.providers.microsoft.azure.triggers.msgraph.MSGraphTrigger"
RuntimeError: You cannot use AsyncToSync in the same thread as an async event loop - just await the async function directly.
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/providers/microsoft/azure/triggers/msgraph.py", line 197 in run
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/providers/microsoft/azure/hooks/msgraph.py", line 403 in run
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/providers/microsoft/azure/hooks/msgraph.py", line 422 in send_request
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/providers/microsoft/azure/hooks/msgraph.py", line 252 in get_conn
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/hooks/base.py", line 64 in get_connection
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/models/connection.py", line 478 in get_connection_from_secrets
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/definitions/connection.py", line 144 in get
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/sdk/execution_time/context.py", line 155 in _get_connection
File "/home/airflow/.local/lib/python3.12/site-packages/airflow/jobs/triggerer_job_runner.py", line 713 in send
File "/home/airflow/.local/lib/python3.12/site-packages/asgiref/sync.py", line 186 in __call__
The dag code:
from datetime import datetime, timedelta
from airflow import DAG
from airflow.providers.microsoft.azure.operators.msgraph import MSGraphAsyncOperator
default_args = {
"start_date": datetime(2025, 1, 1),
"retries": 0,
"retry_delay": timedelta(minutes=5),
}
subscription_id = "placeholder"
resource_group = "rg-fabric"
capacity = "placeholder"
with DAG(
dag_id="get_fabric_capacity",
schedule=None,
default_args=default_args,
catchup=False,
tags=["Fabric"]
) as dag:
get_capacity_details = MSGraphAsyncOperator(
task_id="get_fabric_capacity",
conn_id="FABRIC_CAPACITY",
method="GET",
url=(
"https://management.azure.com"
f"/subscriptions/{subscription_id}"
f"/resourceGroups/{resource_group}"
f"/providers/Microsoft.Fabric"
f"/capacities/{capacity}"
),
query_parameters={"api-version": "2023-11-01"},
response_type="str"
)What you think should happen instead
The expected behaviour is that the request is made to the API, and a response is shown, either 401 or 200 containing a response body
How to reproduce
Create a dag calling the MSGraphAsyncOperator with an existing connection and run the Dag.
Anything else
No response
Are you willing to submit PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
Metadata
Metadata
Assignees
Labels
area:providerskind:bugThis is a clearly a bugThis is a clearly a bugneeds-triagelabel for new issues that we didn't triage yetlabel for new issues that we didn't triage yetprovider:microsoft-azureAzure-related issuesAzure-related issues
Activity
gopidesupavan commentedon Aug 11, 2025
hm this requires async connection implemention
gopidesupavan commentedon Aug 11, 2025
looks like https://github.com/apache/airflow/blob/main/providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/msgraph.py#L252 this is being used for both sync and async
gschuurman commentedon Aug 11, 2025
This is also related to #53447
It seems that all the async operations that use the basehook for getting a connection are broken.
gopidesupavan commentedon Aug 11, 2025
not all only connection s that are using sync in async session
eladkal commentedon Aug 11, 2025
cc @dabla
dabla commentedon Aug 11, 2025
The MSGraphOperator is purely async, all operaions are done through a triggerer, which is async, there are no sync operations involved (except the next method).
gopidesupavan commentedon Aug 11, 2025
no the get connection called from sync i believe it should be async
dabla commentedon Aug 11, 2025
That must something new (or something has changed in Airflow) as this has always been the case
dabla commentedon Aug 11, 2025
I will test this tomorrow
gopidesupavan commentedon Aug 11, 2025
I think from day one the triggers must implement everything in async way, before AF3 its allowable sync version function to write and execute, but with AF3 the interaction to task sdk from the trigger should be async always otherwise the same error as seen above will happen :)
Alteast sync function should use sync_to_async.
dabla commentedon Aug 11, 2025
Weird, cause we are using the MSGraphAsyncOperator in Airflow 3 and never had this issue, I even started a new PR to allow using it through start_from_trigger directly and even there we didn’t encounter this issue. Will check it out tomorrow with above release cause we are using the newer version (which is not release yet due to another bug in the triggerer for which I also did a PR).
13 remaining items
gopidesupavan commentedon Aug 14, 2025
it require async get_connection implementation in provider. if you looks at aws and google providers they all have async implementation for getting connection in triggerer. so similar way azure should have async implementation and thats the correct way, previously it was worked as its direct db call and while calling the get_connection the event loop was in block state , this is not good :)
It just that now async means async now :) so at-least sync_to_async functions should be used if anyone dont want to implement separate async functions
I would call it as provider issue this is not from airflow.
dabla commentedon Aug 14, 2025
Ok, I understand now, but that would imply a lot of providers will need to be updated, as those will suffer the same issue.
gopidesupavan commentedon Aug 14, 2025
Yeah have todo list to find what all providers using sync call from triggerer and list them and fix with help of community , i need get list. will get it this week
dabla commentedon Aug 14, 2025
So yes, your assumption was correct. I'm looking into it as the same issue will also arise with PowerBI.
dabla commentedon Aug 18, 2025
@gopidesupavan @gschuurman I've created a PR which fixes this issue.
gschuurman commentedon Sep 2, 2025
@dabla I just checked the fix, but unfortunatly it does not seem to work.
dabla commentedon Sep 2, 2025
Indeed, I'm also encountering the same error in 3.0.6, it worked in 3.0.4 and 3.0.5-rc2.
dabla commentedon Sep 2, 2025
I just saw a new async get_connection has been added to BaseHook in Airflow 3.0.6, will use that one instead. Now I wonder how I can make my provider cross airflow compatible?
dabla commentedon Sep 2, 2025
The MSGraph is doing the same as the AwsGenericHook, so I wonder if that one would also suffer the same issue? I'm 100% confident that it worked in 3.0.5-rc2, as I just started testing with 3.0.6 today, so until today, it was working fine.
dabla commentedon Sep 2, 2025
I've patched code as follow in KiotaRequestAdapterHook:
I'm still getting same error, here is the stacktrace:
dabla commentedon Sep 2, 2025
It seems the connection.extra_dejson is causing this issue:
Probably the mask_secret is causing the RuntimeError.
dabla commentedon Sep 2, 2025
I patched the _build_request_adapter method of the KiotaRequestAdapterHook locally and replace following line:
config = connection.extra_dejson if connection.extra else {}With:
config = json.loads(connection.extra) if connection.extra else {}And now it works again locally in 3.0.6.
dabla commentedon Sep 2, 2025
I've created a new PR which avoids above mentioned issue.
Use extra_dejson in the msgraph sync get_conn now apache#54350 is fixed