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,
)

log_bg.txt

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 17 (11 by maintainers)

Most upvoted comments

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:

  # Create Airflow Operators for each task dynamically
  task_objects = {}
  for task_id, python_callable in tasks:
      if task_id.startswith('get_'):
          task_objects[task_id] = ShortCircuitOperator(
              task_id=task_id,
              python_callable=python_callable,
              op_kwargs={
                  'credentials': credentials
              },
              dag=dag,
          )
      else:
          task_objects[task_id] = PythonOperator(
              task_id=task_id,
              python_callable=python_callable,
              op_kwargs={
                  'credentials': credentials
              },
              dag=dag,
          )

Hi @dabla, I’m using AWS MWAA 2.10.1 and I still get the same warning. Just wondering whether the issue is fixed in 2.10.1 or only in 2.9.3

Script,

from airflow import DAG
from airflow.utils.dates import days_ago
from airflow.operators.bash import BashOperator
from airflow.utils.log.logging_mixin import LoggingMixin

class MyBashOperator(BashOperator, LoggingMixin):
    def execute(self, context):
        self.log.info("Executing MyBashOperator directly in the DAG definition")
        return super().execute(context)

dag = DAG(
    "test_bash_execution",
    default_args={
        "start_date": days_ago(1),
    },
    schedule_interval=None,
)

# Instantiate the operator
my_bash_task = MyBashOperator(
    task_id="my_bash_operator",
    bash_command="echo 'Testing direct execution'",
    dag=dag,
)

Logs, image

@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.

This Warning still happens for custom Operator written on top of Provider’s operators. (Multiple levels of nesting) . Logic of this PR should work for any levels of nesting so not sure what’s the issue but I still see this with airflow 2.10.2 and 2.10.4

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

Hi @dabla, I’m using AWS MWAA 2.10.1 and I still get the same warning. Just wondering whether the issue is fixed in 2.10.1 or only in 2.9.3

Script,

from airflow import DAG
from airflow.utils.dates import days_ago
from airflow.operators.bash import BashOperator
from airflow.utils.log.logging_mixin import LoggingMixin

class MyBashOperator(BashOperator, LoggingMixin):
    def execute(self, context):
        self.log.info("Executing MyBashOperator directly in the DAG definition")
        return super().execute(context)

dag = DAG(
    "test_bash_execution",
    default_args={
        "start_date": days_ago(1),
    },
    schedule_interval=None,
)

# Instantiate the operator
my_bash_task = MyBashOperator(
    task_id="my_bash_operator",
    bash_command="echo 'Testing direct execution'",
    dag=dag,
)

Logs, image

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:

from airflow import DAG
from airflow.utils.dates import days_ago
from airflow.operators.bash import BashOperator


class MyBashOperator(BashOperator):
    def execute(self, context):
        self.log.info(f"Executing {self.__class__.__name__}")
        return super().execute(context)


dag = DAG(
    "test_bash_execution",
    default_args={
        "start_date": days_ago(1),
    },
    description="A simple DAG to test MyBashOperator execution",
    schedule_interval=None,
)

task = MyBashOperator(
    task_id="my_bash_operator",
    bash_command="ls -lah",
    dag=dag,
)

this produces the same warning message by using this simpler example, i believe it’s more convincing that this issue is “serious” 😁