This is an automated email from the ASF dual-hosted git repository.

eladkal pushed a commit to branch v3-4-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/v3-4-test by this push:
     new d8c0139fdc0 [v3-4-test] Fix `dag.test()` Debug Console hanging under 
IDE debuggers (#77297) (#77866)
d8c0139fdc0 is described below

commit d8c0139fdc01f7fe29a91434faa56ca948ff60c6
Author: Rahul Vats <[email protected]>
AuthorDate: Tue Dec 1 19:39:47 2026 +0530

    [v3-4-test] Fix `dag.test()` Debug Console hanging under IDE debuggers 
(#77297) (#77866)
    
    When a breakpoint stops a task that dag.test() runs under a pydevd-based
    debugger (VS Code's debugpy, PyCharm), pydevd suspends every other thread,
    including the event loop thread the in-process Execution API answers on.
    Debug Console expressions that call the Execution API, such as xcom_pull or
    Variable.get, then never return. Airflow 2's dag.test() ran tasks in the
    calling thread, so these evaluations worked before dag.test() moved to the
    Task SDK.
    
    Only the instance whose callers are in the same process keeps its loop
    running. The Dag processor's and the triggerer's callers are other processes
    relayed by the main thread, which a pause stops anyway, and leaving their
    loop threads traced keeps breakpoints in route code working there.
    (cherry picked from commit a4a5ae6dc00df805f9d8c1cc35a7aa5f3ca4f582)
    
    Co-authored-by: Ephraim Anierobi <[email protected]>
    Co-authored-by: Claude <[email protected]>
---
 .../src/airflow/api_fastapi/execution_api/in_process.py |  6 ++++++
 .../unit/api_fastapi/execution_api/test_in_process.py   | 17 +++++++++++++++++
 task-sdk/src/airflow/sdk/execution_time/supervisor.py   |  2 +-
 .../tests/task_sdk/execution_time/test_supervisor.py    |  9 +++++++++
 4 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/airflow-core/src/airflow/api_fastapi/execution_api/in_process.py 
b/airflow-core/src/airflow/api_fastapi/execution_api/in_process.py
index 9f4713c8cff..9647511cb02 100644
--- a/airflow-core/src/airflow/api_fastapi/execution_api/in_process.py
+++ b/airflow-core/src/airflow/api_fastapi/execution_api/in_process.py
@@ -100,6 +100,7 @@ class InProcessExecutionAPI:
 
     _app: FastAPI | None = None
     triggerer_id: int | None = None
+    serve_while_debugger_paused: bool = False
 
     @cached_property
     def app(self):
@@ -129,6 +130,11 @@ class InProcessExecutionAPI:
         # Own the event loop and thread so the transport controls their 
lifecycle.
         loop = asyncio.new_event_loop()
         thread = threading.Thread(target=loop.run_forever, 
name="InProcessExecutionAPI-loop", daemon=True)
+        if self.serve_while_debugger_paused:
+            # pydevd (debugpy, PyCharm) suspends every other thread at a 
breakpoint, so code paused in
+            # this process could not reach the API. The attribute also stops 
tracing in this thread:
+            # breakpoints in route code don't stop for requests this instance 
serves.
+            thread.pydev_do_not_trace = True  # type: ignore[attr-defined]
         thread.start()
 
         class PEP3333PathMiddleware(ASGIMiddleware):
diff --git 
a/airflow-core/tests/unit/api_fastapi/execution_api/test_in_process.py 
b/airflow-core/tests/unit/api_fastapi/execution_api/test_in_process.py
index 3f093721620..9bedecffe88 100644
--- a/airflow-core/tests/unit/api_fastapi/execution_api/test_in_process.py
+++ b/airflow-core/tests/unit/api_fastapi/execution_api/test_in_process.py
@@ -19,8 +19,13 @@ from __future__ import annotations
 import os
 import subprocess
 import sys
+import threading
 from textwrap import dedent
 
+import pytest
+
+from airflow.api_fastapi.execution_api.in_process import InProcessExecutionAPI
+
 
 def test_construction_defers_execution_api_imports():
     result = subprocess.run(
@@ -58,3 +63,15 @@ def test_app_reexports_in_process_api():
     from airflow.api_fastapi.execution_api.in_process import 
InProcessExecutionAPI
 
     assert legacy_api is InProcessExecutionAPI
+
+
[email protected]("serve_while_debugger_paused", [True, False])
+def test_transport_loop_thread_debugger_marker(serve_while_debugger_paused):
+    before = set(threading.enumerate())
+
+    # Held until the assertion: dropping the transport stops and joins the 
loop thread.
+    transport = 
InProcessExecutionAPI(serve_while_debugger_paused=serve_while_debugger_paused).transport
+
+    (thread,) = {t for t in threading.enumerate() if t.name == 
"InProcessExecutionAPI-loop"} - before
+    assert getattr(thread, "pydev_do_not_trace", False) is 
serve_while_debugger_paused
+    del transport
diff --git a/task-sdk/src/airflow/sdk/execution_time/supervisor.py 
b/task-sdk/src/airflow/sdk/execution_time/supervisor.py
index 42430e3fb47..3f82ef2f9e7 100644
--- a/task-sdk/src/airflow/sdk/execution_time/supervisor.py
+++ b/task-sdk/src/airflow/sdk/execution_time/supervisor.py
@@ -2509,7 +2509,7 @@ class ActivitySubprocess(WatchedSubprocess):
 def in_process_api_server():
     from airflow.api_fastapi.execution_api.app import InProcessExecutionAPI
 
-    api = InProcessExecutionAPI()
+    api = InProcessExecutionAPI(serve_while_debugger_paused=True)
     return api
 
 
diff --git a/task-sdk/tests/task_sdk/execution_time/test_supervisor.py 
b/task-sdk/tests/task_sdk/execution_time/test_supervisor.py
index 12a76d25a1b..f978c65061c 100644
--- a/task-sdk/tests/task_sdk/execution_time/test_supervisor.py
+++ b/task-sdk/tests/task_sdk/execution_time/test_supervisor.py
@@ -5891,6 +5891,15 @@ def test_in_process_api_server_caches_instance():
         in_process_api_server.cache_clear()
 
 
+def test_in_process_api_server_serves_while_debugger_paused():
+    """dag.test() calls the API from the thread a debugger pauses, so the 
API's loop must keep running."""
+    in_process_api_server.cache_clear()
+    try:
+        assert in_process_api_server().serve_while_debugger_paused is True
+    finally:
+        in_process_api_server.cache_clear()
+
+
 def test_api_client_clears_dag_bag_override_when_dag_is_none():
     """_api_client(dag=None) removes stale dag_bag_from_app overrides set by a 
previous call."""
     from unittest.mock import MagicMock

Reply via email to