Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(security): keep a run's subrequest limit from pausing a platform for everyone

spacesdrivefb109c1605b8
diff --git a/backend/lib/circuitBreaker.ts b/backend/lib/circuitBreaker.ts
index bb1f433..2d953eb 100644
--- a/backend/lib/circuitBreaker.ts
+++ b/backend/lib/circuitBreaker.ts
@@ -15,10 +15,19 @@ export interface CircuitBreaker {
run<T>(call: () => Promise<T>, signal?: AbortSignal): Promise<T>;
}
+// the Workers runtime refuses a fetch past the invocation's subrequest allowance before anything leaves, so the error
+// says this run spent its share, nothing about the platform; one person's busy run must not pause everyone's calls
+const SUBREQUEST_LIMIT = /too many subrequests/i;
+
+export function isSubrequestLimit(err: unknown): boolean {
+ const cause = (err as { cause?: unknown } | null)?.cause;
+ return [err, cause].some(value => value instanceof Error && SUBREQUEST_LIMIT.test(value.message));
+}
+
// a timeout, an unreachable host or a 5xx says the platform is down; a 4xx is about the request, and a call the caller
// cancelled says nothing about the platform
export function isOutage(err: unknown, signal?: AbortSignal, httpStatus = true): boolean {
- if (signal?.aborted) return false;
+ if (signal?.aborted || isSubrequestLimit(err)) return false;
if (err instanceof UpstreamTimeoutError) return true;
if (!(err instanceof UpstreamError)) return false;
return err.reason === 'unreachable' || (httpStatus && (err.upstreamStatus ?? 0) >= 500);
@@ -47,7 +56,7 @@ export function createCircuitBreaker(platform: string, { httpStatus = true, now
if (isOutage(err, signal, httpStatus)) {
failures += 1;
if (failures >= FAILURE_THRESHOLD) openUntil = now() + OPEN_MS;
- } else if (!signal?.aborted) {
+ } else if (!signal?.aborted && !isSubrequestLimit(err)) {
// the platform answered, even if with an error, so it is up
failures = 0;
}
diff --git a/backend/services/instagram/instagramMessaging.ts b/backend/services/instagram/instagramMessaging.ts
index 9c7a7d9..d22d8a9 100644
--- a/backend/services/instagram/instagramMessaging.ts
+++ b/backend/services/instagram/instagramMessaging.ts
@@ -1,14 +1,12 @@
import { buildGraphUrl, igFetch, igPost } from './instagramApi.ts';
import { UpstreamError, isAbortError } from '../../lib/errors.ts';
import { redactSecrets } from '../../lib/redact.ts';
+import { isSubrequestLimit } from '../../lib/circuitBreaker.ts';
import { FOLLOW_CHECK_TIMEOUT_MS, SEND_TIMEOUT_MS, classifySendError } from '../../lib/automationDelivery.ts';
import type { FollowCheck, SendResult } from '../../lib/automationDelivery.ts';
import type { ConversationMessage } from '../../lib/automationReview.ts';
import type { InstagramApiErrorBody } from './instagramApi.ts';
-// the Workers runtime refuses a fetch past the run's subrequest allowance before anything leaves
-const SUBREQUEST_LIMIT = /too many subrequests/i;
-
// a private reply names the comment; a message inside the 24 hour window names the person
export type MessageRecipient = { comment_id: string } | { id: string };
@@ -50,7 +48,7 @@ export async function postOnce<T extends InstagramApiErrorBody>(endpoint: string
signal: AbortSignal.timeout(SEND_TIMEOUT_MS),
});
} catch (err) {
- if (SUBREQUEST_LIMIT.test((err as Error).message ?? '')) return { kind: 'not_sent', message: 'this run reached its request limit' };
+ if (isSubrequestLimit(err)) return { kind: 'not_sent', message: 'this run reached its request limit' };
return { kind: 'ambiguous', message: isAbortError(err) ? 'the request timed out' : 'the connection failed' };
}
diff --git a/backend/tests/lib/circuitBreaker.test.ts b/backend/tests/lib/circuitBreaker.test.ts
index 0170f85..b3391c7 100644
--- a/backend/tests/lib/circuitBreaker.test.ts
+++ b/backend/tests/lib/circuitBreaker.test.ts
@@ -1,6 +1,6 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
-import { createCircuitBreaker, isOutage } from '../../lib/circuitBreaker.ts';
+import { createCircuitBreaker, isOutage, isSubrequestLimit } from '../../lib/circuitBreaker.ts';
import { NotFoundError, UpstreamError, UpstreamTimeoutError } from '../../lib/errors.ts';
function clock(start = 0) {
@@ -100,3 +100,16 @@ test('for Instagram a Graph error code above 500, such as 506 for a duplicate po
for (let i = 0; i < 6; i++) await assert.rejects(breaker.run(() => Promise.reject(new UpstreamError('Duplicate posting', { status: 506 }))));
assert.equal(await breaker.run(up), 'ok');
});
+
+test('running out of this invocation\'s subrequests never pauses the platform for everyone, nor clears a real outage count', async () => {
+ const breaker = createCircuitBreaker('YouTube', { now: clock().now });
+ const spent = () => Promise.reject(new UpstreamError('YouTube could not be reached (Too many subrequests.)', { reason: 'unreachable', cause: new Error('Too many subrequests.') }));
+ for (let i = 0; i < 10; i++) await assert.rejects(breaker.run(spent));
+ assert.equal(await breaker.run(up), 'ok');
+ await failTimes(breaker, 4);
+ await assert.rejects(breaker.run(spent));
+ await failTimes(breaker, 1);
+ await assert.rejects(breaker.run(up), /paused for a moment/);
+ assert.equal(isSubrequestLimit(new Error('fetch failed')), false);
+ assert.equal(isOutage(new UpstreamError('x (Too many subrequests.)', { reason: 'unreachable' })), false);
+});