Automation · Error Handling
Cleanup
Cheat sheet · https://pcornel.dk/topic.php?topic=5981
Answer
Cleanup removes temporary files, sessions, locks and partially created resources when a job ends or fails, using finally blocks, trap EXIT or always sections in playbooks.
Remember
- Remove temp files, sessions, locks.
- Run cleanup on failure too.
- Use finally/trap/always.
First checks
trap cleanup EXIT— Bashfinally {} / always:— PowerShell/Ansible- Temp files, locks, sessions released — hygiene
Verify in this order
exit (success or failure) → cleanup → release
Where it usually breaks
Input/desired state → request accepted → job/task terminal status → target object read-back → effective runtime state → original workload → regression/rollback readiness.
Error Handling → Cleanup · operational reference
| Layer | Error Handling → Cleanup operational reference |
|---|---|
| Runtime/ownership | automation cleanup/finalization semantics · the automation runtime finalization path that releases temporary resources regardless of success, handled failure or cancellation |
| Persistent intent | explicit cleanup contract listing owned temporary files/locks/sessions/leases/transactions and which cleanup steps are safe after partial initialization |
| Verification | Run success, controlled-failure and cancellation fixtures that each acquire a temporary resource; inspect both primary status and cleanup logs, then query that no owned resource/lock remains. |
| Expected interpretation | Every path releases only resources owned by the run, primary errors remain visible, cleanup is safe to retry, and independent post-run checks show no leaked lock/session/temp object. |
| Recovery boundary | preserve primary and cleanup errors separately, determine resource ownership from correlation IDs, repair only leaked owned resources, then rerun failure/cancellation fixtures to prove finalization |
Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5981