Bug: Infinite loop in `gap_analysis.py` preload blocks background runner
@PRAteek-singHWY is already working on this.
Since Apr 29, 2026.
- Dominant language
- Python
- Stars
- 180
- Forks
- 137
- Avg merge
- 3d 23h
- Merged PRs (30d)
- 21
Description
What happened
While looking into gap_analysis.py, I found an infinite loop inside the preload(target_url) function that can permanently lock up our background processes.
The function relies on a while len(waiting): loop. It expects calculate_a_to_b(sa, sb) to eventually return True, which allows it to remove a standard pairing from the waiting array. However, if a pair continuously fails, it will return False forever. The waiting array never empties, and the function just sleeps for 30 seconds before endlessly re-trying the same failing standards.
How it breaks (The Proof)
This isn't just theoretical; the codebase guarantees an infinite loop under two specific scenarios:
- Environmental or Heroku 404s: If
CRE_NO_CALCULATE_GAP_ANALYSISis set (or if a standard is missing on Heroku),/rest/v1/map_analysisexplicitly aborts with a404. Over inpreload,calculate_a_to_bseesstatus_code != 200, immediately returnsFalse, and goes back to sleep. It will query the exact same 404 endpoint 30 seconds later natively forever. - Failed Background Jobs: If a heavy
db.gap_analysisjob actually crashes or times out in the RQ queue, its status becomesFAILED. When thepreloadloop queries it 30 seconds later,/rest/v1/map_analysissees the failure, creates a brand new job, and returns the newjob_id. Our loop sees the newjob_id, assumes it's still just "waiting", returnsFalse, and falls right back asleep. It gets stuck eternally creating failing jobs.
Expected Fix
We should implement a MAX_RETRIES = 10 cap inside the loop. If a specific standard pairing fails repeatedly and crosses the 10-retry threshold, we should drop it from the waiting queue so the rest of the application can safely finish.
Also, iterating over the unresolved pairs via Python Tuples instead of repeating the entire nested standards strings array every time will prevent the application from constantly double-checking pairs that have already succeeded.
Let me know if you would like me to open a PR for this, I have a clean fix tested and ready!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.