dabla commented on code in PR #76545:
URL: https://github.com/apache/airflow/pull/76545#discussion_r4676701235


##########
task-sdk/tests/task_sdk/execution_time/test_comms.py:
##########
@@ -359,74 +434,319 @@ async def _async_phase():
             asyncio.set_event_loop(None)
 
         # --- Phase 2: sync send() after the loop is closed (simulates 
_push_xcom_if_needed) ---
-        # _loop_thread_id is still set from phase 1, but the loop is no longer 
running.
-        # This must NOT raise DeadlockImminentError.
-        server2 = threading.Thread(target=_serve_one, args=(w,), daemon=True)
+        server2 = threading.Thread(target=self._serve, args=(w, 1), 
daemon=True)
         server2.start()
 
         result = decoder.send(GetVariable(key="sync_key"))
         server2.join(timeout=2)
 
         assert result is not None
 
-    def 
test_send_from_paused_event_loop_thread_raises_when_asend_in_flight(self, 
socket_pair):
+    @staticmethod
+    def _serve(sock, count: int, delay: float = 0.0) -> None:
+        """Answer ``count`` requests with their own key, after ``delay`` 
seconds each; stop on a closed socket."""
+        try:
+            for _ in range(count):
+                request = _read_request(sock)
+                time.sleep(delay)
+                _respond(sock, request)
+        except (OSError, EOFError):
+            pass
+
+    # Waiting for its own thread would never end: the limit turns a regression 
into a failure.
+    @pytest.mark.execution_timeout(10)
+    def 
test_send_on_a_thread_already_in_a_request_raises_instead_of_waiting(self, 
socket_pair):
+        """
+        A call on a thread that holds the channel for its own request would 
wait for itself: the
+        one real deadlock left, so it raises at once, and the interrupted 
request still completes.
+        """
+        r, w = socket_pair
+        decoder = CommsDecoder(socket=r, log=structlog.get_logger())
+        threading.Thread(target=self._serve, args=(w, 1), daemon=True).start()
+        raised: list[BaseException] = []
+        original_get_response = CommsDecoder._get_response
+
+        def interrupted(self):
+            # What a signal handler would do while this thread waits for the 
response.
+            try:
+                self.send(GetVariable(key="from the handler"))
+            except DeadlockImminentError as exc:
+                raised.append(exc)
+            return original_get_response(self)
+
+        with mock.patch.object(CommsDecoder, "_get_response", interrupted):
+            result = decoder.send(GetVariable(key="outer"))
+
+        assert result.key == "outer"
+        assert len(raised) == 1
+        assert "already in the middle of a request" in str(raised[0])
+        assert not _is_taken(decoder._thread_lock)
+
+    # Waiting for its own thread would never end: the limit turns a regression 
into a failure.

Review Comment:
   Kept it once above the first marker and dropped the other two in 95cbc63cdd.



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