[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):
- The Read tool attempts to process the file as an image
- The API returns error 400:
"Could not process image" - Claude Code automatically retries sending the same invalid data
- User commands like "cancel", "stop", "skip" are ignored
- 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
- Create a file with
.pngextension containing JSON data:
echo '{"data":"base64string","format":"png"}' > test.png
- In Claude Code, attempt to read the file:
Read(test.png)
- Observe the error loop begins
- Try to cancel with commands:
- "cancel"
- "stop"
- "skip this file"
- Result: Commands are ignored, Claude Code continues attempting to send invalid data
Root Cause Analysis
Issues Identified:
- No pre-validation: File content is not validated before API submission
- No retry limit: Infinite retry loop on 400 errors
- Command processing blocked: User input is not processed during error state
- 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)
- Avoid reading files with mismatched content/extension
- Validate file format before reading:
file myfile.png # Check actual format
- If stuck, immediately exit Claude Code (Ctrl+C or close window)
Test Cases
- JSON data in .png file
- XML data in .jpg file
- Binary data in .txt file
- Corrupted image headers
- 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)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗