From a56b03b93634438e656b349be22298691e816666 Mon Sep 17 00:00:00 2001 From: Ash Berlin-Taylor Date: Fri, 13 Dec 2024 11:25:11 +0000 Subject: [PATCH] Better error handling to prevent flaky test_last_chance_exception_handling test (#44909) If the launched subprocess (which in tests just _immediately_ raises an Exception) exits very quickly before we even try to send the startup message it would fail with a BrokenPipeError. All we need to do in this case is handle it as the exit code of the task and it's message (which we already test) will cover it. This particular behaviour is hard to reliably catch in tests, so no tests are added here. --- task_sdk/src/airflow/sdk/execution_time/supervisor.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/task_sdk/src/airflow/sdk/execution_time/supervisor.py b/task_sdk/src/airflow/sdk/execution_time/supervisor.py index cb5554681b303..677030b7bdce2 100644 --- a/task_sdk/src/airflow/sdk/execution_time/supervisor.py +++ b/task_sdk/src/airflow/sdk/execution_time/supervisor.py @@ -412,8 +412,13 @@ def _on_child_started(self, ti: TaskInstance, path: str | os.PathLike[str], requ # Send the message to tell the process what it needs to execute log.debug("Sending", msg=msg) - self.stdin.write(msg.model_dump_json().encode()) - self.stdin.write(b"\n") + + try: + self.stdin.write(msg.model_dump_json().encode()) + self.stdin.write(b"\n") + except BrokenPipeError: + # Debug is fine, the process will have shown _something_ in it's last_chance exception handler + log.debug("Couldn't send startup message to Subprocess - it died very early", pid=self.pid) def kill( self,