fix: clear cached init on failure + clean up SSE intervals on stream close

- services.ts: Clear _aoServicesInit on rejection so subsequent calls
  retry instead of permanently returning a failed promise
- events/route.ts: Clear both intervals in the updates catch block
  (matching heartbeat behavior) to stop polling after disconnect

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Prateek 2026-02-14 17:34:10 +05:30
parent ecf470599d
commit 07683bcc03
2 changed files with 9 additions and 2 deletions

View File

@ -73,7 +73,9 @@ export async function GET(): Promise<Response> {
controller.enqueue(encoder.encode(`data: ${JSON.stringify(event)}\n\n`));
}
} catch {
// Silently skip failed polls — next interval will retry
// enqueue failure means the stream is closed — clean up both intervals
clearInterval(updates);
clearInterval(heartbeat);
}
})();
}, 5000);

View File

@ -34,7 +34,12 @@ export function getServices(): Promise<Services> {
return Promise.resolve(globalForServices._aoServices);
}
if (!globalForServices._aoServicesInit) {
globalForServices._aoServicesInit = initServices();
globalForServices._aoServicesInit = initServices().catch((err) => {
// Clear the cached promise so the next call retries instead of
// permanently returning a rejected promise.
globalForServices._aoServicesInit = undefined;
throw err;
});
}
return globalForServices._aoServicesInit;
}