Skip to content

Resource leak: Temp directories not cleaned up when git clone fails #418

@Serhan-Asad

Description

@Serhan-Asad

Description

When git clone fails in _setup_repository(), the temporary directory created with tempfile.mkdtemp() is not cleaned up, leading to a disk space leak.

Location

File: pdd/agentic_change.py:111-132

Root Cause

# Line 112: Creates temp directory
temp_dir = Path(tempfile.mkdtemp(prefix=f"pdd_{repo}_"))

# Lines 120-131: If clone fails, raises exception without cleanup
try:
    result = subprocess.run(["gh"] + clone_cmd, ...)
    if result.returncode != 0:
        raise RuntimeError(...)  # Exits without cleaning temp_dir
except Exception as e:
    raise RuntimeError(...)  # Exits without cleaning temp_dir

Reproduction

We tested this manually and confirmed the leak:

# Test showed temp directory created but not deleted on clone failure
temp_dir = Path(tempfile.mkdtemp(prefix=f"pdd_{repo}_"))
# ... clone fails ...
# Directory still exists: True ⚠️

Scenarios That Trigger The Leak

  1. Network interruption during clone
  2. Git authentication failures
  3. Rate limiting
  4. Private repositories without proper credentials
  5. Non-existent repositories (after issue API call succeeds)
  6. Disk space issues during clone

Proposed Solutions

Option 1: Use TemporaryDirectory context manager (Recommended)

with tempfile.TemporaryDirectory(prefix=f"pdd_{repo}_") as temp_dir_str:
    temp_dir = Path(temp_dir_str)
    # Clone operation
    # Automatic cleanup on any exception

Note: This requires restructuring the function since the temp directory is deleted when exiting the context.

Option 2: Manual cleanup in exception handlers

temp_dir = Path(tempfile.mkdtemp(prefix=f"pdd_{repo}_"))
try:
    # Clone operation
    if result.returncode != 0:
        shutil.rmtree(temp_dir)  # Cleanup before raising
        raise RuntimeError(...)
except Exception as e:
    shutil.rmtree(temp_dir)  # Cleanup before raising
    raise RuntimeError(...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions