- Home
- Skills
- Testing & Debugging
- Repair Temporary File & Cleanup Failures
More screenshots
Works with the AI tools you already use
Repair Temporary File & Cleanup Failures
Repair Temporary File & Cleanup Failures is a bounded ToolForge Labs workflow.
$8.99
Repair Temporary File & Cleanup Failures
Example session with this skill installed
Our Python background worker is leaving thousands of .tmp files in /tmp after it crashes, eventually hitting disk quotas. It uses a simple open() and doesn't clean up on SIGTERM. Help.
- Read your context and instructions
- Compiled the repair temporary file
I have analyzed the lifecycle failure in your background worker. The primary issue is the lack of a formal cleanup contract and the absence of atomic publication, which leads to orphaned files when the process receives a SIGTERM or crashes during a write operation.
Temporary File Lifecycle Repair
The following implementation uses a context manager to ensure cleanup and an atomic write-then-rename pattern to prevent partial data corruption.
import os
import tempfile
import logging
from contextlib import contextmanager
from pathlib import Path
logger = logging.getLogger(__name__)
@contextmanager
def atomic_write_path(target_path, dir=None):
"""
Ensures a file is written completely or not at all.
Handles cleanup of the temporary file even on exceptions or crashes.
"""
target_path = Path(target_path)
# Create temp file in the same filesystem as target for atomic rename
fd, temp_path = tempfile.mkstemp(
dir=dir or target_path.parent,
prefix=f".{target_path.name}-",
suffix=".tmp"
)
try:
os.close(fd) # Close handle so we can use standard file IO
yield Path(temp_path)
# Ensure data is flushed to disk before renaming
with open(temp_path, 'rb+') as f:
f.flush()
os.fsync(f.fileno())
os.replace(temp_path, target_path)
except Exception as e:
logger.error(f"Failed to write {target_path}: {e}")
raise
finally:
# Final safety cleanup if rename didn't happen
if os.path.exists(temp_path):
try:
os.unlink(temp_path)
except OSError:
pass
# Usage in your worker
def process_data(data, output_file):
with atomic_write_path(output_file) as tmp:
with open(tmp, 'w') as f:
f.write(data)
| Invariant | Implementation Detail | Benefit |
|---|---|---|
| Cleanup | finally block with os.unlink | Prevents disk quota exhaustion on SIGTERM/Exceptions. |
| Atomicity | os.replace | Ensures the target file is never in a partially-written state. |
| Durability | os.fsync | Protects against data loss during system power failure. |
| Security | tempfile.mkstemp | Uses safe permissions (0600) and prevents naming collisions. |
Next steps
- Integrate the
atomic_write_pathutility into your main processing loop. - Register a
signalhandler for SIGTERM to allow the context manager'sfinallyblock to execute. - Verify that your
/tmpdirectory and the target directory reside on the same mount point to ensureos.replaceis atomic.
Connects securely to your tools. The creator never sees your data.
What you get
About this skill
Repair Temporary File & Cleanup Failures is a bounded ToolForge Labs workflow. Repair temporary-file lifecycle failures across secure creation, naming, permissions, atomic replacement, cancellation, crashes, concurrent workers, cleanup, disk pressure, and cross-platform behavior. Works from repositories or partial helper/log evidence. Produces bounded repairs and deterministic tests while preserving sandbox and data-handling rules. It works from a full repository or partial evidence such as snippets, logs, configuration, traces, tickets, and architecture notes. The skill preserves existing product and security policy, separates verified facts from assumptions, avoids invented execution claims, and produces concrete repair or review steps with focused deterministic validation or traceable evidence.
How to install
Works the same in every agent - Claude, Cursor, Codex, Copilot and 20+ more.
- 1
Download the ZIP
Free skills download straight away. Paid skills unlock right after purchase.
- 2
Unzip into your skills folder
Every agent reads skills from one folder on your machine. Drop the unzipped folder in there.
- 3
Ask your agent to use it
Restart the agent if it was already running. It picks the skill up automatically - no config needed.
Skills folder by agent
Click the path to copy it. Create the folder if it does not exist yet.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill passes our trust checks below.
Security scanned
Passed our 8-point scan before listing
Fresh listing
Recently published to Agensi
30-day refund
Not a fit? Get your money back
Trust & safety
Security scanned
Verified clean 21 days ago
- Passed all security checks, Safe to install