devtrack icon indicating copy to clipboard operation
devtrack copied to clipboard

[BUG] [CRITICAL] Serverless Function Deadlocks via Self-Loopback Fetches in AI Insights Route

Open VIDYANKSHINI opened this issue 5 months ago • 10 comments

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

  1. Open the codebase and navigate to src/app/api/ai-insights/route.ts
  2. Scroll to line ~151 where Promise.all is executed.
  3. Observe the 4 consecutive loopback fetch() requests hitting ${baseUrl}/api/metrics/... using the Cookie headers.
  4. 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.

VIDYANKSHINI avatar Jun 19 '26 12:06 VIDYANKSHINI

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, and repos) and identify the underlying service-layer functions they use.
  • Refactor src/app/api/ai-insights/route.ts to 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!

VIDYANKSHINI avatar Jun 19 '26 12:06 VIDYANKSHINI

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!

github-actions[bot] avatar Jun 19 '26 12:06 github-actions[bot]

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.

github-actions[bot] avatar Jun 27 '26 05:06 github-actions[bot]

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!

sakshiwankhade026-coder avatar Jul 03 '26 14:07 sakshiwankhade026-coder

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 avatar Oct 14 '26 17:10 Priyanshu-byte-coder

@Priyanshu-byte-coder This issue is already taken by @VIDYANKSHINI, @sakshiwankhade026-coder. Check other open issues!

github-actions[bot] avatar Oct 14 '26 17:10 github-actions[bot]

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.

Priyanshu-byte-coder avatar Nov 04 '26 03:11 Priyanshu-byte-coder

/assign

officialasishkumar avatar Nov 12 '26 14:11 officialasishkumar

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!

github-actions[bot] avatar Nov 12 '26 14:11 github-actions[bot]

✅ Closed as completed!

If DevTrack has been useful, a ⭐ star on the repo helps the project grow and reach more developers.

github-actions[bot] avatar Nov 14 '26 16:11 github-actions[bot]