[BUG] [CRITICAL] Serverless Function Deadlocks via Self-Loopback Fetches in AI Insights Route
Bug Description
What happened?
In the newly added src/app/api/ai-insights/route.ts, the backend endpoint gathers data for the AI Mentor by making 4 parallel fetch() HTTP requests back to its own server's API routes (/api/metrics/contributions, /api/metrics/prs, /api/metrics/streak, and /api/metrics/repos).
In serverless environments (like Vercel), execution workers are strictly limited. A single request to /api/ai-insights consumes 1 worker, which then forces the infrastructure to spawn 4 additional concurrent workers to handle the loopback requests. Under even moderate traffic, this causes rapid connection pool exhaustion, leading to a catastrophic deadlock where workers wait on each other and ultimately fail with 504 Gateway Timeouts.
What did you expect instead?
Following Next.js App Router best practices, server-side route handlers should never invoke their own API routes via HTTP fetch(). The endpoint should directly import and invoke the underlying server-side service functions (e.g., fetchContributionsMetrics(), fetchPRMetrics(), etc.) located in src/lib/ to bypass HTTP networking entirely.
Steps to Reproduce
- Open the codebase and navigate to
src/app/api/ai-insights/route.ts - Scroll to line
~151wherePromise.allis executed. - Observe the 4 consecutive loopback
fetch()requests hitting${baseUrl}/api/metrics/...using theCookieheaders. - Deploying this logic to a serverless edge and hitting the AI Mentor endpoint with parallel users will immediately trigger Vercel 504 timeouts due to worker exhaustion.
Affected Area
API Routes
Screenshots
N/A - Architectural / Backend issue
Browser & OS
No response
Environment
Production (devtrack.site)
Additional Context
This represents a known Next.js anti-pattern that multiplies cold start latency. Because the incoming request already decrypted the NextAuth session, making 4 new external HTTP calls forces Next.js to reconstruct the headers and decrypt the session token 4 more times redundantly. Refactoring this to use direct function calls will drastically improve dashboard loading speeds and prevent production outages.
Hi @Priyanshu-byte-coder,
I would like to work on this issue.
After reviewing src/app/api/ai-insights/route.ts, I agree that the current loopback fetch() pattern can create unnecessary serverless worker utilization, increased latency, and redundant session/token processing.
My plan is:
- Audit the existing metric endpoints (
contributions,prs,streak, andrepos) and identify the underlying service-layer functions they use. - Refactor
src/app/api/ai-insights/route.tsto directly import and invoke those server-side functions instead of making HTTP requests to internal API routes. - Reuse the already authenticated session/token within the request lifecycle to avoid repeated authentication overhead.
- Preserve the existing API response structure so the AI Mentor functionality remains unchanged.
- Validate performance improvements and ensure all metrics are still aggregated correctly.
I have experience working on DevTrack issues and would be happy to submit a PR promptly.
Please assign this issue to me under GSSoC'26.
Thank you!
Assigned to @VIDYANKSHINI! You have 7 days to open a PR. No PR after 7 days = auto-unassigned so others can pick it up. Good luck!
Hey @VIDYANKSHINI — this issue was assigned 7 days ago with no PR opened. Unassigning so others can pick it up.
Still working on it? Drop a comment and request re-assignment.
Hi 👋
I would like to work on this issue under GSSoC'26.
I have previously contributed to open-source projects and have 5 PRs successfully merged. I have gone through the issue requirements and would be happy to work on the implementation while following the project's guidelines and maintaining code quality.
Please assign this issue to me if it is available.
Thank you!
Hey @VIDYANKSHINI, @sakshiwankhade026-coder — this issue was assigned a while back with no recent activity. Unassigning so it's available for Hacktoberfest contributors. Still working on it? Comment and we'll reassign you.
@Priyanshu-byte-coder This issue is already taken by @VIDYANKSHINI, @sakshiwankhade026-coder. Check other open issues!
Update: /api/ai-insights no longer calls its own API routes. The same pattern is still in src/app/api/personality/route.ts (around line 174: four fetch(${baseUrl}/api/metrics/...) calls with the session cookie). Keeping this open for that route: it should call the metric helpers in src/lib/ directly.
/assign
Assigned to @officialasishkumar! You have 7 days to open a PR. No PR after 7 days = auto-unassigned so others can pick it up. Good luck!
✅ Closed as completed!
If DevTrack has been useful, a ⭐ star on the repo helps the project grow and reach more developers.