# cleanup Cleanup handles, resources. Generated by `make records` from the decisions whose `topic` (or `also`) is this one; edit the decisions, not this page. ## Current ### Lifecycle - **Cleanup runs in a fresh process after the task** (D16): Cleanup runs after the task ends, including when it is killed. It uses the task’s preloaded or bare fork server, with spawn as fallback; downstream tasks wait. - **An attempt whose process never started has no cleanup** (D216): A rejected attempt, missing secret, or timeout during reads can leave the process unstarted. Its cleanup result is `not needed`. - **Cleanups default to three retries 30 seconds apart** (D217): `cleanup_retries` and `cleanup_retry_delay` are available on tasks and in `StepDefaults`. Their defaults are three retries and 30 seconds between retries. - **Cancellation does not stop a running per-task cleanup** (D219): Cancellation leaves a running per-task cleanup running until it finishes or reaches `cleanup_timeout`. During drain it may continue until the pod is killed; outside Kubernetes the agent waits for it. - **Per-task cleanup has no flag to skip waiting** (D230): Downstream tasks wait for per-task cleanup. There is no flag to skip the wait; independent slow cleanup can be a resource's. - **A cleanup keeps its own task kind** (D274): A cleanup retains its own kind, such as `python` or `empty`. Its role comes from `Dag.resources` and is shown as cleanup of its resource. - **An empty cleanup's message is the task's reason** (D276): An empty cleanup has no attempt; its message goes in the task's reason. The reason is shown where a log would be. ### Building it - **Cleanup and resources are built in separate sub-steps** (D214): M4.8a covers same-worker cleanup; M4.8b/b2/b3 cover recovery, moved cleanups and network cuts. M4.8c1/c2 cover Python- resource parsing and execution; M4.8d command resources; M4.8e UI and test doubles. Script resources and cleanups belong to M4.9c. - **Split M4.8b into journal and moved-cleanup commits** (D251): M4.8b covers the journal, restarts, kept result, `worker_lost`, and drain. M4.8b2 sends handles to the master and implements `cleanup_anywhere`. - **Split resource implementation into API and master commits** (D265): M4.8c1 covers the Python API, model and parse rules; M4.8c2 covers master state, cleanup and run result. A resource with no users gets a parse warning and cleans up immediately after setup. ### Connections and secrets - **A cleanup receives its task's declared connections** (D218): A cleanup gets the connections and secrets its task declared. A command's connection variables and files stay until the cleanup ends. ### Moving and disconnection - **A lost worker fails the run when its declared cleanup cannot move** (D220): For a task declaring per-task cleanup without `cleanup_anywhere`, worker loss records a failed cleanup and fails the run even when no handle was registered or the task's retry succeeds. The failure message points to `cleanup_anywhere`. - **Cleanup anywhere waits when no worker can take it** (D221): A `cleanup_anywhere` cleanup waits when no worker can take it and is shown as `unschedulable`. The task's retry waits too. *Not found in the code yet:* the task's retry waits too. - **A cleanup moved to another worker keeps the same attempt** (D243): The attempt row moves to the new worker for the cleanup. The old worker’s name remains in the log and error summary. *Not found in the code yet:* the old worker's name stays in the log. - **A cleanup without a worker remains `cleaning_up`** (D244): The instance remains `cleaning_up` with reason “no worker for the cleanup”. It is shown as `unschedulable`. - **A moved cleanup runs the new worker’s current code** (D245): A cleanup on another worker uses that worker’s current code. A cleanup function missing from that code fails the cleanup. *Not found in the code yet:* a cleanup function missing from that code fails the cleanup. - **A moved cleanup goes ahead of waiting tasks** (D246): A moved cleanup uses its task’s queue ahead of waiting tasks. It is not held by limits. - **Each cleanup move counts as a try** (D247): A cleanup may move up to `retries + 1` times; another worker loss after that allowance ends it `worker_lost`. An assignment its worker's messages show it never started, or one never sent, returns without consuming a move; one that may have started counts as started (decision 460). - **A disconnected worker stops tasks with `cleanup_anywhere`** (D252): After a worker lease without word from the master, the worker stops its tasks with `cleanup_anywhere`. Other tasks are not stopped by this rule. - **A disconnected task’s cleanup waits for the master** (D253): The cleanup of a task stopped after disconnection waits for the master’s answer. `Continue` runs it and `Kill` drops it. - **A task ending while disconnected holds its movable cleanup** (D254): A task with `cleanup_anywhere` that ends by itself while disconnected holds its cleanup. It waits for the master’s answer. - **A cleanup already running continues after disconnection** (D255): A cleanup under way when the master becomes unavailable keeps running. It may overlap a cleanup moved to another worker; the docs say so. - **A moved cleanup waits after the worker lease** (D256): The worker stops the task at the lease; the master waits the cancel grace plus the hidden cleanup move wait. The default total is 40 seconds, and the deadline is stored with the attempt. - **A task stopped on disconnection is `lost`** (D257): A task stopped after losing contact with the master ends `lost`. It uses a retry. - **A cleanup waits for an unschedulable user** (D273): A cleanup waits while a user is `unschedulable`, until the user finishes or is cancelled. A resource's `unschedulable_timeout` (a cleanup option) may fail the user and release cleanup; the default is unlimited, and a user of several resources gets the shortest. - **A moving cleanup's result from the worker it moved from counts** (D282): While a cleanup waits to move, its previous worker’s cleanup result is stored and the pending move dropped. Its journaled `Finished` receives `Continue` so the result is sent again. On the first move that worker’s task result counts; on later moves the previously kept task result remains. - **Worker reconnect waits are capped at half the master's lease** (D307): A worker caps its reconnect backoff at half the lease the master gave it. A restarted master gives each worker a fresh lease from its start, plus the time a deposed master may still lead (decision 444). - **The returning worker cleans handles the master never received** (D308): Register reports journal handle counts. If cleanup_anywhere moved from the original worker and its journal exceeds the master's contiguous prefix, an unassigned move is adopted back, dropping its kept lost result; if assigned or ended, the original worker stops any task and cleans from cleanup_from. The marked rest merges once, never as the whole cleanup: failed or worker_lost wins, otherwise ok wins, and tries and time add. Failure fails an open run or marks an already successful run yellow. - **Running cleanup continues past the drain deadline** (D346): The agent does not stop a running cleanup at the drain deadline. A cleanup timeout that, with the drain's terminate and report periods, doesn't fit the grace is warned once at worker start (the default) or per task (its own). ### Journal and handles - **Handles are masked before the journal and master** (D222): Handles are masked like outputs before reaching the worker journal or master, with a log warning. A cleanup needing a secret declares it; resource values are masked as outputs. *Not found in the code yet:* the log warning when handles are masked. - **Handles are limited to 1 MiB per attempt** (D228): An attempt's handles may total at most 1 MiB. A handle over the limit is refused with an error naming the limit; the master only warns when handles don't fit its message. - **Task and cleanup results are reported separately** (D229): The task result is reported when its process ends and retained while the instance is `cleaning_up`; `Finished` settles the cleanup and attempt. Worker loss preserves that result without rerunning completed work. The cleanup moves when `cleanup_anywhere` permits it; otherwise it is recorded as `worker_lost`. - **`cleanup add` returns after journaling and sending the handle** (D242): `cleanup add` returns once the handle is journaled and sent. Handles are sent again on reconnect; a pod lost in the gap loses the handle for `cleanup_anywhere`. *Not found in the code yet:* a pod lost in the gap loses the handle for `cleanup_anywhere`. - **A journaled cleanup waits for the master’s `Continue`** (D248): A cleanup recovered from the journal starts on the master’s `Continue` response to `Register`. A master outage delays it. - **A cleanup can outlast the pod grace period** (D249): A cleanup continues until the pod is killed if it outlasts the grace period. Outside Kubernetes, the agent waits for it. - **Warn about cleanup timeout at assignment once per task** (D250): The agent logs when `cleanup_timeout` does not fit the grace period. The default `[worker] cleanup_timeout` is warned about once at the worker's start; a task's own `cleanup_timeout` at assignment, once per task. ### Resources - **Calling a setup twice in one DAG is a parse error** (D223): A setup can be called only once in a DAG. Calling it twice is a parse error. - **Expanding a setup is a parse error** (D224): `.expand` on a setup is a parse error. A setup cannot be expanded into resource instances. - **Resource cleanup takes priority over waiting tasks** (D225): A resource's cleanup goes ahead of waiting tasks and is not held by DAG limits. It still needs a worker slot. - **Resource cleanup inherits setup secrets and connections** (D226): Resource cleanup inherits its setup's secrets and connections and may add its own. Its queue defaults to the setup's; a static resource has only its own declarations. - **A resource has setup and cleanup parts** (D259): A resource is a distinct graph item with setup and cleanup parts. Both parts always exist. - **A resource without a cleanup function gets an empty cleanup** (D260): Without a declared cleanup, a resource gets an empty cleanup in the graph and run. The master completes it without an attempt, process or worker, recording the explanation in the task’s reason where a log would appear. A repeated declaration from the same source replaces the first; one from another source is a parse error. - **A resource cleanup is a visible task instance** (D261): A resource’s cleanup is the visible task `.cleanup`, using ordinary attempts, retries, logs, dispatch and lost-worker handling. It retains its execution kind; `Dag.resources` identifies its cleanup role. Resource state is derived from setup and cleanup tasks, without a table of resource instances. - **A resource cleanup exhausting retries fails the run** (D263): A cleanup that fails after its last retry remains `cleanup_failed` and fails the run. The failure is shown clearly; a retry command comes later. - **Resource cleanups run while a DAG is paused** (D264): A paused DAG continues to run resource cleanups. Pausing stops new work while existing resources are freed. - **A group’s successors wait for its blocking resource cleanups** (D266): By default, group successors wait for cleanups of resources set up in that group, unless a successor or its descendants use the resource. Blocking cleanup failure makes successors `upstream_failed`. With `blocking=False`, successors proceed but cleanup failure still fails the run. Resources outside groups create no such barrier. - **A resource has one cleanup across imports** (D267): A resource is shared by DAGs that import it, with its cleanup attached to the resource. A different cleanup requires a different resource. - **A resource’s cleanup is declared with its method** (D268): Use `@cluster.cleanup`, bare or with options such as `blocking=False`. The method works on a setup, its override and `Resource(name)`. - **Connections use generators; resources use separate functions** (D269): A connection opens and closes in a generator within one process. A resource has separate setup and cleanup functions in separate tasks. - **Resource cleanups keep the run’s active slot** (D270): A run keeps its `max_active_runs` slot until ordinary task and resource cleanups end, including resource cleanups with `blocking=False`. A late rest cleanup under D308 can run after the run has ended. - **Command and script cleanups are resource methods** (D271): Declare command and script cleanups with `cluster.cleanup_command([...])` and `cluster.cleanup_script(job=...)`. Both methods accept `blocking=`. - **A resource's state is derived from its setup and cleanup** (D275): Resource state comes from the states of its setup and cleanup tasks. It includes waiting for users, cleaning up, cleaned, cleanup failed, not needed and cleanup cancelled. - **Cancelling a run keeps its resource cleanups** (D277): A cancelled run still runs the cleanup of each resource set up, including retries. It ends `cancelled`, or `failed` if a cleanup fails; a cleanup can itself be cancelled under D279. Cleanups ignore task concurrency limits. - **Resource cleanups have a default timeout of 30 minutes per try** (D278): A resource cleanup without `timeout` uses `[worker] resource_cleanup_timeout`, default 30 minutes. The master marks the assignment and the agent fills the worker's timeout; a timed-out try can be retried. - **A resource cleanup can be cancelled directly** (D279): `schedr tasks cancel` and the panel cancel a waiting resource cleanup immediately or stop a running one; cancelling its run keeps it. The resource becomes `cleanup_cancelled`, or `not_needed` if its setup failed, skipped or was cancelled; the run ends cancelled unless something failed. Cancelling a blocking cleanup cancels what follows its group; cancelling a non-blocking cleanup affects nothing else. - **A late cleanup failure turns a successful run yellow** (D309): A run that already ended SUCCESS keeps that state when a rest cleanup fails later. It is shown yellow with `late_cleanup_failed` (the UI says "late cleanup failed"); the failure is logged and counted. ### UI - **Resource tasks appear in a box beside the run graph** (D290): The run graph places resource setups and cleanups in lanes in a Resources box to the left of tasks. They remain at their corresponding row heights, with lines in a separate channel crossing only between rows. - **Resource graph lines summarize users and expand on hover** (D291): Shared-group users get one setup-to-group line and one group-to-cleanup line; otherwise lines connect first and last users. Other lines and the rail appear on hover or selection, each user has a colored dot, and lines from cleanup onward remain visible. ### From other topics - Build M4 feature views as UI shells in M3 (D25, `ui-cli`; built M3.2b) - Async task calls return coroutines outside a DAG (D49, `async-tasks`; built M4.10) - Per-task cleanup defaults to the worker cleanup timeout (D235, `configuration`; built M4.8f) - Show a resource as two caps joined by a rail (D262, `ui-cli`; built M4.8e) - The graph shows which tasks a cleanup holds back (D272, `ui-cli`; built M4.8e) - A disconnected or frozen worker can cause a task to run twice (D280, `workers`; built M4.8z) - Script resources reject contradictory declarations (D312, `scripts`; built M4.9c) - Script cleanup settings can override declaration defaults (D313, `scripts`; built M4.9c) - Workers reject tasks whose cleanup script they cannot run (D314, `scripts`; built M4.9c) - Wait steps do not accept task cleanup options (D323, `wait-steps`; built M4.11a1) - Sensors and resources have their own endpoints (D340, `ui-cli`; built M4.11a3) ## Replaced - D227 (dropped): The proposed test cleanup decision was dropped - D215 (dropped): The proposed sub-step commit decision was dropped ## Where in code - `crates/schedr-agent/src/agent.rs` (D243, D248, D254, D255) - `crates/schedr-agent/src/agent.rs::NO_WORD` (D252) - `crates/schedr-agent/src/agent.rs::NotNeeded` (D216) - `crates/schedr-agent/src/agent.rs::cut_off` (D252) - `crates/schedr-agent/src/agent.rs::drain_running` (D346) - `crates/schedr-agent/src/agent.rs::kept_cleanup` (D229) - `crates/schedr-agent/src/agent.rs::moved_cleanup` (D245) - `crates/schedr-agent/src/agent.rs::reconnect_cap` (D307) - `crates/schedr-agent/src/agent.rs::send_handles` (D242) - `crates/schedr-agent/src/agent.rs::warned_room` (D250) - `crates/schedr-agent/src/attempt.rs::CleanupGate` (D253) - `crates/schedr-agent/src/attempt.rs::can_run_cleanup_script` (D314) - `crates/schedr-agent/src/handles.rs::MAX_HANDLES_SIZE` (D228) - `crates/schedr-agent/src/handles.rs::add` (D222) - `crates/schedr-agent/src/status.rs` (D252) - `crates/schedr-api/src/routes.rs::list_run_resources` (D340) - `crates/schedr-api/src/routes.rs::list_switched_off_sensors` (D340) - `crates/schedr-api/src/views.rs` (D309) - `crates/schedr-api/src/views.rs::ResourceView` (D275) - `crates/schedr-config/src/config.rs::cleanup_move_wait` (D256) - `crates/schedr-config/src/config.rs::resource_cleanup_timeout` (D278) - `crates/schedr-db/migrations/postgres/0001_initial.sql` (D247) - `crates/schedr-db/migrations/sqlite/0001_initial.sql::cleanup_moves` (D247) - `crates/schedr-db/src/attempts.rs` (D229, D273, D277, D282) - `crates/schedr-db/src/attempts.rs::cleanup_moves` (D247) - `crates/schedr-db/src/attempts.rs::move_cleanup` (D243) - `crates/schedr-db/src/attempts.rs::ready_instances` (D225) - `crates/schedr-db/src/instances.rs` (D277) - `crates/schedr-db/src/reads.rs::late_failure` (D309) - `crates/schedr-db/src/runs.rs::mark_late_failure` (D309) - `crates/schedr-master/src/core.rs` (D229, D282) - `crates/schedr-master/src/core.rs::take_back` (D308) - `crates/schedr-master/src/dispatch.rs` (D228, D242, D243, D255, D282) - `crates/schedr-master/src/dispatch.rs::NO_CLEANUP_WORKER` (D221, D244) - `crates/schedr-master/src/dispatch.rs::assign_cleanups` (D246) - `crates/schedr-master/src/dispatch.rs::cleanup_move_wait` (D256) - `crates/schedr-master/src/dispatch.rs::end_lost` (D220) - `crates/schedr-master/src/dispatch.rs::resource_of_cleanup` (D225) - `crates/schedr-master/src/dispatch.rs::unschedulable_timeout_ms` (D273) - `crates/schedr-master/src/dispatch/tests.rs` (D220, D221) - `crates/schedr-master/src/execution.rs` (D273) - `crates/schedr-master/src/progress.rs` (D277) - `crates/schedr-master/src/progress.rs::EMPTY_CLEANUP` (D276) - `crates/schedr-model/src/dag.rs::Resource` (D259) - `crates/schedr-model/src/dag.rs::ResourceState` (D263, D275) - `crates/schedr-model/src/dag.rs::blocking` (D266, D272) - `crates/schedr-model/src/dag.rs::resource_of_cleanup` (D261) - `crates/schedr-model/src/dag.rs::unschedulable_timeout_ms` (D273) - `crates/schedr-model/src/states.rs` (D216) - `crates/schedr-model/src/task.rs::DEFAULT_CLEANUP_RETRIES` (D217) - `crates/schedr-model/src/task.rs::DEFAULT_CLEANUP_RETRY_DELAY_MS` (D217) - `crates/schedr-proto/proto/schedr/protocol/v1/master.proto` (D242) - `crates/schedr-ui/src/runs.rs::Badge` (D291) - `crates/schedr-ui/src/runs.rs::Graph` (D290) - `crates/schedr-ui/src/runs.rs::Rail` (D291) - `crates/schedr-ui/src/runs.rs::ResourceRow` (D279) - `crates/schedr-ui/src/runs.rs::cap_of` (D262) - `crates/schedr-ui/src/runs.rs::rails_of` (D262) - `crates/schedr-ui/src/runs.rs::resource_edges` (D272) - `crates/schedr-ui/templates/run.html` (D291) - `crates/schedr/src/cli.rs` (D340) - `python/schedr/_build.py::refuse_second_setup` (D223) - `python/schedr/_build.py::wait_for_cleanups` (D266) - `python/schedr/_model/dag.py` (D259, D266) - `python/schedr/operators.py` (D259) - `python/schedr/operators.py::_declared_cleanup` (D313) - `python/schedr/operators.py::_refuse_for_resource` (D312) - `python/schedr/resources.py` (D226, D260) - `python/schedr/resources.py::DEFAULT_CLEANUP_RETRIES` (D313) - `python/schedr/resources.py::Resource` (D267) - `python/schedr/resources.py::ResourceFunction` (D269) - `python/schedr/resources.py::_declare` (D224) - `python/schedr/resources.py::cleanup` (D268) - `python/schedr/resources.py::cleanup_command` (D271) - `python/schedr/resources.py::cleanup_script` (D271) - `python/schedr/runner/coroutines.py::run_coroutine` (D49) - `python/schedr/sensors.py::_no_cleanup` (D323) - `python/schedr/tasks.py::is_async` (D49) ## Open None. Details and edge cases, with the reasons; read them before changing this: - archive/PROJECT.md: Cleanup and resources, Sensors and triggers