Claude
commited on
docs(audit): add comprehensive deep dive bug report
Browse filesDocument all bugs found in the codebase audit:
- P1: ErrorBoundary, bare exception suppression, case validation (Fixed)
- P2: JSON parse errors, thread safety, temp directory cleanup
- P3: Untyped errors, hardcoded filenames, unused dependencies
Includes web research findings on HF Spaces issues and code quality
observations (both positive patterns and anti-patterns).
- BUGS-AUDIT-DEEP-DIVE.md +321 -0
BUGS-AUDIT-DEEP-DIVE.md
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Deep Dive Audit: Additional Bugs and Anti-Patterns
|
| 2 |
+
|
| 3 |
+
**Date**: 2025-12-12
|
| 4 |
+
**Auditor**: Claude Code Deep Dive
|
| 5 |
+
**Scope**: Full codebase audit beyond initial HF Spaces integration issues
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## Executive Summary
|
| 10 |
+
|
| 11 |
+
Following the HF Spaces integration fixes (BUGS-HF-SPACES-INTEGRATION.md), this audit examined the entire codebase for hidden bugs, anti-patterns, silent failures, and incomplete implementations. Several issues were found that could impact reliability and debuggability.
|
| 12 |
+
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
## P1 - HIGH PRIORITY
|
| 16 |
+
|
| 17 |
+
### BUG-010: Missing Error Boundary in React App
|
| 18 |
+
|
| 19 |
+
**Severity**: P1 - HIGH
|
| 20 |
+
**Impact**: Unhandled React errors crash entire frontend
|
| 21 |
+
**File**: `frontend/src/App.tsx`
|
| 22 |
+
|
| 23 |
+
#### Problem
|
| 24 |
+
|
| 25 |
+
No React Error Boundary wraps the application. If NiiVueViewer (WebGL), ProgressIndicator, or any component throws during render:
|
| 26 |
+
1. Entire app crashes to white screen
|
| 27 |
+
2. User loses all progress/state
|
| 28 |
+
3. No user-friendly error message
|
| 29 |
+
|
| 30 |
+
#### Evidence
|
| 31 |
+
|
| 32 |
+
```tsx
|
| 33 |
+
// App.tsx - No error boundary
|
| 34 |
+
export default function App() {
|
| 35 |
+
return (
|
| 36 |
+
<Layout>
|
| 37 |
+
{/* If any child throws, app crashes */}
|
| 38 |
+
<NiiVueViewer ... /> // WebGL can throw
|
| 39 |
+
</Layout>
|
| 40 |
+
)
|
| 41 |
+
}
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
#### Fix Required
|
| 45 |
+
|
| 46 |
+
Add ErrorBoundary component wrapping the main app content.
|
| 47 |
+
|
| 48 |
+
**Status**: Fixed in this commit
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
### BUG-011: Bare Exception Suppression Loses Diagnostic Info
|
| 53 |
+
|
| 54 |
+
**Severity**: P1 - HIGH
|
| 55 |
+
**Impact**: Silent failures impossible to debug
|
| 56 |
+
**File**: `src/stroke_deepisles_demo/api/routes.py:230-231`
|
| 57 |
+
|
| 58 |
+
#### Problem
|
| 59 |
+
|
| 60 |
+
```python
|
| 61 |
+
with contextlib.suppress(Exception):
|
| 62 |
+
volume_ml = round(compute_volume_ml(result.prediction_mask, threshold=0.5), 2)
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
This suppresses ALL exceptions including:
|
| 66 |
+
- FileNotFoundError (mask file missing)
|
| 67 |
+
- ValueError (invalid threshold)
|
| 68 |
+
- MemoryError (volume too large)
|
| 69 |
+
- AttributeError (bad prediction object)
|
| 70 |
+
|
| 71 |
+
When `volume_ml` is `None`, users have no idea why.
|
| 72 |
+
|
| 73 |
+
#### Fix Required
|
| 74 |
+
|
| 75 |
+
Log the exception, return None only for expected errors.
|
| 76 |
+
|
| 77 |
+
**Status**: Fixed in this commit
|
| 78 |
+
|
| 79 |
+
---
|
| 80 |
+
|
| 81 |
+
### BUG-012: No Case ID Validation Before Job Creation
|
| 82 |
+
|
| 83 |
+
**Severity**: P1 - MEDIUM
|
| 84 |
+
**Impact**: Jobs fail after 30+ seconds instead of immediately
|
| 85 |
+
**File**: `src/stroke_deepisles_demo/api/routes.py:81-103`
|
| 86 |
+
|
| 87 |
+
#### Problem
|
| 88 |
+
|
| 89 |
+
```python
|
| 90 |
+
def create_segment_job(..., body: SegmentRequest, ...):
|
| 91 |
+
# body.case_id is trusted without validation
|
| 92 |
+
store.create_job(job_id, body.case_id, body.fast_mode)
|
| 93 |
+
# If case_id is invalid, job fails later with confusing error
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
If a user submits an invalid case ID like "nonexistent-case", the job is created, but fails ~30 seconds later with a generic "Segmentation failed" message.
|
| 97 |
+
|
| 98 |
+
#### Fix Required
|
| 99 |
+
|
| 100 |
+
Validate case_id exists before creating job, return 400 immediately if invalid.
|
| 101 |
+
|
| 102 |
+
**Status**: Fixed in this commit
|
| 103 |
+
|
| 104 |
+
---
|
| 105 |
+
|
| 106 |
+
## P2 - MEDIUM PRIORITY
|
| 107 |
+
|
| 108 |
+
### BUG-013: Silent JSON Parse Failures in API Client
|
| 109 |
+
|
| 110 |
+
**Severity**: P2 - MEDIUM
|
| 111 |
+
**Impact**: Generic error messages instead of server details
|
| 112 |
+
**File**: `frontend/src/api/client.ts:50,85,120`
|
| 113 |
+
|
| 114 |
+
#### Problem
|
| 115 |
+
|
| 116 |
+
```typescript
|
| 117 |
+
const error = await response.json().catch(() => ({}))
|
| 118 |
+
// If JSON parsing fails, error becomes {} and error.detail is undefined
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
When the backend returns malformed JSON or HTML (502 proxy error pages), the error message becomes generic because `error.detail` is undefined.
|
| 122 |
+
|
| 123 |
+
#### Fix Required
|
| 124 |
+
|
| 125 |
+
Log parse failures in development, provide more context in error messages.
|
| 126 |
+
|
| 127 |
+
**Status**: Fixed in this commit
|
| 128 |
+
|
| 129 |
+
---
|
| 130 |
+
|
| 131 |
+
### BUG-014: Job Store Thread Safety Concern
|
| 132 |
+
|
| 133 |
+
**Severity**: P2 - LOW (in practice)
|
| 134 |
+
**Impact**: Potential race conditions in multi-worker deployment
|
| 135 |
+
**File**: `src/stroke_deepisles_demo/api/job_store.py:183-193`
|
| 136 |
+
|
| 137 |
+
#### Problem
|
| 138 |
+
|
| 139 |
+
```python
|
| 140 |
+
def get_job(self, job_id: str) -> Job | None:
|
| 141 |
+
with self._lock:
|
| 142 |
+
return self._jobs.get(job_id) # Returns reference, not copy
|
| 143 |
+
|
| 144 |
+
# Caller can then modify returned job outside lock
|
| 145 |
+
job = store.get_job(job_id)
|
| 146 |
+
if job:
|
| 147 |
+
job.status = JobStatus.RUNNING # Race if another thread modifies
|
| 148 |
+
```
|
| 149 |
+
|
| 150 |
+
#### Mitigating Factors
|
| 151 |
+
|
| 152 |
+
- HF Spaces runs single uvicorn worker
|
| 153 |
+
- Code comments explicitly document this limitation
|
| 154 |
+
- All mutation methods use locks
|
| 155 |
+
|
| 156 |
+
#### Recommendation
|
| 157 |
+
|
| 158 |
+
Document that multi-worker deployment requires shared store (Redis).
|
| 159 |
+
|
| 160 |
+
**Status**: Documented, low-priority fix
|
| 161 |
+
|
| 162 |
+
---
|
| 163 |
+
|
| 164 |
+
### BUG-015: HuggingFace Dataset Temp Directory Leak on Exception
|
| 165 |
+
|
| 166 |
+
**Severity**: P2 - MEDIUM
|
| 167 |
+
**Impact**: Orphaned temp directories if download fails
|
| 168 |
+
**File**: `src/stroke_deepisles_demo/data/adapter.py:216-223`
|
| 169 |
+
|
| 170 |
+
#### Problem
|
| 171 |
+
|
| 172 |
+
```python
|
| 173 |
+
if self._temp_dir is None:
|
| 174 |
+
self._temp_dir = Path(tempfile.mkdtemp(prefix="isles24_hf_"))
|
| 175 |
+
|
| 176 |
+
# If _download_case_from_parquet() raises AFTER temp dir creation,
|
| 177 |
+
# and caller doesn't use context manager, temp dir leaks
|
| 178 |
+
case_data = self._download_case_from_parquet(file_idx, subject_id)
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
#### Mitigating Factors
|
| 182 |
+
|
| 183 |
+
- Pipeline code uses context manager correctly
|
| 184 |
+
- Temp directory is small (~50MB per case)
|
| 185 |
+
- HF Spaces ephemeral storage clears on restart
|
| 186 |
+
|
| 187 |
+
#### Status
|
| 188 |
+
|
| 189 |
+
Low-priority, documented
|
| 190 |
+
|
| 191 |
+
---
|
| 192 |
+
|
| 193 |
+
## P3 - LOW PRIORITY
|
| 194 |
+
|
| 195 |
+
### BUG-016: Console.warn with Untyped Error
|
| 196 |
+
|
| 197 |
+
**Severity**: P3 - LOW
|
| 198 |
+
**Impact**: Potential uncaught exception in error logging
|
| 199 |
+
**File**: `frontend/src/hooks/useSegmentation.ts:115`
|
| 200 |
+
|
| 201 |
+
```typescript
|
| 202 |
+
console.warn('Polling error (will retry):', err)
|
| 203 |
+
// If err is circular object, could cause issues
|
| 204 |
+
```
|
| 205 |
+
|
| 206 |
+
---
|
| 207 |
+
|
| 208 |
+
### BUG-017: Hardcoded NIfTI Output Filename Detection
|
| 209 |
+
|
| 210 |
+
**Severity**: P3 - LOW
|
| 211 |
+
**Impact**: Fragile if DeepISLES changes output naming
|
| 212 |
+
**File**: `src/stroke_deepisles_demo/inference/deepisles.py:96-117`
|
| 213 |
+
|
| 214 |
+
```python
|
| 215 |
+
possible_names = [
|
| 216 |
+
"prediction.nii.gz",
|
| 217 |
+
"pred.nii.gz",
|
| 218 |
+
# ... many hardcoded names
|
| 219 |
+
]
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
If DeepISLES updates output naming convention, code silently picks wrong file.
|
| 223 |
+
|
| 224 |
+
---
|
| 225 |
+
|
| 226 |
+
### BUG-018: Subprocess Stderr Potentially Exposed
|
| 227 |
+
|
| 228 |
+
**Severity**: P3 - LOW (Security)
|
| 229 |
+
**Impact**: Internal paths/config may leak to frontend
|
| 230 |
+
**File**: `src/stroke_deepisles_demo/inference/direct.py:207-211`
|
| 231 |
+
|
| 232 |
+
```python
|
| 233 |
+
raise DeepISLESError(
|
| 234 |
+
f"DeepISLES inference failed with exit code {result.returncode}. "
|
| 235 |
+
f"stderr: {result.stderr}" # May contain sensitive paths
|
| 236 |
+
)
|
| 237 |
+
```
|
| 238 |
+
|
| 239 |
+
This is logged but sanitized before reaching the frontend (routes.py:264 returns generic message).
|
| 240 |
+
|
| 241 |
+
---
|
| 242 |
+
|
| 243 |
+
### BUG-019: Unused Frontend Dependencies
|
| 244 |
+
|
| 245 |
+
**Severity**: P3 - LOW
|
| 246 |
+
**Impact**: Bundle size bloat
|
| 247 |
+
**File**: `frontend/package.json`
|
| 248 |
+
|
| 249 |
+
These dependencies appear unused:
|
| 250 |
+
- `cropperjs` - No image cropping in the app
|
| 251 |
+
- `lazy-brush` - No brush/drawing functionality
|
| 252 |
+
- `resize-observer-polyfill` - Modern browsers don't need this
|
| 253 |
+
|
| 254 |
+
---
|
| 255 |
+
|
| 256 |
+
## Issues Already Fixed (This PR)
|
| 257 |
+
|
| 258 |
+
| Bug ID | Issue | Fix |
|
| 259 |
+
|--------|-------|-----|
|
| 260 |
+
| BUG-001 | Cold Start / 503 Handling | Added `waking_up` state with retry |
|
| 261 |
+
| BUG-002 | CORS Regex Security | Anchored to specific origin |
|
| 262 |
+
| BUG-003 | COOP/COEP Headers | Added custom_headers |
|
| 263 |
+
| BUG-004 | Hardware Requirements | Documented GPU needs |
|
| 264 |
+
| BUG-005 | Ephemeral Disk Warning | Added warning to results |
|
| 265 |
+
| BUG-007 | WebGL2 Documentation | Added browser requirements |
|
| 266 |
+
| BUG-008 | Fork Deployment Docs | Added instructions |
|
| 267 |
+
| BUG-009 | FRONTEND_ORIGIN | Added to Dockerfile |
|
| 268 |
+
|
| 269 |
+
---
|
| 270 |
+
|
| 271 |
+
## Code Quality Observations
|
| 272 |
+
|
| 273 |
+
### Positive Patterns Found
|
| 274 |
+
|
| 275 |
+
1. **Thread-safe job store** - Proper RLock usage for mutations
|
| 276 |
+
2. **Context manager for datasets** - Ensures temp file cleanup
|
| 277 |
+
3. **Path traversal protection** - Job ID validation, path.is_relative_to() checks
|
| 278 |
+
4. **Typed API contracts** - Pydantic schemas match TypeScript types
|
| 279 |
+
5. **Graceful WebGL cleanup** - Forces context loss to free GPU memory
|
| 280 |
+
6. **Exponential backoff** - Proper retry logic for cold starts
|
| 281 |
+
|
| 282 |
+
### Anti-Patterns Found
|
| 283 |
+
|
| 284 |
+
1. **Bare exception suppression** - `contextlib.suppress(Exception)` hides bugs
|
| 285 |
+
2. **Silent JSON parse fallback** - `.catch(() => ({}))` loses error info
|
| 286 |
+
3. **No input validation** - Case ID not validated before job creation
|
| 287 |
+
4. **Missing error boundary** - React crashes not caught
|
| 288 |
+
|
| 289 |
+
---
|
| 290 |
+
|
| 291 |
+
## Web Research Findings
|
| 292 |
+
|
| 293 |
+
Based on December 2025 HuggingFace Spaces documentation and forum discussions:
|
| 294 |
+
|
| 295 |
+
1. **504 Gateway Timeouts** ([HF Forums](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018)): Gradio/FastAPI requests timing out after 60s. Our async job queue pattern correctly handles this.
|
| 296 |
+
|
| 297 |
+
2. **CORS with Private Spaces** ([HF Forums](https://discuss.huggingface.co/t/getting-a-cors-error-when-embedding-the-my-private-space/142466)): CORS errors when embedding private spaces. Our public space configuration is correct.
|
| 298 |
+
|
| 299 |
+
3. **custom_headers Support** ([HF Docs](https://huggingface.co/docs/hub/spaces-config-reference)): Confirmed that COEP/COOP/CORP headers are supported in static spaces. Our configuration matches documentation.
|
| 300 |
+
|
| 301 |
+
4. **SharedArrayBuffer Requirements** ([Chrome Blog](https://developer.chrome.com/blog/enabling-shared-array-buffer)): Cross-origin isolation required for SharedArrayBuffer. Our headers enable this correctly.
|
| 302 |
+
|
| 303 |
+
---
|
| 304 |
+
|
| 305 |
+
## Summary Table
|
| 306 |
+
|
| 307 |
+
| Priority | Count | Status |
|
| 308 |
+
|----------|-------|--------|
|
| 309 |
+
| **P1** | 3 | Fixed in this PR |
|
| 310 |
+
| **P2** | 3 | 1 Fixed, 2 Documented |
|
| 311 |
+
| **P3** | 4 | Documented |
|
| 312 |
+
|
| 313 |
+
---
|
| 314 |
+
|
| 315 |
+
## Sources
|
| 316 |
+
|
| 317 |
+
- [HuggingFace Spaces Config Reference](https://huggingface.co/docs/hub/spaces-config-reference)
|
| 318 |
+
- [HF Forum: 504 Gateway Timeout](https://discuss.huggingface.co/t/504-gateway-timeout-with-http-request/24018)
|
| 319 |
+
- [HF Forum: CORS Issues](https://discuss.huggingface.co/t/cors-issue-with-huggingface-spaces-and-netlify-hosted-react-app/62634)
|
| 320 |
+
- [Chrome: SharedArrayBuffer Requirements](https://developer.chrome.com/blog/enabling-shared-array-buffer)
|
| 321 |
+
- [GitHub: COEP/COOP Headers Issue](https://github.com/huggingface/huggingface_hub/issues/1525)
|