amoghrajesh commented on code in PR #74010:
URL: https://github.com/apache/airflow/pull/74010#discussion_r4753099968


##########
task-sdk/src/airflow/sdk/execution_time/comms.py:
##########
@@ -422,8 +438,17 @@ def _read_frame(self, maxfds: int | None = None) -> 
tuple[_ResponseFrame, list[i
             return resp, fds or []
         return resp
 
-    def _from_frame(self, frame) -> ReceiveMsgType | None:
-        from airflow.sdk.exceptions import AirflowRuntimeError
+    def _from_frame(self, frame, request_id: int | None = None) -> 
ReceiveMsgType | None:
+        from airflow.sdk.exceptions import AirflowRuntimeError, 
AirflowSupervisorCommsOutOfSync
+
+        # A frame for a different request means the stream is out of step, and 
returning it would hand
+        # the caller another request's data.
+        if request_id is not None and frame.id != request_id:

Review Comment:
   On a mismatch, `_from_frame` raises and leaves the real reply on the socket. 
The next request then reads the wrong frame again, so one orphan ack breaks 
every later supervisor call, including the final task state send. Log and skip 
frames with an older id, and raise only for a newer id. If the PR keeps the 
plain raise, document that the task is expected to fail.



##########
task-sdk/tests/task_sdk/execution_time/test_secrets.py:
##########
@@ -175,6 +179,110 @@ async def asend(*_args, **_kwargs):
             await backend.aget_variable("denied_var")
 
 
+class TestOutOfSyncSupervisorComms:
+    """A desynced supervisor connection must surface, not turn into "not 
found".
+
+    Both the backend and the dispatchers catch ``Exception`` to fall through 
to the next backend,
+    so each of them needs its own re-raise for the error to reach the task.
+    """
+
+    @pytest.mark.parametrize("method", ["get_connection", "get_variable"])
+    def test_backend_raises(self, mock_supervisor_comms, method):
+        mock_supervisor_comms.send.side_effect = 
AirflowSupervisorCommsOutOfSync("out of sync")
+
+        with pytest.raises(AirflowSupervisorCommsOutOfSync, match="out of 
sync"):
+            getattr(ExecutionAPISecretsBackend(), method)("some_key")
+
+    @pytest.mark.asyncio
+    @pytest.mark.parametrize("method", ["aget_connection", "aget_variable"])
+    async def test_backend_raises_async(self, mock_supervisor_comms, method):
+        async def asend(*_args, **_kwargs):
+            raise AirflowSupervisorCommsOutOfSync("out of sync")
+
+        mock_supervisor_comms.asend = asend
+
+        with pytest.raises(AirflowSupervisorCommsOutOfSync, match="out of 
sync"):
+            await getattr(ExecutionAPISecretsBackend(), method)("some_key")
+
+    def test_get_connection_does_not_fall_through(self, mock_supervisor_comms, 
monkeypatch):

Review Comment:
   Four new tests (lines 207, 224, 242, 264) copy the setup of the existing 
deny tests at lines 297, 320 and 344. They differ only in the exception raised. 
Parametrize the existing tests over `AirflowSecretsBackendAccessDenied` and 
`AirflowSupervisorCommsOutOfSync`, with ids `denied` and `out_of_sync`, and 
delete the copies.



##########
task-sdk/tests/task_sdk/execution_time/test_secrets.py:
##########
@@ -175,6 +179,110 @@ async def asend(*_args, **_kwargs):
             await backend.aget_variable("denied_var")
 
 
+class TestOutOfSyncSupervisorComms:
+    """A desynced supervisor connection must surface, not turn into "not 
found".
+
+    Both the backend and the dispatchers catch ``Exception`` to fall through 
to the next backend,
+    so each of them needs its own re-raise for the error to reach the task.
+    """
+
+    @pytest.mark.parametrize("method", ["get_connection", "get_variable"])
+    def test_backend_raises(self, mock_supervisor_comms, method):
+        mock_supervisor_comms.send.side_effect = 
AirflowSupervisorCommsOutOfSync("out of sync")
+
+        with pytest.raises(AirflowSupervisorCommsOutOfSync, match="out of 
sync"):
+            getattr(ExecutionAPISecretsBackend(), method)("some_key")
+
+    @pytest.mark.asyncio
+    @pytest.mark.parametrize("method", ["aget_connection", "aget_variable"])
+    async def test_backend_raises_async(self, mock_supervisor_comms, method):
+        async def asend(*_args, **_kwargs):
+            raise AirflowSupervisorCommsOutOfSync("out of sync")
+
+        mock_supervisor_comms.asend = asend
+
+        with pytest.raises(AirflowSupervisorCommsOutOfSync, match="out of 
sync"):
+            await getattr(ExecutionAPISecretsBackend(), method)("some_key")
+
+    def test_get_connection_does_not_fall_through(self, mock_supervisor_comms, 
monkeypatch):
+        from unittest.mock import MagicMock
+
+        from airflow.sdk.execution_time import context as ctx_module
+
+        mock_supervisor_comms.send.side_effect = 
AirflowSupervisorCommsOutOfSync("out of sync")
+        later_backend = MagicMock(name="LaterBackend")

Review Comment:
   Use autospec pls.



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