Files
CatoandClaude 19ffeaff74 fix(auth): a session read that hangs cannot take the site down (#298)
On 2026-10-30 every Substrata page answered 502 after 23 seconds for hours
while /api/health still answered 200: session reads hung (get-session took
251 s) and every page waits for one. A restart cleared it; the cause was not
visible from outside the box.

- currentSession() gives up after 4 s and answers "signed out", logged, so a
  public page always renders.
- Accounts read from their own small pool, so a session read never queues
  behind long research queries for one of the five shared connections, and
  the server cancels any account query after 5 s instead of letting it hold
  its connection forever.


Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-30 10:00:49 +01:00

18 lines
554 B
TypeScript

/**
* `promise`, or a rejection once `ms` have passed without an answer. The
* timer is always cleared, so a fast answer leaves nothing running.
*/
export async function within<T>(promise: Promise<T>, ms: number, what: string): Promise<T> {
let timer: ReturnType<typeof setTimeout> | undefined;
try {
return await Promise.race([
promise,
new Promise<never>((_, reject) => {
timer = setTimeout(() => reject(new Error(`${what}: no answer within ${ms}ms`)), ms);
}),
]);
} finally {
clearTimeout(timer);
}
}