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

kaxil pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new a4a5ae6dc00 Fix `dag.test()` Debug Console hanging under IDE debuggers 
(#77297)
a4a5ae6dc00 is described below

commit a4a5ae6dc00df805f9d8c1cc35a7aa5f3ca4f582
Author: Ephraim Anierobi <[email protected]>
AuthorDate: Fri Nov 20 12:07:29 2026 +0100

    Fix `dag.test()` Debug Console hanging under IDE debuggers (#77297)
    
    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.
---
 .../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 60bfe223f88..1676409bb04 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
@@ -128,6 +128,7 @@ class InProcessExecutionAPI:
 
     _app: FastAPI | None = None
     triggerer_id: int | None = None
+    serve_while_debugger_paused: bool = False
 
     @cached_property
     def app(self):
@@ -157,6 +158,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()
 
         app = self.app
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 ea53e73f602..16ff5893ef2 100644
--- a/task-sdk/src/airflow/sdk/execution_time/supervisor.py
+++ b/task-sdk/src/airflow/sdk/execution_time/supervisor.py
@@ -2524,7 +2524,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 2e831af490e..53d16ef597e 100644
--- a/task-sdk/tests/task_sdk/execution_time/test_supervisor.py
+++ b/task-sdk/tests/task_sdk/execution_time/test_supervisor.py
@@ -6265,6 +6265,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