[Bug] [Critical] Claude Code Gets Stuck in Infinite Loop When Reading Invalid Image Files

Resolved 💬 3 comments Opened Sep 4, 2025 by kolkov Closed Sep 8, 2025

Bug Report: Claude Code Gets Stuck in Infinite Loop When Reading Invalid Image Files

Summary

Claude Code enters an unrecoverable state when attempting to read a file with .png extension that contains non-image data (e.g., JSON), repeatedly trying to send the invalid image despite user commands to cancel.

Product Information

  • Product: Claude Code (Desktop)
  • Version: Latest (as of September 2025)
  • Platform: Windows 10/11
  • Date: September 4, 2025

Bug Description

Current Behavior

When Claude Code attempts to read a .png file that contains non-PNG data (such as JSON text):

  1. The Read tool attempts to process the file as an image
  2. The API returns error 400: "Could not process image"
  3. Claude Code automatically retries sending the same invalid data
  4. User commands like "cancel", "stop", "skip" are ignored
  5. The only solution is to exit Claude Code completely

Expected Behavior

  • Claude Code should detect file format mismatch before sending
  • On API error 400, it should stop attempting to send the file
  • User should be able to cancel/skip the operation
  • Claude Code should validate file content matches extension

Error Messages

{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "Could not process image"
  },
  "request_id": "req_011CSo7M7NfnMyhmTVhswmmo"
}

Steps to Reproduce

  1. Create a file with .png extension containing JSON data:
echo '{"data":"base64string","format":"png"}' > test.png
  1. In Claude Code, attempt to read the file:
Read(test.png)
  1. Observe the error loop begins
  1. Try to cancel with commands:
  • "cancel"
  • "stop"
  • "skip this file"
  1. Result: Commands are ignored, Claude Code continues attempting to send invalid data

Root Cause Analysis

Issues Identified:

  1. No pre-validation: File content is not validated before API submission
  2. No retry limit: Infinite retry loop on 400 errors
  3. Command processing blocked: User input is not processed during error state
  4. File type assumption: Extension is trusted without verification

Impact

  • Severity: High
  • User Experience: Forces complete restart of Claude Code
  • Productivity: Significant disruption to workflow
  • Data Loss Risk: Unsaved work may be lost on forced exit

Proposed Solutions

1. Pre-flight Validation

def validate_image_file(filepath):
    with open(filepath, 'rb') as f:
        header = f.read(8)
        # PNG magic bytes: 89 50 4E 47 0D 0A 1A 0A
        if filepath.endswith('.png'):
            return header.startswith(b'\x89PNG\r\n\x1a\n')
        # Add other formats...
    return False

2. Retry Limit Implementation

MAX_RETRIES = 3
retry_count = 0

while retry_count < MAX_RETRIES:
    try:
        send_image(data)
        break
    except InvalidImageError:
        retry_count += 1
        if retry_count >= MAX_RETRIES:
            log_error("Failed to send image after {} attempts".format(MAX_RETRIES))
            break

3. User Command Interrupt Handler

def handle_user_interrupt():
    commands = ["cancel", "stop", "skip"]
    if any(cmd in user_input.lower() for cmd in commands):
        cancel_current_operation()
        return True
    return False

4. Graceful Error Recovery

  • Show clear error message to user
  • Offer options: Retry, Skip, or Cancel
  • Log the error for debugging
  • Continue with next operation

Workarounds (Current)

  1. Avoid reading files with mismatched content/extension
  2. Validate file format before reading:
file myfile.png  # Check actual format
  1. If stuck, immediately exit Claude Code (Ctrl+C or close window)

Test Cases

  1. JSON data in .png file
  2. XML data in .jpg file
  3. Binary data in .txt file
  4. Corrupted image headers
  5. Empty files with image extensions

Expected Fix Behavior

User: Read(screenshot.png)
Claude: Error: File 'screenshot.png' contains JSON data, not PNG image data.
        Would you like me to:
        1. Read it as JSON
        2. Skip this file
        3. Show raw content
User: 1
Claude: [Reads and displays JSON content]

Related Issues

  • Image processing errors with base64 encoded data
  • File type detection failures
  • Command processing during error states

Priority

Critical - Causes complete loss of control requiring application restart

Additional Notes

  • This issue commonly occurs when APIs return JSON instead of binary image data
  • Affects automation workflows and CI/CD pipelines
  • Particularly problematic when working with screenshot APIs that may return different formats

---

Reported by: GODA Development Team
Contact: [Report filed via GitHub Issues]
Reference: Case encountered during GODA Browser Automation API development

Attachments

  • Error logs showing retry loop
  • Example files that trigger the issue
  • Screen recording of the bug (available upon request)

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗