hkc-8010 commented on code in PR #76514:
URL: https://github.com/apache/airflow/pull/76514#discussion_r4725622102


##########
airflow-core/src/airflow/jobs/triggerer_job_runner.py:
##########
@@ -317,6 +317,10 @@ def _execute(self) -> int | None:
             # to force-kill here. trigger_runner may be None if start() raised.
             if self.trigger_runner is not None:
                 self.trigger_runner.kill(escalation_delay=10, force=True)
+                # kill() can return with log lines still unread, e.g. the 
"trigger completed" lines the
+                # runner writes as it cancels its triggers, so drain the 
sockets before uploading.
+                self.trigger_runner.cleanup_sockets_after_kill()

Review Comment:
   Thanks for testing this with a real child process. I took the suggestion: 
`stop_taking_output()` now shuts the runner's sockets for reading before 
`cleanup_sockets_after_kill()`.
   
   Added `test_drain_after_kill_ends_while_a_trigger_child_keeps_writing`. It 
floods the socket from a real child process and checks that the drain returns 
and still delivers the line queued before the kill. With the `shutdown()` 
removed it fails on the 5s join.
   
   One platform note: on macOS `shutdown(SHUT_RD)` discards what is already 
queued (Linux keeps it), so there the drain stops without hanging but loses the 
last lines. I skip the line assertions on darwin instead of working around it.
   
   #77349 makes the same change inside `cleanup_sockets_after_kill()` itself. 
If that lands first I'll drop `stop_taking_output()` from here.
   



##########
airflow-core/src/airflow/jobs/triggerer_job_runner.py:
##########
@@ -835,6 +832,31 @@ def _handle_request(self, msg: ToTriggerSupervisor, log: 
FilteringBoundLogger, r
 
         self.send_msg(resp, request_id=req_id, error=None, **dump_opts)
 
+    @staticmethod
+    def _upload_trigger_log(
+        trigger_id: int, factory: TriggerLoggingFactory, log: 
FilteringBoundLogger
+    ) -> None:
+        try:
+            factory.upload_to_remote()
+        except Exception:
+            log.exception("Failed to upload trigger logs to remote", 
trigger_id=trigger_id)
+        finally:
+            # Close the FD explicitly even if upload raised, otherwise the file
+            # handle leaks for every failed upload.
+            factory.close()
+
+    def upload_remaining_trigger_logs(self) -> None:
+        """
+        Upload the logs of triggers that never finished here, for use once the 
runner has exited.
+
+        A trigger's log is otherwise only uploaded when the runner reports it 
finished. Triggers that
+        are still running when the triggerer shuts down never report that, so 
without this what they
+        logged here would be lost with the local disk.
+        """
+        while self.logger_cache:

Review Comment:
   Agreed. Since #76527 merged, `close_trigger_log_files()` already sends 
watcher files through `upload_and_close_all()` with 16 threads, so I widened it 
to every log left in `logger_cache` instead of only the watcher ones. The task 
logs of still-running triggers now go through the same pool, and the serial 
loop from the first version is gone.
   
   Together with the drain above, this also covers the second item of #76867 
(drain the runner's sockets before uploading the watcher files on stop).
   



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