Implementation:Kubeflow Pipelines Dsl ExitHandler
| Sources | Domains | Last Updated |
|---|---|---|
| Kubeflow Pipelines, KFP Control Flow | ML_Pipelines, Reliability | 2026-02-13 |
Overview
Concrete tool for guaranteed cleanup task execution in pipelines provided by the KFP DSL context manager.
Description
dsl.ExitHandler is a context manager that ensures an exit task runs after all tasks within the block complete, regardless of success or failure. The exit task must be defined before the with block.
Usage
Use within a @dsl.pipeline function when you need guaranteed execution of a task after a block of potentially-failing tasks.
Code Reference
Source Location: Repository: kubeflow/pipelines, File: samples/core/exit_handler/exit_handler.py (L38-45)
Signature:
class ExitHandler:
def __init__(
self,
exit_task: PipelineTask,
name: Optional[str] = None,
): ...
def __enter__(self) -> 'ExitHandler': ...
def __exit__(self, *args): ...
Import: from kfp import dsl
I/O Contract
Inputs:
| Parameter | Type | Required | Description |
|---|---|---|---|
exit_task |
PipelineTask |
Yes | Task guaranteed to run after block |
name |
str |
No | Display name |
Outputs:
ExitHandlerContext — tasks in block execute normally; exit_task runs after block finishes.
Usage Examples
Example 1: Basic exit handler
Source: exit_handler.py
@dsl.pipeline(name='pipeline-with-exit-handler')
def pipeline_exit_handler(message: str = 'Hello World!'):
exit_task = print_op(message='Exit handler has worked!')
with dsl.ExitHandler(exit_task):
print_op(message=message)
fail_op(message='Task failed.')
Example 2: Combined with other control flow
Source: DSL - Control structures.py
@dsl.pipeline(name='tutorial-control-flows')
def control_flows_pipeline():
exit_task = print_op(message='Exit handler has worked!')
with dsl.ExitHandler(exit_task):
fail_op(message="Failing the run to demonstrate that exit handler still gets executed.")