Repository navigation
Description
Apache Airflow Provider(s)
common-sql
Versions of Apache Airflow Providers
apache-airflow-providers-amazon==9.0.0
apache-airflow-providers-common-sql==1.19.0
apache-airflow-providers-mysql==5.7.3
Apache Airflow version
2.10.3
Operating System
Amazon Linux 2023
Deployment
Amazon (AWS) MWAA
Deployment details
The issue happens on a production Amazon (AWS) MWAA environment, and is also reproducible locally using the MWAA local runner (deployed on a Mac).
What happened
The SQLExecuteQueryOperator fails with timeout, but only after the query has completed.
This is the workflow currently happening: T0: Start -> T1: Run query -> T2: Expected timeout failure -> T3: Query finishes -> T4: Operator fails on timeout
What you think should happen instead
Once the execution timeout is reached, the SQLExecuteQueryOperator should kill the query and then fail.
This is the expected workflow: T0: Start -> T1: Run query -> T2: Expected timeout, operator kills query and fails
How to reproduce
The issue is reproducible both on a production Amazon (AWS) MWAA environment, and locally using the MWAA local runner (deployed on a Mac).
The DAG below contains two tasks:
- The first one executes a long-running query on a MySQL database using the
SQLExecuteQueryOperator. - The second one executes a long-running process using the
PythonOperator.
The PythonOperator fails its execution exactly after one minute (as expected), the SQLExecuteQueryOperator takes a few minutes until the query completes (not as expected).
import os
import time
from datetime import timedelta
from airflow.decorators import dag
from airflow.models.connection import Connection
from airflow.operators.python import PythonOperator
from airflow.providers.common.sql.operators.sql import SQLExecuteQueryOperator
@dag(schedule=None)
def test_sql_operator_timeout_dag():
# SQL operator test
connection = Connection(
conn_id="mysql_connection",
conn_type="mysql",
host="xyz",
port="3306",
schema="test",
login="xyz",
password="xyz",
)
env_key = f"AIRFLOW_CONN_{connection.conn_id.upper()}"
connection_uri = connection.get_uri()
os.environ[env_key] = connection_uri
SQLExecuteQueryOperator(
task_id="mysql_long_query",
conn_id=connection.conn_id,
sql="select benchmark(1200000000, md5('when will it end?'));",
execution_timeout=timedelta(minutes=1),
)
# Python operator test
def sleeping_function():
time.sleep(120)
PythonOperator(
task_id="python_long_task",
python_callable=sleeping_function,
execution_timeout=timedelta(minutes=1),
)
test_sql_operator_timeout_dag = test_sql_operator_timeout_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
Activity
boring-cyborg commentedon Jan 22, 2025
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
eladkal commentedon Jan 22, 2025
cc @dabla maybe you will have time to look into this? Seems inconsistent behavior of SQLExecuteQueryOperator
dabla commentedon Jan 22, 2025
I've also encountered the same behaviour, as the connection is blocking the thread or something and thus the SQLExecuteQueryOperator task only stops once the connection returns back, at least that's what I experienced.
So to avoid that issue, what I've done when using the JdbcHook, was by specifying a connection timeout on the JDBC connection, that way when the task has to timeout, the connection will also (even though the query will continue to run on the database) and thus not block the thread of the Airflow worker, but this was a specific solution with JDBC connections.
Bellow the code:
So maybe in the hook some kind of timeout should also be specified on the connection being used, or if you want a more finegrained approach per operator, the you could specify the connection timeout through the hook_params of the SQLExecuteQueryOperator? Still, this al depends if the underlying connection supports it of course.
eladkal commentedon Jan 22, 2025
Should we apply this logic for the underlying get hook function that we are using? While not generic to all if we can provide workaround for connections that support it I think we should.
Yet the underlying issue is not clear to me. The kill signal from timeout comes from the scheduler. Why would the task not respond to it? The
exectuion_timeoutis for Airflow, not for the underlying SQL engine.dabla commentedon Jan 22, 2025
We could, but that possibly would not work for all cases and would be a workaround of the real issue.
It's indeed a weird issue and I was also surprised by that behaviour. Back then I thought it was related to the Jdbc connection and maybe there was some glitch between the Python and Java process, but now we can assume this is a generic issue related to database connections. So yes it's weird that worker doesn't get killed.
eladkal commentedon Jan 22, 2025
I am not sure it explains it. Airflow runs a process (that happens to be database connection) Airflow should be able to kill the process. The fact that the issue happens for Sql but not for Python is really odd. From the description it sounds like something is blocking the scheduler from killing the task (because it tried to kill it after its finished). I think that the issue is with the scheduler itself.
dabla commentedon Jan 22, 2025
Question, at least for me, is how do you debug something like this?
potiuk commentedon Feb 24, 2025
This is a question of signal handling most likely. Airlfow's timeout is done using SIGALRM. If the driver runs a low-level c code that does not handle signals properly this behaviour might happen. Proper implementation of a c-library code is to periodically check in a loop if signal has been received and pass control back to python if it happened - but some c-level code (especially database drivers) might not handle it properly.
The solution - discussed few times is to make another fork of the process and handle timeout in the separate fork, and we decided to defer it to after new Task SDK is completed - and maybe even in new Task SDK it's already handled ? @ashb @amoghrajesh ?
Generally we have already two processes (parent and fork):
a) supervisor that performs heartbeat
b) task that executes the job
there was also c) openlineage handling
In Airlfow 2 timeout was implemented as siglarm handling inside b) which means that if b) executed bad-behaving C-code timeout was not happening. In Airflow 3 we have a bit different way of heartbeating and task execution and it's being still iterated on - and the options we could implement is to move the timout handling to a) or introduce another fork with timeout between a) and b). The former likely better from resource usage point of view.
dabla commentedon Feb 26, 2025
Thank you Jarek, this seems indeed to be a probable explanation. This proves that I have still a lot to learn from Airflow inner workings :(
amoghrajesh commentedon Feb 28, 2025
@potiuk @dabla IIUC, are you checking with me if every "task" will now run a new subprocess?
Signal handling is now done by a) (supervisor) which watches the b) (task runner subprocess) and this should probably be fixed there :)
@bentorb maybe try and run your task with Airflow 3? We have beta just released
potiuk commentedon Feb 28, 2025
I have not checked - but is timeout handling also done in supervisor? It was previously done in the equivalent of the task runner subprocess and that was the root cause of the problem.
bentorb commentedon Mar 3, 2025
Thank you @amoghrajesh. MWAA supports up to Airflow 2.10.3, so at the moment this is not an option for my team.
github-actions commentedon Mar 4, 2026
This issue has been automatically marked as stale because it has been open for 365 days without any activity. There has been several Airflow releases since last activity on this issue. Kindly asking to recheck the report against latest Airflow version and let us know if the issue is reproducible. The issue will be closed in next 30 days if no further activity occurs from the issue author.
darshil929 commentedon Jun 30, 2026
I looked into this on current main, since the thread trailed off on "try Airflow 3", and it still reproduces.
I wrapped two workloads in the same TimeoutPosix/SIGALRM context the task runner applies around execute, both with a 1s execution_timeout. A plain time.sleep is killed right at 1s, but a blocking native query only fails once it returns, around 5.7s in my run. I used a long sqlite3 query for the native side since it behaves the same way as mysqlclient here. So the alarm fires on time, it just can't interrupt the native call until control returns to Python.
To answer the question raised earlier in the thread: on main, execution_timeout is still handled in the task runner via SIGALRM, not in the supervisor (supervisor.py doesn't act on it), so the Airflow 3 execution rework didn't change this part.
The general fix appears to be tracked already in #53337 (moving enforcement to the supervisor), with an earlier attempt in #59657 that was closed for inactivity. I'd be glad to take this on if it's helpful. Do you have a preference on the direction, reviving the supervisor-level approach or something narrower on the common-sql side such as a DB-side statement timeout? Happy to go whichever way you prefer.
potiuk commentedon Jun 30, 2026
I do not think there is a narrower fix. But also If you are new, I do not think you will know all edge cases to implement it properly and a lot of maintaineer time might be wasted, so if you are not fully confident in your knowledge of that part, I would recommend you to wait for someone who knows all the detetails about task-sdk > task communication and supervisor to implement it.
darshil929 commentedon Jul 1, 2026
Understood, and thanks for being straight with me about it. I'll step back and leave it to someone who knows the supervisor and task-sdk side well.
If there's another open issue you think might be a good fit for me in the meantime, I'd really appreciate a pointer, otherwise I'll keep digging through the good first issues. Thanks again for taking the time.