jagdish-kumawat opened a new issue, #77848: URL: https://github.com/apache/airflow/issues/77848
### Under which category would you file this issue? Airflow Core ### Apache Airflow version 3.4.0 ### What happened and how to reproduce it? Pressing Ctrl+C in `airflow standalone` sometimes never finishes. Standalone prints `Shutting down components`, never prints `Complete`, and the scheduler and triggerer processes keep running until they are killed. It happened 4 times in about 30 Ctrl+C presses here, 3 of them while the machine was short of memory: ``` dag-processor | 2026-12-01T04:33:56.138423Z [info ] Found 5 files for bundle dags-folder (dag_discovery_safe_mode=True) [airflow.dag_processing.manager.DagFileProcessorManager] loc=manager.py:1033 ^Cstandalone | Shutting down components ``` **Cause: the SIGINT/SIGTERM handler logs, and it can run in the middle of a log write.** `SchedulerJobRunner._exit_gracefully` and `TriggererJobRunner._exit_gracefully` handle both signals, and both log before anything else: - https://github.com/apache/airflow/blob/e24990d25cf546a83fbecfb46bdee8b54a5115f0/airflow-core/src/airflow/jobs/scheduler_job_runner.py#L932-L944 - https://github.com/apache/airflow/blob/e24990d25cf546a83fbecfb46bdee8b54a5115f0/airflow-core/src/airflow/jobs/triggerer_job_runner.py#L287-L296 `self.log` is a structlog logger, and Airflow's console output goes through structlog's `WriteLogger`. Its `msg()` writes and flushes while holding a per-file `threading.Lock`, which is not re-entrant (structlog 26.1.0, the version in the 3.4.0 constraints and the latest release; `main` is the same): https://github.com/hynek/structlog/blob/26.1.0/src/structlog/_output.py#L217-L223 ```python def msg(self, message: str) -> None: with self._lock: self._write(message + "\n") self._flush() ``` Python runs a signal handler in the main thread whenever the interpreter checks for pending signals. A flush checks after every `write()` it makes, and a `write()` interrupted with `EINTR` runs the handlers before it retries: https://github.com/python/cpython/blob/v3.14.10/Modules/_io/bufferedio.c#L2046-L2050 So a signal that arrives while the main thread is inside `msg()` runs `_exit_gracefully` inside `msg()`, and its `self.log.info(...)` waits forever for the lock that the same thread already holds. Each further signal runs the handler again, and it blocks again. Python's logging documentation warns about logging from signal handlers for this reason: https://docs.python.org/3/library/logging.html#thread-safety `airflow standalone` makes this likely. Ctrl+C sends SIGINT to the whole process group, and standalone then calls `terminate()` on every component at once, so each component gets SIGTERM while its SIGINT handler is still writing "Exiting gracefully". Standalone then waits in `command.join()` forever: - https://github.com/apache/airflow/blob/e24990d25cf546a83fbecfb46bdee8b54a5115f0/airflow-core/src/airflow/cli/commands/standalone_command.py#L128-L136 - https://github.com/apache/airflow/blob/e24990d25cf546a83fbecfb46bdee8b54a5115f0/airflow-core/src/airflow/cli/commands/standalone_command.py#L342-L345 This is the main thread of the hung scheduler from that standalone run, sampled with macOS `sample` (interpreter frames between these lines removed). The triggerer's is the same, starting in `select_kqueue_control_impl` instead of `time_sleep`: ``` time_sleep PyErr_CheckSignals _io_TextIOWrapper_flush _io__Buffered_flush _bufferedwriter_flush_unlocked PyErr_CheckSignals lock_PyThread_acquire_lock _PyMutex_LockTimed _PyParkingLot_Park _pthread_cond_wait __psynch_cvwait ``` **Reproduce it every time, with one SIGTERM and only the scheduler** (Python 3.14.10, uv 0.13.10, macOS). Pausing the reader of the scheduler's stdout makes its main thread block in a log write while it holds the lock, and a SIGTERM then interrupts that write: ```bash uv venv --python 3.14 source .venv/bin/activate uv pip install "apache-airflow==3.4.0" --constraint "https://raw.githubusercontent.com/apache/airflow/constraints-3.4.0/constraints-3.14.txt" export AIRFLOW_HOME="$PWD" AIRFLOW__LOGGING__LOGGING_LEVEL=DEBUG airflow db migrate mkfifo scheduler.out cat scheduler.out > scheduler.log & READER=$! airflow scheduler --skip-serve-logs > scheduler.out 2>&1 & SCHED=$! sleep 15 # the scheduler is up, logging at DEBUG level kill -STOP $READER # stop reading the pipe: it fills, and the next log write blocks sleep 30 kill -TERM $SCHED # arrives while the main thread is inside WriteLogger.msg() kill -CONT $READER # the pipe is read again sleep 20 kill -0 $SCHED && echo "scheduler still running 20 s after SIGTERM" grep -c 'Exiting gracefully' scheduler.log ``` Output: ``` scheduler still running 20 s after SIGTERM 0 ``` When the pipe is read normally, the same SIGTERM stops the scheduler within a second, and its log ends with `Exiting gracefully upon receiving signal 15`. The hung scheduler ends with `kill -HUP $SCHED`, because SIGHUP has no handler. To see the Python frames, I loaded `faulthandler.register(signal.SIGINFO, all_threads=True)` from a `sitecustomize.py` on `PYTHONPATH` and sent `kill -INFO $SCHED` to the hung scheduler. Its only thread: ``` Current thread 0x00000001ed166240 (most recent call first): File ".../site-packages/structlog/_output.py", line 221 in msg File ".../site-packages/structlog/_base.py", line 224 in _proxy_to_logger File ".../site-packages/airflow/_shared/logging/structlog.py", line 141 in meth File ".../site-packages/airflow/jobs/scheduler_job_runner.py", line 561 in _exit_gracefully File ".../site-packages/structlog/_output.py", line 223 in msg File ".../site-packages/structlog/_base.py", line 224 in _proxy_to_logger File ".../site-packages/airflow/_shared/logging/structlog.py", line 141 in meth File ".../site-packages/airflow/executors/base_executor.py", line 551 in _emit_metrics File ".../site-packages/airflow/executors/base_executor.py", line 516 in heartbeat File ".../site-packages/airflow/jobs/scheduler_job_runner.py", line 2082 in _run_scheduler_loop File ".../site-packages/airflow/jobs/scheduler_job_runner.py", line 1868 in _execute File ".../site-packages/airflow/jobs/job.py", line 399 in execute_job File ".../site-packages/airflow/jobs/job.py", line 370 in run_job ... ``` Line 223 of `_output.py` is `self._flush()` inside `with self._lock:`, and line 221 is `with self._lock:` itself. ### What you think should happen instead? SIGINT and SIGTERM should always stop the scheduler and the triggerer, and `airflow standalone` should reach `Complete` after Ctrl+C. Possible directions (I haven't tried a fix): don't log inside the signal handlers (record the signal and log it from the main loop), or make the write path safe to re-enter from a handler. In standalone, waiting for the components to exit after the SIGINT before sending SIGTERM would also avoid the second signal. ### Operating System macOS 26.7.4 (arm64) ### Deployment Virtualenv installation ### Apache Airflow Provider(s) _No response_ ### Versions of Apache Airflow Providers Not relevant (airflow-core). structlog 26.1.0 from the 3.4.0 constraints. ### Official Helm Chart version Not Applicable ### Kubernetes Version Not Applicable ### Helm Chart configuration Not Applicable ### Docker Image customizations Not Applicable ### Anything else? #19260 reported the same symptom on 2.2 (`Shutting down components`, then a hang). It was closed in 2023 after it stopped reproducing on 2.6, and its discussion pointed at the LocalExecutor and triggerer shutdown, not at logging. The hang isn't limited to standalone: any SIGINT or SIGTERM that arrives while the main thread is writing a log line has the same effect. Standalone just makes it likely by sending two signals milliseconds apart. ### Are you willing to submit PR? - [ ] 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]
