shivaam commented on code in PR #75184:
URL: https://github.com/apache/airflow/pull/75184#discussion_r4521711178


##########
task-sdk/src/airflow/sdk/coordinators/_subprocess.py:
##########
@@ -605,6 +658,220 @@ def _handle_set_task_state_store(
                 )
         return super()._handle_set_task_state_store(msg, log, req_id)
 
+    def _monitor_subprocess(self):
+        try:
+            super()._monitor_subprocess()
+        finally:
+            self._stop_xcom_backend()
+
+    def _start_xcom_backend_call(
+        self, call: XComBackendCall, finish: Callable[[XComBackendResult], 
None]
+    ) -> None:
+        """Send ``call`` to the XCom backend process, which starts on the 
first call it handles."""
+        if self._xcom_backend is not None and not 
self._xcom_backend.can_run_a_call:
+            self._stop_xcom_backend()
+        if self._xcom_backend is None:
+            self._xcom_backend = XComBackendProcess.start(
+                id=self.id,
+                client=self.client,
+                logger=self.process_log,
+                selector=self.selector,
+            )
+        self._xcom_backend.send_call(call, finish)
+
+    def _stop_xcom_backend(self) -> None:
+        """Stop the XCom backend process, so that the next call starts one 
that can answer it."""
+        if (backend := self._xcom_backend) is not None:
+            self._xcom_backend = None
+            backend.stop()
+
+    def _call_xcom_backend(
+        self,
+        call: XComBackendCall,
+        then: Callable[[XComBackendResult], RequestResult],
+        req_id: int,
+    ) -> ResponseSent:
+        """
+        Answer request ``req_id`` with ``then(result)`` once the XCom backend 
has run ``call``.
+
+        The request loop keeps servicing the task while the call runs. A call 
that fails fails the request.
+        """
+
+        def answer(resp: BaseModel | None, error: ErrorResponse | None, 
**dump_opts: bool) -> None:
+            if not send_unless_exited(self, resp, req_id, error, **dump_opts):
+                self.process_log.warning(
+                    "The runtime exited before its XCom request was answered", 
request_id=req_id
+                )
+
+        def finish(result: XComBackendResult) -> None:
+            try:
+                if result.error is not None:
+                    raise XComBackendError(result.error)
+                resp, dump_opts = then(result)

Review Comment:
   Shall we keep the original API error and HTTP status here? If the backend 
stores the value successfully but the API rejects the database write, we 
currently report it as a backend error. Preserving the API error would make the 
actual failure clearer.



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