Spaces:
Sleeping
Sleeping
Claude Code Claude Opus 4.6 commited on
Commit ·
0c79ba5
1
Parent(s): 1c7b56d
Claude Code: Fix Cain error field handling - clear stale 'unknown' at startup
Browse files- entrypoint.sh: Add python script to clear error field from cain_status.json before startup
- app.py: Prevent writing 'unknown' as error value - use None instead
This fixes the recurring issue where stale 'unknown' errors persist across
container restarts, causing health checks to report errors when Cain is healthy.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- app.py +7 -1
- entrypoint.sh +20 -0
app.py
CHANGED
|
@@ -435,7 +435,13 @@ try:
|
|
| 435 |
# CRITICAL: Explicitly set error to null when imports succeed
|
| 436 |
status_data["error"] = None
|
| 437 |
except Exception as e:
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
# Add explanatory note about error field semantics
|
| 441 |
status_data["_note"] = "error=null means healthy - null or 'unknown' string are both treated as 'no error'"
|
|
|
|
| 435 |
# CRITICAL: Explicitly set error to null when imports succeed
|
| 436 |
status_data["error"] = None
|
| 437 |
except Exception as e:
|
| 438 |
+
error_msg = f"{type(e).__name__}: {e}"
|
| 439 |
+
# CRITICAL: Never write "unknown" as error - use specific error or None
|
| 440 |
+
# "unknown" is treated as null/healthy, so avoid ambiguity
|
| 441 |
+
if error_msg.strip().lower() in ("unknown", "none", "null", ""):
|
| 442 |
+
status_data["error"] = None
|
| 443 |
+
else:
|
| 444 |
+
status_data["error"] = error_msg
|
| 445 |
|
| 446 |
# Add explanatory note about error field semantics
|
| 447 |
status_data["_note"] = "error=null means healthy - null or 'unknown' string are both treated as 'no error'"
|
entrypoint.sh
CHANGED
|
@@ -44,6 +44,26 @@ mkdir -p /app/logs
|
|
| 44 |
# Ensure cain_status.json directory exists
|
| 45 |
mkdir -p /data
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
echo ""
|
| 48 |
echo "=========================================="
|
| 49 |
echo "Starting uvicorn..."
|
|
|
|
| 44 |
# Ensure cain_status.json directory exists
|
| 45 |
mkdir -p /data
|
| 46 |
|
| 47 |
+
# CRITICAL: Clear stale error field from cain_status.json before startup
|
| 48 |
+
# This prevents "unknown" errors from persisting across container restarts
|
| 49 |
+
if [ -f "/data/cain_status.json" ]; then
|
| 50 |
+
echo "Cleaning stale error field from status file..."
|
| 51 |
+
# Use python to safely update JSON (sets error to null)
|
| 52 |
+
python3 -c "
|
| 53 |
+
import json
|
| 54 |
+
try:
|
| 55 |
+
with open('/data/cain_status.json', 'r') as f:
|
| 56 |
+
data = json.load(f)
|
| 57 |
+
data['error'] = None
|
| 58 |
+
data['_cleaned_at'] = '$(date -u +"%Y-%m-%dT%H:%M:%SZ")'
|
| 59 |
+
with open('/data/cain_status.json', 'w') as f:
|
| 60 |
+
json.dump(data, f, indent=2)
|
| 61 |
+
print('Status file error field cleared')
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f'Could not clean status file: {e}')
|
| 64 |
+
" 2>/dev/null || echo "(Could not clean status file - continuing)"
|
| 65 |
+
fi
|
| 66 |
+
|
| 67 |
echo ""
|
| 68 |
echo "=========================================="
|
| 69 |
echo "Starting uvicorn..."
|