dabla opened a new issue, #77875:
URL: https://github.com/apache/airflow/issues/77875

   ### Under which category would you file this issue?
   
   Task SDK
   
   ### Apache Airflow version
   
   main (`732204be41`), also present in 3.4.0
   
   ### What happened and how to reproduce it?
   
   A task with an `execution_timeout` can hang when the timeout fires, instead 
of failing with `AirflowTaskTimeout`.
   
   `TimeoutPosix.handle_timeout` is the `SIGALRM` handler, and it logs before 
it raises:
   
   
https://github.com/apache/airflow/blob/732204be41f30e5de626a63e516464d89eaff49c/task-sdk/src/airflow/sdk/execution_time/timeout.py#L36-L39
   
   Python runs a signal handler on the main thread, between two bytecodes of 
whatever that thread is doing. When the main thread is itself writing a log 
line at that moment, it is inside structlog's write lock (`WriteLogger.msg` / 
`BytesLogger.msg` hold a plain, non-reentrant `threading.Lock` per output 
file). The handler's `self.log.error(...)` then waits for the lock its own 
thread holds, so it never returns and the exception is never raised.
   
   The window is small for a task that logs little, and wide for one that logs 
a lot on the main thread, for example an async task that logs per item.
   
   A deterministic reproduction, which makes the alarm arrive while a log line 
is being written:
   
   ```python
   import faulthandler
   import io
   import signal
   import sys
   
   import structlog
   
   from airflow.sdk.execution_time.timeout import timeout
   
   faulthandler.dump_traceback_later(5, exit=True)  # shows where it hangs, 
then exits
   
   
   class AlarmWhileWriting(io.StringIO):
       """Stands for the alarm arriving while a log line is being written."""
   
       fired = False
   
       def write(self, text):
           if not self.fired:
               AlarmWhileWriting.fired = True
               signal.raise_signal(signal.SIGALRM)
           return super().write(text)
   
   
   
structlog.configure(logger_factory=structlog.WriteLoggerFactory(file=AlarmWhileWriting()))
   log = structlog.get_logger(logger_name="task")
   
   try:
       with timeout(60):
           log.info("a task logs on the main thread")
   except BaseException as e:
       print("raised:", type(e).__name__, file=sys.stderr)
   ```
   
   It never prints `raised: AirflowTaskTimeout`. After 5 seconds `faulthandler` 
shows the main thread waiting in the handler's own log call, inside the log 
call it interrupted:
   
   ```
   Timeout (0:00:05)!
   Thread 0x... (most recent call first):
     File ".../structlog/_output.py", line 221 in msg
     File ".../structlog/_base.py", line 224 in _proxy_to_logger
     File ".../airflow/_shared/logging/structlog.py", line 135 in meth
     File ".../airflow/sdk/execution_time/timeout.py", line 38 in handle_timeout
     File "repro.py", line 23 in write
     File ".../structlog/_output.py", line 222 in msg
     File ".../structlog/_base.py", line 224 in _proxy_to_logger
     File ".../airflow/_shared/logging/structlog.py", line 135 in meth
     File "repro.py", line 32 in <module>
   ```
   
   I first saw it without any patching, with a real timer: an async task 
logging two INFO lines per item on the main thread, with a random 
`execution_timeout` between 0.05 and 0.6 seconds, hung in 3 of about 100 runs, 
each with this stack.
   
   ### What you think should happen instead?
   
   The task should end with `AirflowTaskTimeout` at its `execution_timeout`, 
whatever the main thread was doing.
   
   A signal handler should not take a lock the interrupted thread may hold. The 
handler could only raise, and the "Process timed out" line could be logged 
where the exception is handled, outside the handler.
   
   ### Operating System
   
   Ubuntu 24.04 (WSL2), Python 3.12.3, structlog 26.1.0
   
   ### Deployment
   
   Other
   
   ### Apache Airflow Provider(s)
   
   _No response_
   
   ### Versions of Apache Airflow Providers
   
   _No response_
   
   ### Official Helm Chart version
   
   Not Applicable
   
   ### Kubernetes Version
   
   Not Applicable
   
   ### Helm Chart configuration
   
   Not Applicable
   
   ### Docker Image customizations
   
   Not Applicable
   
   ### Anything else?
   
   The log call in the handler came with the move of the task timeout into the 
Task SDK (#54089); Airflow 2's `airflow/utils/timeout.py` logged in the handler 
as well, through the standard library's logging, whose handler lock is 
reentrant.
   
   The supervisor still ends such a task: its own deadline for the timeout 
sends SIGTERM, then SIGKILL. So the hang is bounded, but the task is killed 
instead of failing with `AirflowTaskTimeout`, and nothing in its log says why.
   
   ### Are you willing to submit PR?
   
   - [x] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to