1. Introduction: The "Loading..." Screen That Freezes
Customer complaints poured in: "The app is frozen and won't move." Upon investigation, the screen was frozen for over 30 seconds when calling an external API.
The cause was the complete absence of timeout settings in the API call code generated by Genspark. If the external API did not respond, the program would wait indefinitely, leaving users in a state where they could do nothing.
What you will learn in this article
- Actual damage caused by missing timeout handling
- 4 timeout points AI tends to overlook
- Correct timeout implementation by language and library
- How to determine appropriate timeout values
- Recovery strategies after a timeout
2. Incident Details: Missing Timeout for External API Calls
Let's look at the problematic code generated by Genspark.
2.1 Problematic Code
import requests
def fetch_user_profile(user_id):
"""Fetches user profile from an external API"""
url = f"https://api.example.com/users/{user_id}"
# No timeout setting!
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return None
2.2 What Was the Problem?
This code does not specify a timeout parameter in requests.get(). As a result:
- If the external API does not respond, it waits indefinitely.
- Users perceive the screen as frozen.
- Server resources are wasted.
- Other processes may also be blocked.
2.3 Actual Damage
Impact of Freezing
- User Complaints: 47 cases of "app not working"
- Average Freeze Time: 28.3 seconds
- Churn Rate: Increased by 23%
- App Store Rating: Declined from 4.2 to 3.8
2.4 Why It Wasn't Noticed in the Local Environment
In the development environment, the external API always responded quickly, so the need for a timeout was not noticed. In the production environment, temporary API server failures and network delays occurred, bringing the problem to light.
3. Why Timeout Handling Is Crucial
Timeout handling is fundamental to defensive programming.
3.1 Uncertainty of External Dependencies
External systems (APIs, databases, file systems) do not always respond normally:
- Temporary server failures
- Network delays/disconnections
- Response delays due to overload
- Security issues like DDoS attacks
3.2 Efficient Resource Utilization
Without timeouts, limited resources are wasted:
- Threads/Processes: Occupied in a waiting state
- Memory: Connection information is continuously held
- Network Connections: Connection pool is exhausted
3.3 Improved User Experience
Appropriate timeouts enable prompt error handling:
- Notify "Currently unavailable" within seconds
- Provide alternatives (cached data)
- Offer retry options
4. 4 Timeout Points AI Tends to Overlook
We explain where AIs like Genspark tend to forget timeout settings.
4.1 Point 1: HTTP/API Requests
This is the most common oversight.
Oversight Patterns
- No timeout in requests.get() / requests.post()
- No timeout in urllib.request.urlopen()
- No timeout in axios / fetch
4.2 Point 2: Database Queries
Complex queries or fetching large amounts of data can take longer than expected.
4.3 Point 3: File I/O
Accessing network drives or slow storage can cause freezes.
4.4 Point 4: External Process Execution
External commands may not terminate when using subprocess.run() or os.system().
5. Correct Timeout Implementation: By Language and Library
We introduce how to correctly implement timeouts for each language and library.
5.1 Python - requests
import requests
from requests.exceptions import Timeout, RequestException
def fetch_user_profile(user_id, timeout=5):
"""Fetches user profile from an external API"""
url = f"https://api.example.com/users/{user_id}"
try:
# Connection timeout 3 seconds, read timeout 5 seconds
response = requests.get(url, timeout=(3, 5))
response.raise_for_status()
return response.json()
except Timeout:
logger.error(f"Timeout fetching user {user_id}")
return None
except RequestException as e:
logger.error(f"Error: {e}")
return None
5.2 Python - Databases
# Set timeout during connection
conn = pymysql.connect(
host='localhost',
user='user',
password='pass',
database='db',
connect_timeout=5, # Connection timeout
read_timeout=10, # Read timeout
write_timeout=10 # Write timeout
)
5.3 JavaScript - fetch API
async function fetchUserProfile(userId, timeout = 5000) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(
`https://api.example.com/users/${userId}`,
{ signal: controller.signal }
);
clearTimeout(timeoutId);
return await response.json();
} catch (error) {
if (error.name === 'AbortError') {
console.error('Fetch timeout');
}
return null;
}
}
5.4 Python - subprocess
try:
# 10-second timeout for external command
result = subprocess.run(
['external_command', 'arg1'],
timeout=10,
capture_output=True
)
except subprocess.TimeoutExpired:
logger.error("Command timeout")
# Termination process
6. Best Practices for Timeout Configuration
We introduce how to determine appropriate timeout values and operational strategies.
6.1 How to Determine Timeout Values
Recommended Timeout Values
- API Calls: 5-10 seconds (3 seconds for fast APIs)
- Database Queries: 10-30 seconds (60 seconds for complex queries)
- File Downloads: 60-300 seconds (depending on size)
- External Processes: 30-120 seconds (depending on processing content)
6.2 Gradual Timeouts
Set timeouts in multiple stages:
- Connection Timeout: Shorter (3-5 seconds)
- Read Timeout: Medium (5-10 seconds)
- Overall Timeout: Longer (10-30 seconds)
6.3 Retry Strategy
from requests.exceptions import Timeout
def fetch_with_retry(url, max_retries=3, timeout=5):
"""Automatically retries on timeout"""
for attempt in range(max_retries):
try:
response = requests.get(url, timeout=timeout)
return response.json()
except Timeout:
if attempt < max_retries - 1:
wait_time = 2 ** attempt # Exponential backoff
logger.warning(f"Retry {attempt + 1}, wait {wait_time}s")
time.sleep(wait_time)
else:
raise
return None
6.4 Fallback Strategy
Prepare alternative measures in case of a timeout:
- Cached Data: Display even old data
- Default Value: Return a safe default value
- User Notification: Clearly communicate the situation
6.5 Monitoring and Alerts
Metrics to Monitor
- Timeout Rate: What percentage of all requests timed out
- Average Response Time: Is it approaching the timeout value
- Retry Count: Are excessive retries occurring
7. Summary: The Importance of Defensive Programming
The lack of timeout handling is a serious issue that significantly degrades user experience.
5 Lessons Learned
- Timeout for all external calls: APIs, DBs, and file I/O alike
- Appropriate Value Settings: Realistic values based on processing content
- Gradual Timeouts: Set for connection, read, and overall
- Retry Strategy: Automatic retry with exponential backoff
- Monitoring and Alerts: Constantly monitor for timeouts
Code generated by Genspark tends to only assume the "happy path". Defensive programming that considers real-world uncertainties such as error cases, timeouts, and network failures is essential.
Related Articles
- The Night a Genspark-Generated Infinite Loop Crashed Our Server
- Genspark Freezes/Loops: How to Protect Yourself from Sudden Stoppages
- 10 Lessons Learned from Genspark Development