Skip to content

Commit 67fe256

Browse files
committed
Fix pdb / breakpoint() hang in workflow code (temporalio#1104)
Closes temporalio#1104. breakpoint() and pdb.set_trace() inside workflow code silently hang even with debug_mode=True and an unsandboxed runner. Three orthogonal issues contribute; this PR addresses all three behind the existing debug_mode flag so production behavior is unchanged. 1. Thread placement. Activations run on a ThreadPoolExecutor worker thread, so pdb's cmdloop() calls input() from a thread that doesn't own the controlling TTY. Fixed by scheduling the activation as a loop.call_soon callback and awaiting a future the callback completes. The dispatch task suspends at the await so it's no longer mid-__step() when the workflow's internal task stepping happens. (A direct synchronous call ran afoul of Python 3.14's tightened asyncio task-entry validation: "Cannot enter into task while another task is being executed.") 2. Sandbox restriction. The sandbox flags `breakpoint` and `input` as non-deterministic builtins. With debug_mode=True the user has explicitly accepted non-determinism for the debugging session, so we relax those two specific restrictions when the runner is a SandboxedWorkflowRunner. Other sandbox checks remain intact. 3. Silent-hang failure mode. Installs a process-wide sys.breakpointhook at worker startup that raises a clear RuntimeError when breakpoint() is called from a workflow worker thread without debug_mode, replacing the silent hang. Adds a "Debugging Workflows with breakpoint() / pdb" subsection to the README under Workflow Sandbox, including a runnable example and the caveats around workflow task timeouts. Tests at tests/worker/test_breakpoint_hang.py cover thread placement in both modes, the sandboxed-workflow path, and the defensive hook. Verified on Python 3.13 and 3.14 locally; CI matrix green on fork. The load-bearing observation for the dispatch fix: `await future` suspends the dispatch task such that asyncio no longer considers it "currently executing," even though it's still in a pending state. That's what lets workflow.activate(act) step the workflow's internal task without 3.14's task-entry error.
1 parent 7ea54e6 commit 67fe256

3 files changed

Lines changed: 370 additions & 46 deletions

File tree

README.md

Lines changed: 70 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,75 @@ 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(sandboxed=False)
1264+
class DebugMeWorkflow:
1265+
@workflow.run
1266+
async def run(self) -> str:
1267+
x = 42
1268+
breakpoint() # interactive pdb prompt opens here
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` (not under pytest, which captures stdin and breaks the REPL). At the `(Pdb)`
1294+
prompt try `p x`, `n`, `c`, `q`.
1295+
1296+
Two caveats when pausing at a breakpoint inside a workflow:
1297+
1298+
1. **Workflow task timeout.** Temporal expires a workflow task after ~10 seconds by default. If you sit at the
1299+
`(Pdb)` prompt longer than that, the server reassigns the task and your workflow replays from the start when
1300+
you continue — re-hitting the breakpoint. Pass `task_timeout=timedelta(minutes=N)` to `execute_workflow` /
1301+
`start_workflow` to give yourself debugging headroom:
1302+
1303+
```python
1304+
await client.execute_workflow(MyWorkflow.run, ..., task_timeout=timedelta(minutes=10))
1305+
```
1306+
1307+
2. **Deterministic replay.** Workflows are deterministic and replay from history; any wall-clock pause violates
1308+
that contract. For post-mortem debugging without these caveats, use the [Replayer](#replayer) on a recorded
1309+
history instead of live debugging.
1310+
1311+
A `breakpoint()` call from workflow code without `debug_mode` enabled raises a `RuntimeError` with a pointer to
1312+
this section, so the failure mode is loud rather than a silent hang.
1313+
12441314
##### Known Sandbox Issues
12451315

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

temporalio/worker/_workflow.py

Lines changed: 145 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,59 @@
4848
# Set to true to log all activations and completions
4949
LOG_PROTOS = False
5050

51+
# Prefix used to detect threads in the workflow task ThreadPoolExecutor.
52+
_WORKFLOW_THREAD_NAME_PREFIX = "temporal_workflow_"
53+
54+
_ORIGINAL_BREAKPOINTHOOK = sys.breakpointhook
55+
56+
57+
def _temporal_workflow_breakpoint_hook(*args: object, **kwargs: object) -> object:
58+
if threading.current_thread().name.startswith(_WORKFLOW_THREAD_NAME_PREFIX):
59+
raise RuntimeError(
60+
"breakpoint() / pdb.set_trace() inside workflow code requires "
61+
"debug_mode=True (or the TEMPORAL_DEBUG environment variable) on "
62+
"the Worker. Without it the workflow runs on a thread pool and "
63+
"pdb's interactive REPL cannot read stdin."
64+
)
65+
return _ORIGINAL_BREAKPOINTHOOK(*args, **kwargs)
66+
67+
68+
def _install_workflow_breakpoint_hook() -> None:
69+
if sys.breakpointhook is not _temporal_workflow_breakpoint_hook:
70+
sys.breakpointhook = _temporal_workflow_breakpoint_hook
71+
72+
73+
def _relax_sandbox_for_debugger(workflow_runner: WorkflowRunner) -> WorkflowRunner:
74+
"""Lift sandbox restrictions on `breakpoint` and `input` for debug_mode.
75+
76+
Both are flagged as non-deterministic by default. Users opting into
77+
debug_mode have accepted non-determinism for the session, so a targeted
78+
relaxation beats forcing them to swap to UnsandboxedWorkflowRunner.
79+
"""
80+
from temporalio.worker.workflow_sandbox._runner import SandboxedWorkflowRunner
81+
82+
if not isinstance(workflow_runner, SandboxedWorkflowRunner):
83+
return workflow_runner
84+
85+
restrictions = workflow_runner.restrictions
86+
invalid = restrictions.invalid_module_members
87+
builtins_matcher = invalid.children.get("__builtins__")
88+
if builtins_matcher is None or not (
89+
"breakpoint" in builtins_matcher.use or "input" in builtins_matcher.use
90+
):
91+
return workflow_runner
92+
93+
new_use = set(builtins_matcher.use) - {"breakpoint", "input"}
94+
new_builtins = dataclasses.replace(builtins_matcher, use=new_use)
95+
new_invalid = dataclasses.replace(
96+
invalid, children={**invalid.children, "__builtins__": new_builtins}
97+
)
98+
new_restrictions = dataclasses.replace(
99+
restrictions, invalid_module_members=new_invalid
100+
)
101+
return dataclasses.replace(workflow_runner, restrictions=new_restrictions)
102+
103+
51104
# Value was chosen abitrarily as a small number that allows some concurrency and prevents
52105
# large numbers of concurrent external storage operations causing resource contention.
53106
# This default limit is per workflow task activation and does not limit the total number
@@ -96,6 +149,13 @@ def __init__(
96149
)
97150
)
98151
self._workflow_task_executor_user_provided = workflow_task_executor is not None
152+
153+
# Debug mode (also enabled by TEMPORAL_DEBUG) disables deadlock
154+
# detection, runs activations inline on the main thread, and lifts
155+
# the sandbox restriction on breakpoint()/input() so pdb works.
156+
self._debug_mode = bool(debug_mode or os.environ.get("TEMPORAL_DEBUG"))
157+
if self._debug_mode:
158+
workflow_runner = _relax_sandbox_for_debugger(workflow_runner)
99159
self._workflow_runner = workflow_runner
100160
self._unsandboxed_workflow_runner = unsandboxed_workflow_runner
101161
self._data_converter = data_converter
@@ -127,11 +187,9 @@ def __init__(
127187
)
128188
self._throw_after_activation: Exception | None = None
129189

130-
# If there's a debug mode or a truthy TEMPORAL_DEBUG env var, disable
131-
# deadlock detection, otherwise set to 2 seconds
132-
self._deadlock_timeout_seconds = (
133-
None if debug_mode or os.environ.get("TEMPORAL_DEBUG") else 2
134-
)
190+
self._deadlock_timeout_seconds = None if self._debug_mode else 2
191+
192+
_install_workflow_breakpoint_hook()
135193

136194
# Keep track of workflows that could not be evicted
137195
self._could_not_evict_count = 0
@@ -241,6 +299,34 @@ async def drain_poll_queue(self) -> None:
241299
except PollShutdownError:
242300
return
243301

302+
async def _activate_inline_for_debug(
303+
self,
304+
loop: asyncio.AbstractEventLoop,
305+
workflow: _RunningWorkflow,
306+
act: temporalio.bridge.proto.workflow_activation.WorkflowActivation,
307+
) -> temporalio.bridge.proto.workflow_completion.WorkflowActivationCompletion:
308+
# Indirect through call_soon + a future so the activation runs outside
309+
# the dispatch task's __step() context. Python 3.14 refuses to enter a
310+
# task while another on the same thread is mid-step; suspending at the
311+
# await below clears that state so workflow.activate can step its own
312+
# task without collision.
313+
future: asyncio.Future = loop.create_future()
314+
315+
def run_inline() -> None:
316+
# _run_once clears the running-loop registration on exit; restore
317+
# the main loop so later code sees the right one.
318+
main_loop = asyncio._get_running_loop()
319+
try:
320+
completion = workflow.activate(act)
321+
future.set_result(completion)
322+
except BaseException as e:
323+
future.set_exception(e)
324+
finally:
325+
asyncio._set_running_loop(main_loop)
326+
327+
loop.call_soon(run_inline)
328+
return await future
329+
244330
async def _handle_activation(
245331
self, act: temporalio.bridge.proto.workflow_activation.WorkflowActivation
246332
) -> None:
@@ -330,35 +416,43 @@ async def _handle_activation(
330416
)
331417
self._running_workflows[act.run_id] = workflow
332418

333-
# Run activation in separate thread so we can check if it's
334-
# deadlocked
335-
activate_task = asyncio.get_running_loop().run_in_executor(
336-
self._workflow_task_executor,
337-
workflow.activate,
338-
act,
339-
)
340-
341-
# Run activation task with deadlock timeout
342-
try:
343-
completion = await asyncio.wait_for(
344-
activate_task, self._deadlock_timeout_seconds
419+
if self._debug_mode:
420+
# Inline on the main thread so pdb / breakpoint() can read
421+
# stdin. The loop blocks during the activation — that's the
422+
# intended single-stepping semantic.
423+
completion = await self._activate_inline_for_debug(
424+
asyncio.get_running_loop(), workflow, act
345425
)
346-
except asyncio.TimeoutError:
347-
# Need to create the deadlock exception up here so it
348-
# captures the trace now instead of later after we may have
349-
# interrupted it
350-
deadlock_exc = _DeadlockError.from_deadlocked_workflow(
351-
workflow.instance, self._deadlock_timeout_seconds
426+
else:
427+
# Run activation in separate thread so we can check if it's
428+
# deadlocked
429+
activate_task = asyncio.get_running_loop().run_in_executor(
430+
self._workflow_task_executor,
431+
workflow.activate,
432+
act,
352433
)
353-
# When we deadlock, we will raise an exception to fail
354-
# the task. But before we do that, we want to try to
355-
# interrupt the thread and put this activation task on
356-
# the workflow so that the successive eviction can wait
357-
# on it before trying to evict.
358-
workflow.attempt_deadlock_interruption()
359-
# Set the task and raise
360-
workflow.deadlocked_activation_task = activate_task
361-
raise deadlock_exc from None
434+
435+
# Run activation task with deadlock timeout
436+
try:
437+
completion = await asyncio.wait_for(
438+
activate_task, self._deadlock_timeout_seconds
439+
)
440+
except asyncio.TimeoutError:
441+
# Need to create the deadlock exception up here so it
442+
# captures the trace now instead of later after we may have
443+
# interrupted it
444+
deadlock_exc = _DeadlockError.from_deadlocked_workflow(
445+
workflow.instance, self._deadlock_timeout_seconds
446+
)
447+
# When we deadlock, we will raise an exception to fail
448+
# the task. But before we do that, we want to try to
449+
# interrupt the thread and put this activation task on
450+
# the workflow so that the successive eviction can wait
451+
# on it before trying to evict.
452+
workflow.attempt_deadlock_interruption()
453+
# Set the task and raise
454+
workflow.deadlocked_activation_task = activate_task
455+
raise deadlock_exc from None
362456

363457
except Exception as err:
364458
if isinstance(err, _DeadlockError):
@@ -576,22 +670,27 @@ async def _handle_cache_eviction(
576670
handle_eviction_task: asyncio.Future | None = None
577671
while True:
578672
try:
579-
# We only create the eviction task if we haven't already or
580-
# it is done. This is because if it already is running and
581-
# timed out, it's still running (and holding on to a
582-
# thread). But if did complete running but failed with
583-
# another error, we want to re-create the task.
584-
if not handle_eviction_task or handle_eviction_task.done():
585-
handle_eviction_task = (
586-
asyncio.get_running_loop().run_in_executor(
587-
self._workflow_task_executor,
588-
workflow.activate,
589-
act,
673+
if self._debug_mode:
674+
await self._activate_inline_for_debug(
675+
asyncio.get_running_loop(), workflow, act
676+
)
677+
else:
678+
# We only create the eviction task if we haven't already or
679+
# it is done. This is because if it already is running and
680+
# timed out, it's still running (and holding on to a
681+
# thread). But if did complete running but failed with
682+
# another error, we want to re-create the task.
683+
if not handle_eviction_task or handle_eviction_task.done():
684+
handle_eviction_task = (
685+
asyncio.get_running_loop().run_in_executor(
686+
self._workflow_task_executor,
687+
workflow.activate,
688+
act,
689+
)
590690
)
691+
await asyncio.wait_for(
692+
handle_eviction_task, self._deadlock_timeout_seconds
591693
)
592-
await asyncio.wait_for(
593-
handle_eviction_task, self._deadlock_timeout_seconds
594-
)
595694
# Break if it succeeds
596695
break
597696
except BaseException as err:

0 commit comments

Comments
 (0)