Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: cancel Beeper retry backoff at deadline (#7176)

[._.]/ Adam Eivy13ba65e78cf1
diff --git a/server/lib/fetchWithTimeout.js b/server/lib/fetchWithTimeout.js
index 5f2421efa5..187522fc2d 100644
--- a/server/lib/fetchWithTimeout.js
+++ b/server/lib/fetchWithTimeout.js
@@ -35,11 +35,33 @@ export function fetchWithTimeout(url, options = {}, timeoutMs = 15000, retry = {
// exhausted one.
return fetchOnce(url, options, timeoutMs).catch((err) => {
if (retries < 1 || typeof shouldRetry !== 'function' || !shouldRetry(err)) throw err;
- return new Promise((resolve) => setTimeout(resolve, retryDelayMs))
+ return waitForRetry(retryDelayMs, options.signal)
.then(() => fetchWithTimeout(url, options, timeoutMs, { ...retry, retries: retries - 1 }));
});
}
+/**
+ * Pause before a retry while honoring the same caller cancellation signal as
+ * the fetch attempts. This keeps a caller-owned total deadline authoritative
+ * even when it expires between attempts.
+ */
+function waitForRetry(delayMs, signal) {
+ if (!signal) return new Promise((resolve) => setTimeout(resolve, delayMs));
+ if (signal.aborted) return Promise.reject(signal.reason || new DOMException('aborted', 'AbortError'));
+
+ return new Promise((resolve, reject) => {
+ const onAbort = () => {
+ clearTimeout(timeoutId);
+ reject(signal.reason || new DOMException('aborted', 'AbortError'));
+ };
+ const timeoutId = setTimeout(() => {
+ signal.removeEventListener('abort', onAbort);
+ resolve();
+ }, delayMs);
+ signal.addEventListener('abort', onAbort, { once: true });
+ });
+}
+
/**
* One attempt: fetch with an AbortController timeout, honoring a caller signal.
* @returns {Promise<Response>}
diff --git a/server/services/beeperClient.test.js b/server/services/beeperClient.test.js
index ffe77d0575..4477628fd1 100644
--- a/server/services/beeperClient.test.js
+++ b/server/services/beeperClient.test.js
@@ -428,6 +428,31 @@ describe('beeperClient', () => {
}
});
+ it('a total deadline cancels retry backoff before another read attempt starts', async () => {
+ vi.useFakeTimers();
+ try {
+ const fetchMock = vi.fn().mockRejectedValue(
+ Object.assign(new TypeError('fetch failed'), { cause: { code: 'ECONNRESET' } }),
+ );
+ vi.stubGlobal('fetch', fetchMock);
+
+ const request = listChatsPage({
+ baseUrl: DEFAULT_BASE_URL, token: 't', timeoutMs: 25,
+ });
+ const rejection = expect(request).rejects.toMatchObject({
+ code: 'NETWORK_ERROR',
+ context: { details: { timedOut: true } },
+ });
+ await vi.advanceTimersByTimeAsync(25);
+
+ await rejection;
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ expect(vi.getTimerCount()).toBe(0);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it.each([200, 500])('bounds a stalled JSON body after immediate HTTP %s headers', async (status) => {
vi.useFakeTimers();
try {