Skip to content

Commit bff1622

Browse files
committed
Fix pdb / breakpoint() hang in workflow code (#1104)
When debug_mode=True (or TEMPORAL_DEBUG=1), breakpoint() inside workflow code now opens an interactive pdb prompt -- including from a sandboxed workflow run under pytest. Four pieces: - Inline dispatch on the asyncio main thread (via loop.call_soon to avoid nesting inside the dispatch task's __step() and tripping Python 3.14's task-entry validation). - breakpoint removed from the sandbox's invalid builtins so the call reaches the worker hook. Nothing else is relaxed. - A Pdb subclass that lands at the workflow's own frame, suspends sandbox checks during each REPL interaction, and overrides q/Ctrl-D to continue the workflow instead of failing it with BdbQuit. - A defensive sys.breakpointhook that raises a clear RuntimeError when breakpoint() is called from a workflow worker thread without debug_mode, replacing the previous silent hang. When debug_mode is not set, the worker's dispatch and sandbox config are unchanged. Adds a README subsection on debugging workflows and five tests at tests/worker/test_breakpoint_hang.py. Verified on Python 3.13 and 3.14. Closes #1104.
1 parent 7ea54e6 commit bff1622

4 files changed

Lines changed: 558 additions & 44 deletions

File tree

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ informal introduction to the features and their implementation.
8282
- [Customizing the Sandbox](#customizing-the-sandbox)
8383
- [Passthrough Modules](#passthrough-modules)
8484
- [Invalid Module Members](#invalid-module-members)
85+
- [Debugging Workflows with `breakpoint()` / `pdb`](#debugging-workflows-with-breakpoint--pdb)
8586
- [Known Sandbox Issues](#known-sandbox-issues)
8687
- [Global Import/Builtins](#global-importbuiltins)
8788
- [Sandbox is not Secure](#sandbox-is-not-secure)
@@ -1241,6 +1242,79 @@ my_worker = Worker(..., workflow_runner=SandboxedWorkflowRunner(restrictions=my_
12411242

12421243
See the API for more details on exact fields and their meaning.
12431244

1245+
##### Debugging Workflows with `breakpoint()` / `pdb`
1246+
1247+
Setting `debug_mode=True` on the `Worker` (or `TEMPORAL_DEBUG=1` in the environment) routes workflow activations
1248+
onto the asyncio main thread instead of a worker thread pool. This lets `breakpoint()` and `pdb.set_trace()`
1249+
inside workflow code open an interactive REPL — without it, pdb hangs because its `input()` call would run on a
1250+
thread that does not own the controlling TTY.
1251+
1252+
A minimal runnable example:
1253+
1254+
```python
1255+
import asyncio
1256+
from datetime import timedelta
1257+
1258+
from temporalio import workflow
1259+
from temporalio.client import Client
1260+
from temporalio.worker import Worker
1261+
1262+
1263+
@workflow.defn
1264+
class DebugMeWorkflow:
1265+
@workflow.run
1266+
async def run(self) -> str:
1267+
x = 42
1268+
breakpoint() # interactive pdb prompt opens at this line
1269+
return f"x was {x}"
1270+
1271+
1272+
async def main() -> None:
1273+
client = await Client.connect("localhost:7233")
1274+
async with Worker(
1275+
client,
1276+
task_queue="debug-me",
1277+
workflows=[DebugMeWorkflow],
1278+
debug_mode=True,
1279+
):
1280+
result = await client.execute_workflow(
1281+
DebugMeWorkflow.run,
1282+
id="debug-me-wf",
1283+
task_queue="debug-me",
1284+
task_timeout=timedelta(minutes=10), # see caveat below
1285+
)
1286+
print(result)
1287+
1288+
1289+
if __name__ == "__main__":
1290+
asyncio.run(main())
1291+
```
1292+
1293+
Run with `python debug_me.py`, or under pytest with `pytest -s` (the `-s` flag disables pytest's stdin
1294+
capture). At the `(Pdb)` prompt you'll land at the line where `breakpoint()` was called, with workflow
1295+
locals in scope. Try `p x`, `n`, `c`, `q`.
1296+
1297+
**Quitting cleanly.** Typing `q` or hitting Ctrl-D continues the workflow rather than raising `BdbQuit`
1298+
(which would fail the workflow task). To genuinely abort, kill the outer process with Ctrl-C.
1299+
1300+
Two caveats when pausing at a breakpoint inside a workflow:
1301+
1302+
1. **Workflow task timeout.** Temporal expires a workflow task after ~10 seconds by default. If you sit at the
1303+
`(Pdb)` prompt longer than that, the server reassigns the task and your workflow replays from the start when
1304+
you continue — re-hitting the breakpoint. Pass `task_timeout=timedelta(minutes=N)` to `execute_workflow` /
1305+
`start_workflow` to give yourself debugging headroom:
1306+
1307+
```python
1308+
await client.execute_workflow(MyWorkflow.run, ..., task_timeout=timedelta(minutes=10))
1309+
```
1310+
1311+
2. **Deterministic replay.** Workflows are deterministic and replay from history; any wall-clock pause violates
1312+
that contract. For post-mortem debugging without these caveats, use the [Replayer](#replayer) on a recorded
1313+
history instead of live debugging.
1314+
1315+
A `breakpoint()` call from workflow code without `debug_mode` enabled raises a `RuntimeError` with a pointer to
1316+
this section, so the failure mode is loud rather than a silent hang.
1317+
12441318
##### Known Sandbox Issues
12451319

12461320
Below are known sandbox issues. As the sandbox is developed and matures, some may be resolved.

temporalio/worker/_debugger.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
"""Workflow debugger support.
2+
3+
When ``debug_mode=True`` on the Worker (or the ``TEMPORAL_DEBUG`` env var
4+
is set), the worker uses helpers from this module to make ``breakpoint()``
5+
inside workflow code open an interactive pdb prompt. The inline-dispatch
6+
piece lives on the worker itself; everything else (sandbox relaxation,
7+
breakpoint hook, custom Pdb subclass) lives here.
8+
"""
9+
10+
from __future__ import annotations
11+
12+
import dataclasses
13+
import sys
14+
import threading
15+
from types import FrameType, TracebackType
16+
17+
import temporalio.workflow
18+
from temporalio.worker.workflow_sandbox._runner import SandboxedWorkflowRunner
19+
20+
from ._workflow_instance import WorkflowRunner
21+
22+
__all__ = [
23+
"_install_workflow_breakpoint_hook",
24+
"_relax_sandbox_for_debugger",
25+
"_temporal_workflow_breakpoint_hook",
26+
]
27+
28+
# Prefix used to detect threads in the workflow task ThreadPoolExecutor.
29+
_WORKFLOW_THREAD_NAME_PREFIX = "temporal_workflow_"
30+
31+
_ORIGINAL_BREAKPOINTHOOK = sys.breakpointhook
32+
33+
34+
def _build_workflow_pdb_class() -> type:
35+
"""Build a Pdb subclass that suspends sandbox restrictions during the REPL.
36+
37+
pdb's cmdloop touches ``readline.get_completer`` and other
38+
sandbox-restricted internals each time it interacts with the user; we
39+
bracket each interaction with ``_sandbox_unrestricted.value = True`` and
40+
restore the previous value afterwards. Outside the REPL the sandbox
41+
stays intact.
42+
43+
``pdb`` is imported lazily because it's a debug-only dependency that
44+
pulls in ``cmd``/``bdb``/``linecache``; no reason to pay that cost at
45+
worker import time.
46+
"""
47+
import pdb
48+
49+
from temporalio.workflow._sandbox import _sandbox_unrestricted
50+
51+
class _WorkflowPdb(pdb.Pdb):
52+
# The `interaction` signature differs across Python versions: 3.10-3.12
53+
# typeshed names the second parameter `traceback: TracebackType | None`,
54+
# while 3.13+ renames it `tb_or_exc` and widens the type to include
55+
# `BaseException`. No single signature satisfies both stubs, so we
56+
# suppress the override check.
57+
def interaction( # type: ignore[override]
58+
self,
59+
frame: FrameType | None,
60+
tb_or_exc: TracebackType | BaseException | None,
61+
) -> None:
62+
prev = getattr(_sandbox_unrestricted, "value", False)
63+
_sandbox_unrestricted.value = True
64+
try:
65+
super().interaction(frame, tb_or_exc) # type: ignore[arg-type]
66+
finally:
67+
_sandbox_unrestricted.value = prev
68+
69+
# Override `q`/`quit`/`exit`/EOF (Ctrl-D) to behave like `continue`.
70+
# Default pdb raises `BdbQuit`, which propagates as an uncaught
71+
# exception out of workflow.run, fails the workflow task, and
72+
# triggers a server retry storm during teardown. For a debug
73+
# session the user almost always wants "stop debugging and let the
74+
# workflow finish" — that's `continue`. Users who truly want to
75+
# abort can Ctrl-C the outer shell.
76+
def do_quit(self, arg: str) -> bool | None:
77+
self.message(
78+
"[Temporal] 'q'/Ctrl-D continues the workflow. "
79+
"Ctrl-C the outer shell to abort."
80+
)
81+
return self.do_continue(arg)
82+
83+
do_q = do_exit = do_quit
84+
do_EOF = do_quit
85+
86+
return _WorkflowPdb
87+
88+
89+
def _temporal_workflow_breakpoint_hook(*args: object, **kwargs: object) -> object:
90+
"""Process-wide ``sys.breakpointhook`` that handles ``breakpoint()`` calls.
91+
92+
From a workflow worker thread without ``debug_mode``: raises a clear
93+
``RuntimeError`` (replacing the previous silent hang). From inside a
94+
workflow activation (with ``debug_mode`` on): drops the user into a
95+
custom Pdb at the workflow's own frame. From anywhere else: delegates
96+
to whatever hook was previously installed.
97+
"""
98+
if threading.current_thread().name.startswith(_WORKFLOW_THREAD_NAME_PREFIX):
99+
raise RuntimeError(
100+
"breakpoint() / pdb.set_trace() inside workflow code requires "
101+
"debug_mode=True (or the TEMPORAL_DEBUG environment variable) on "
102+
"the Worker. Without it the workflow runs on a thread pool and "
103+
"pdb's interactive REPL cannot read stdin."
104+
)
105+
if not temporalio.workflow.in_workflow():
106+
# Not inside a workflow activation — let pytest's wrapper, ipdb, or
107+
# whatever else is configured handle it.
108+
return _ORIGINAL_BREAKPOINTHOOK(*args, **kwargs)
109+
# Inside a workflow: drop the user into pdb at the caller's frame (the
110+
# workflow's `run` method, where breakpoint() was actually written) rather
111+
# than landing inside this hook. Bypassing the configured breakpoint hook
112+
# also avoids pytest's pdb wrapper, which assumes a test-code context and
113+
# touches sandbox-restricted internals during its terminal-writer setup.
114+
# `sandbox_unrestricted()` lifts member checks for the duration of the
115+
# REPL so pdb's own initialization (readline, etc.) isn't blocked.
116+
# `skip` tells pdb not to stop in our hook frame or the contextlib
117+
# plumbing — without it pdb's first step lands at the `with` teardown
118+
# instead of the user's next workflow line.
119+
caller_frame = sys._getframe(1)
120+
with temporalio.workflow.unsafe.sandbox_unrestricted():
121+
pdb_cls = _build_workflow_pdb_class()
122+
pdb_cls(
123+
skip=[
124+
"temporalio.worker._debugger",
125+
"temporalio.workflow._sandbox",
126+
"contextlib",
127+
]
128+
).set_trace(caller_frame)
129+
return None
130+
131+
132+
def _install_workflow_breakpoint_hook() -> None:
133+
"""Set ``sys.breakpointhook`` to the workflow hook if it isn't already."""
134+
if sys.breakpointhook is not _temporal_workflow_breakpoint_hook:
135+
sys.breakpointhook = _temporal_workflow_breakpoint_hook
136+
137+
138+
def _relax_sandbox_for_debugger(workflow_runner: WorkflowRunner) -> WorkflowRunner:
139+
"""Allow ``breakpoint()`` past the sandbox so it can reach the worker hook.
140+
141+
The sandbox flags ``breakpoint`` as non-deterministic by default; without
142+
this relaxation the call raises before our breakpoint hook can run.
143+
Once inside the hook, the hook itself enters ``sandbox_unrestricted()``
144+
for the duration of the debugger session, so pdb's internals (readline,
145+
os.environ, etc.) aren't blocked either — without permanently dropping
146+
sandbox checks for the rest of workflow execution.
147+
"""
148+
if not isinstance(workflow_runner, SandboxedWorkflowRunner):
149+
return workflow_runner
150+
151+
restrictions = workflow_runner.restrictions
152+
invalid = restrictions.invalid_module_members
153+
builtins_matcher = invalid.children.get("__builtins__")
154+
if builtins_matcher is None or "breakpoint" not in builtins_matcher.use:
155+
return workflow_runner
156+
157+
new_use = set(builtins_matcher.use) - {"breakpoint"}
158+
new_builtins = dataclasses.replace(builtins_matcher, use=new_use)
159+
new_invalid = dataclasses.replace(
160+
invalid, children={**invalid.children, "__builtins__": new_builtins}
161+
)
162+
new_restrictions = dataclasses.replace(
163+
restrictions, invalid_module_members=new_invalid
164+
)
165+
return dataclasses.replace(workflow_runner, restrictions=new_restrictions)

0 commit comments

Comments
 (0)