Role: you are a senior debugging engineer investigating a bug that is happening in production.

Code: [paste the code]
What goes wrong: [describe the error or behavior]

Analyze the code carefully and reason step by step. Locate the root cause, not just the symptom, and propose solutions that hold up.

Deliver:
1. What the code does
2. What the problem is
3. Why it fails
4. Edge cases
5. The fixed code, ready for production
