Skip to content

Upload logs of still-running triggers when the triggerer shuts down - #76514

Open
hkc-8010 wants to merge 1 commit into
apache:mainfrom
hkc-8010:fix/triggerer-upload-logs-on-shutdown
Open

hkc-8010 wants to merge 1 commit into
apache:mainfrom
hkc-8010:fix/triggerer-upload-logs-on-shutdown

Conversation

@hkc-8010

@hkc-8010 hkc-8010 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

When the triggerer stops (a deploy, a pod replacement, any SIGTERM), every trigger that is still running loses what it logged on that triggerer. A trigger's log is uploaded to remote storage only when the runner reports the trigger finished, and a trigger that is running at shutdown never reports that. Its file stays on the local disk of a pod that is going away. The trigger then resumes on another triggerer, which writes its own .trigger.<job_id>.log, so the task log in the UI jumps from "Pausing task as DEFERRED" straight to the new triggerer's "trigger ... starting", with nothing in between.

We hit this on a production deployment where an image deploy restarted the triggerer while 32 triggers were running, and a data-wait trigger lost three hours of log.

Airflow 2 did not have this gap. TriggererHandlerWrapper.close() closed every open per-trigger handler at shutdown, and the object-store task handlers upload on close (see #37206). That path went away when the triggerer moved to TriggerRunnerSupervisor.

Change

After kill() in TriggererJobRunner._execute, the supervisor now:

  1. shuts the runner's sockets for reading (stop_taking_output()), then drains them with the existing cleanup_sockets_after_kill(). kill() can return with log lines still unread, for example the "trigger completed" lines the runner writes as it cancels its triggers. Shutting the read side first makes the drain stop at what is already queued, so a trigger's child process that inherited the runner's stdout and keeps writing cannot keep it running forever (Socket drain after a kill never ends while a child process keeps writing #76867).
  2. uploads and closes every entry still in logger_cache. close_trigger_log_files() (added in Give each asset watcher its own log file on the triggerer #76527) did this for watcher files only; it now covers task trigger logs too, through the same 16-thread upload_and_close_all(). A failed upload does not stop the others and every file is closed.

This also covers the second item of #76867: the watcher files are now uploaded after the drain, so the runner's last lines reach them.

Testing

  • test_execute_closes_trigger_files_after_the_runner_exits: _execute calls kill, stop_taking_output, cleanup_sockets_after_kill, then close_trigger_log_files, in that order.
  • test_close_trigger_log_files_uploads_every_trigger_log: watcher and task trigger logs are both uploaded and closed, and a failing upload does not stop the others.
  • test_trigger_log_uploaded_when_triggerer_stops_before_trigger_finishes: starts the real runner subprocess with a 1 hour TimeDeltaTrigger, shuts it down, and checks the uploaded file has both the "starting" line and the "trigger completed" line written during shutdown. Skipped on macOS, where shutdown(SHUT_RD) discards queued data.
  • test_drain_after_kill_ends_while_a_trigger_child_keeps_writing: a real child process floods the socket; the drain returns and still delivers the line queued before the kill.
  • I reverted each part in turn (no shutdown(), watcher-only upload, no drain call) and each fails its test on the assertion that names the bug.
  • test_triggerer_job.py passes in full (346 passed), and the tests above pass on Linux / Python 3.11 in the CI image.

No new deadline was added. With 16 upload threads, the 1000 triggers at 50 ms each case from review (51s serially) works out to about 3s. That is arithmetic, I did not re-measure it.

No newsfragment, since this is a bugfix.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: Claude Code following the guidelines

🤖 Generated with Claude Code

@hkc-8010
hkc-8010 marked this pull request as ready for review November 5, 2026 04:41
@eladkal eladkal added this to the Airflow 3.4.1 milestone Nov 5, 2026
@eladkal eladkal added type:bug-fix Changelog: Bug Fixes backport-to-v3-4-test Backport to v3-4-test labels Nov 5, 2026

@seanghaeli seanghaeli left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @o-nikolas for pointing this PR out

A trigger's log was only uploaded when the runner reported it finished, so
triggers still running when the triggerer stopped lost everything they had
logged there. After kill(), shut the runner's sockets for reading, drain
them, and upload and close every trigger log still open through the
parallel watcher-log upload pool.

Shutting the sockets for reading first keeps the drain from running forever
when a trigger left a child process that keeps writing to the runner's
stdout.

Co-Authored-By: Claude <[email protected]>
@hkc-8010
hkc-8010 force-pushed the fix/triggerer-upload-logs-on-shutdown branch from e6059af to 805197b Compare November 28, 2026 05:10
@hkc-8010

Copy link
Copy Markdown
Contributor Author

@seanghaeli I rebased onto main after #76527 and addressed both of your comments, replies are in the threads. Could you take another look when you get a chance?

@hkc-8010
hkc-8010 requested a review from seanghaeli November 28, 2026 05:21

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Triggerer backport-to-v3-4-test Backport to v3-4-test type:bug-fix Changelog: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants