airflow: "execute cannot be called outside TaskInstance" warning fired despite being in a TaskInstance
Apache Airflow version
2.9.3
If “Other Airflow 2 version” selected, which one?
No response
What happened?
BigQueryTableExistenceSensor fire an “execute cannot be called outside TaskInstance” warning on execute
My observation: The ExecutorSafeguard is being called twice for a single sensor execution
Reason is, that the first encounter is over the TaskInstance _execute_callable (as intended). Now the decorater checks, if it is executed within a Taskinstance. After that, it will call the “real” execute function, which is also decorated and therefor will do the check again, but without a sentinel, which leads to a failed check and fires a warning (or raise a exception if allow_nested_operators is set).
See also: https://github.com/apache/airflow/discussions/41426
And there was also already opened an issue with the same problem but not solved, see #39413
What you think should happen instead?
Decorater should not fire a warning. Maybe it should be checked, if the decorater was already called and therefor did already checked it.
How to reproduce
from airflow import DAG
from airflow.providers.google.cloud.sensors.bigquery import BigQueryTableExistenceSensor
from airflow.utils.dates import days_ago
from airflow.models.baseoperator import BaseOperator
class LoggingBaseOperator(BaseOperator):
def execute(self, context):
self.log.info(f"Executing {self.__class__.__name__}")
return super().execute(context)
class LoggingBigQueryTableExistenceSensor(
BigQueryTableExistenceSensor, LoggingBaseOperator
):
def poke(self, context):
self.log.info(f"Poking {self.__class__.__name__}")
return True
dag = DAG(
"test_bigquery_sensor_double_execution",
default_args={
"start_date": days_ago(1),
},
description="A simple DAG to test BigQueryTableExistenceSensor execution",
schedule_interval=None,
)
sensor_task = LoggingBigQueryTableExistenceSensor(
task_id="test_sensor",
project_id="your-project-id",
dataset_id="your-dataset-id",
table_id="your-table-id",
poke_interval=60,
dag=dag,
)
Operating System
Ubuntu 22.04.4 LTS
Versions of Apache Airflow Providers
No response
Deployment
Docker-Compose
Deployment details
Docker image: apache/airflow:2.9.3-python3.11
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
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 17 (11 by maintainers)
I’m getting the same warning for a ShortCircuitOperator in Version 2.10.4
{baseoperator.py:421} WARNING - ShortCircuitOperator.execute cannot be called outside TaskInstance!The DAG configuration looks like this:
@sam-gen-cop The fix is now available in Airflow 2.10.3
The build of the PR passed so once it’s merged I think we can close this issue.
Can you open a new issue for that with the detail of your case please ? Or even better - contributing a fix - following that one here should be relatively simple so maybe you or your company would like to contribute it? Just to put it in perspective - this is an open-source project that a lot of people contribute to, so when you create such an issue, you will have to wait until somoene picks it up and fix it. But the most certain way is to implement and contribute a fix yourself. The second best is to find someone who can do it - maybe even paying them. The third best is to open a detailed issue that makes it easy to reproduce by anyone, we can then mark it as “good first issue” for example and hopefully sooner or later one of the contributors will fix it.
Following one of those routes is the best way to help in solving your issue @npatel44
The fix is not in Airflow 2.10.1 nor 2.10.2, I just checked the source code. I suppose it will be in 2.10.3 @potiuk @uranusjr ?
Hmm seem I can’t find it in rc1 of 2.10.3?
@SKalide i think you can simplify your example:
this produces the same warning message by using this simpler example, i believe it’s more convincing that this issue is “serious” 😁