Handling errors automatically#
In a workflow, it is likely that errors will occur. In Taskblaster, errors can be handled automatically by defining an error handler. The error handler can adjust tasks in the workflow, e.g. by changing variables/objects or redefining functions. The error handler can handle the error after a task has failed, or it can handle the error during the workflow execution.
In this tutorial, we will produce a workflow with a failing task. Then we will use an error handler that will handle the failed task.
Handling tasks after they have failed#
First create a repository with a workflow that contains a simple task that will fail:
$ tb init
Created repository using module "taskblaster.repository" in "/home/me/repo".
import math
import taskblaster as tb
def L2_norm_fails(x, y):
return math.sqrt(x**2 + y**2)
def L2_norm_normalized(x, y):
assert x != 0
normalized_sum = 1 + (y / x) ** 2
return abs(x) * math.sqrt(normalized_sum)
@tb.workflow
class Workflow:
a = tb.var()
b = tb.var()
@tb.task
def L2_norm(self):
return tb.node(L2_norm_fails, x=self.a, y=self.b)
def workflow(runner):
runner.run_workflow(Workflow(a=1e200, b=1e200))
The function L2_norm_fails will fail if it tries to square a large number.
The function L2_norm_normalized will be used for error handling because it will
not fail for large numbers because it normalizes the input values.
Next we run the workflow
$ tb workflow src/main.py entry: add new 0/0 tree/L2_norm add: 1
And run the task
$ tb run tree/L2_norm
Starting worker rank=000 size=001
[rank=000 2026-11-02 14:08:17 N/A-0/1] Worker class: —
[rank=000 2026-11-02 14:08:17 N/A-0/1] Required tags: —
[rank=000 2026-11-02 14:08:17 N/A-0/1] Supported tags: —
[rank=000 2026-11-02 14:08:17 N/A-0/1] name: None
tags: —
required_tags: —
resources: None
max_tasks: None
subworker_size: None
subworker_count: None
wall_time: None
no_mpi: None
workflow: None
[rank=000 2026-11-02 14:08:17 N/A-0/1] Main loop
[rank=000 2026-11-02 14:08:17 N/A-0/1] Running L2_norm ...
[rank=000 2026-11-02 14:08:17 N/A-0/1] Traceback (most recent call last):
File "/home/docs/checkouts/readthedocs.org/user_builds/taskblaster/envs/latest/lib/python3.12/site-packages/taskblaster/worker.py", line 579, in process_one_task
loaded_task.run(self)
File "/home/docs/checkouts/readthedocs.org/user_builds/taskblaster/envs/latest/lib/python3.12/site-packages/taskblaster/worker.py", line 218, in run
output = function(**kwargs)
^^^^^^^^^^^^^^^^^^
File "/home/me/repo/src/main.py", line 7, in L2_norm_fails
return math.sqrt(x**2 + y**2)
~^^~
OverflowError: (34, 'Numerical result out of range')
[rank=000 2026-11-02 14:08:17 N/A-0/1] Task L2_norm failed: (34, 'Numerical result out of range')
[rank=000 2026-11-02 14:08:17 N/A-0/1] WARDEN: Task L2_norm has handlers [].
[rank=000 2026-11-02 14:08:17 N/A-0/1] WARDEN: No handlers found.
[rank=000 2026-11-02 14:08:17 N/A-0/1] No available tasks, end worker main loop
Using tb ls we can see that the task L2_norm has failed.
$ tb ls state info tags worker time priority folder ──────── ────────── ─────────── ─────────── ─────────── ──────── ────────────────────────────────────────────────── fail 0/0 N/A 00:00:00 0.00 tree/L2_norm ^^^^ OverflowError: (34, 'Numerical result out of range')
The error handler will now be used to handle the error. We will handle the error
by making the L2_norm task use L2_norm_normalized instead of L2_norm_fails
both previously defined in main.py. The target function is accessible as loaded_task.target
and its keyword argument dictionary as loaded_task.kwargs.
Next we define an error handler L2NormHandler. For reasons related to the
documentation build system we here include this as a new file, main_with_handler.py,
but ordinarily one would just edit the existing file.
from main import L2_norm_fails
import taskblaster as tb
from taskblaster.handler import (
ErrorHandler,
SkipHandlingOutcome,
UpdateTaskOutcome,
)
class L2NormHandler(ErrorHandler):
def __init__(self, max_attempts=1):
super().__init__(max_attempts=max_attempts)
def handle(self, handler_data, loaded_task, exception_data):
if exception_data.name == 'OverflowError':
loaded_task.target = 'main.L2_norm_normalized'
return UpdateTaskOutcome(loaded_task, exception_data, handler_data)
return SkipHandlingOutcome(exception_data)
@tb.workflow
class Workflow:
a = tb.var()
b = tb.var()
@tb.task(error_handlers=L2NormHandler())
def L2_norm(self):
return tb.node(L2_norm_fails, x=self.a, y=self.b)
def workflow(runner):
runner.run_workflow(Workflow(a=1e200, b=1e200))
Run the workflow
$ tb workflow src/main_with_handler.py entry: have handlers fail 0/0 tree/L2_norm ^^^^ OverflowError: (34, 'Numerical result out of range') have: 1
Use tb warden kick to re-run the workflow with the error handler.
$ tb warden kick
Starting worker rank=000 size=001
[rank=000 2026-11-02 14:08:17 worker] Worker class: —
[rank=000 2026-11-02 14:08:17 worker] Required tags: —
[rank=000 2026-11-02 14:08:17 worker] Supported tags: —
[rank=000 2026-11-02 14:08:17 worker] name: None
tags: —
required_tags: —
resources: None
max_tasks: None
subworker_size: None
subworker_count: None
wall_time: None
no_mpi: None
workflow: None
[rank=000 2026-11-02 14:08:17 worker] WARDEN: looping over tasks in tree L2_norm IndexNode(name='L2_norm', state=<State.fail: 'F'>, awaitcount=0)
[rank=000 2026-11-02 14:08:17 worker] WARDEN: loading exception from: L2_norm
[rank=000 2026-11-02 14:08:17 worker] WARDEN: loading ExceptionData.
[rank=000 2026-11-02 14:08:17 worker] WARDEN: Task L2_norm has handlers [L2NormHandler(max_attempts=1)].
[rank=000 2026-11-02 14:08:17 worker] WARDEN: Checking ExceptionData(info=OverflowError: (34, 'Numerical result out of range'), module=builtins, name=OverflowError) for each handler.
[rank=000 2026-11-02 14:08:17 worker] WARDEN: Initializing handler_data.json.
[rank=000 2026-11-02 14:08:17 worker] WARDEN: UpdateTaskOutcome: Handler returned UpdateTaskOutcome. The handler
successfully handled the error. Warden will now update the task.
[rank=000 2026-11-02 14:08:17 worker] WARDEN: Updating state to partial.
We can use tb ls to see that the task L2_norm is now in the partial state.
So we run the task again.
$ tb run tree/L2_norm
Starting worker rank=000 size=001
[rank=000 2026-11-02 14:08:17 N/A-0/1] Worker class: —
[rank=000 2026-11-02 14:08:17 N/A-0/1] Required tags: —
[rank=000 2026-11-02 14:08:17 N/A-0/1] Supported tags: —
[rank=000 2026-11-02 14:08:17 N/A-0/1] name: None
tags: —
required_tags: —
resources: None
max_tasks: None
subworker_size: None
subworker_count: None
wall_time: None
no_mpi: None
workflow: None
[rank=000 2026-11-02 14:08:17 N/A-0/1] Main loop
[rank=000 2026-11-02 14:08:17 N/A-0/1] Running L2_norm ...
[rank=000 2026-11-02 14:08:17 N/A-0/1] Task L2_norm finished in 0:00:00.000725
[rank=000 2026-11-02 14:08:17 N/A-0/1] No available tasks, end worker main loop
After running the task, we can use tb ls to see that the task L2_norm has completed successfully.
$ tb ls state info tags worker time priority folder ──────── ────────── ─────────── ─────────── ─────────── ──────── ────────────────────────────────────────────────── done 0/0 N/A 00:00:00 0.00 tree/L2_norm
Workflow with pre-defined error handler#
The whole workflow can also be run with the error handler defined from the start.
This is done by defining the error handler in the workflow before
running the associated tasks. If the error handler is defined before running the workflow,
then tb warden kick is not needed.
First unrun the failed task:
$ tb unrun tree/L2_norm --force unrun: done L2_norm 1 task were unrun.
And then we rerun the workflow with the error handler by using main_with_handler.py.
Now L2_norm` already has the error handler before it is run and will not fail.
$ tb workflow src/main_with_handler.py entry: have new 0/0 tree/L2_norm have: 1
And run the task
$ tb run tree/L2_norm
Starting worker rank=000 size=001
[rank=000 2026-11-02 14:08:18 N/A-0/1] Worker class: —
[rank=000 2026-11-02 14:08:18 N/A-0/1] Required tags: —
[rank=000 2026-11-02 14:08:18 N/A-0/1] Supported tags: —
[rank=000 2026-11-02 14:08:18 N/A-0/1] name: None
tags: —
required_tags: —
resources: None
max_tasks: None
subworker_size: None
subworker_count: None
wall_time: None
no_mpi: None
workflow: None
[rank=000 2026-11-02 14:08:18 N/A-0/1] Main loop
[rank=000 2026-11-02 14:08:18 N/A-0/1] Running L2_norm ...
[rank=000 2026-11-02 14:08:18 N/A-0/1] Traceback (most recent call last):
File "/home/docs/checkouts/readthedocs.org/user_builds/taskblaster/envs/latest/lib/python3.12/site-packages/taskblaster/worker.py", line 579, in process_one_task
loaded_task.run(self)
File "/home/docs/checkouts/readthedocs.org/user_builds/taskblaster/envs/latest/lib/python3.12/site-packages/taskblaster/worker.py", line 218, in run
output = function(**kwargs)
^^^^^^^^^^^^^^^^^^
File "/home/me/repo/src/main.py", line 7, in L2_norm_fails
return math.sqrt(x**2 + y**2)
~^^~
OverflowError: (34, 'Numerical result out of range')
[rank=000 2026-11-02 14:08:18 N/A-0/1] Task L2_norm failed: (34, 'Numerical result out of range')
[rank=000 2026-11-02 14:08:18 N/A-0/1] WARDEN: Task L2_norm has handlers [L2NormHandler(max_attempts=1)].
[rank=000 2026-11-02 14:08:18 N/A-0/1] WARDEN: Checking ExceptionData(info=OverflowError: (34, 'Numerical result out of range'), module=builtins, name=OverflowError) for each handler.
[rank=000 2026-11-02 14:08:18 N/A-0/1] WARDEN: Initializing handler_data.json.
[rank=000 2026-11-02 14:08:18 N/A-0/1] WARDEN: UpdateTaskOutcome: Handler returned UpdateTaskOutcome. The handler
successfully handled the error. Warden will now update the task.
[rank=000 2026-11-02 14:08:18 N/A-0/1] WARDEN: Updating state to partial.
[rank=000 2026-11-02 14:08:18 N/A-0/1] Running L2_norm ...
[rank=000 2026-11-02 14:08:18 N/A-0/1] Task L2_norm finished in 0:00:00.000828
[rank=000 2026-11-02 14:08:18 N/A-0/1] No available tasks, end worker main loop
Use tb ls to see that L2_norm has completed successfully.
$ tb ls state info tags worker time priority folder ──────── ────────── ─────────── ─────────── ─────────── ──────── ────────────────────────────────────────────────── done 0/0 N/A 00:00:00 0.00 tree/L2_norm