Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): apply the timer ceiling to every timer-backed knob

Yudhi Armyndharisb42e4272e572
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.ts
index 67f555ebd..a9967dd5e 100644
--- a/src/config/env.validation.spec.ts
+++ b/src/config/env.validation.spec.ts
@@ -658,8 +658,40 @@ describe('validateEnv', () => {
'MESSAGE_REAPER_INTERVAL_MS',
'WEBHOOK_RECONCILE_INTERVAL_MS',
'INGRESS_RECONCILE_INTERVAL_MS',
+ 'SESSION_TAKEOVER_SWEEP_MS',
+ 'SESSION_PROXY_TIMEOUT_MS',
+ 'MEDIA_DOWNLOAD_TIMEOUT_MS',
+ 'WEBHOOK_TIMEOUT',
+ 'RATE_LIMIT_SHORT_TTL',
+ 'RATE_LIMIT_MEDIUM_TTL',
+ 'RATE_LIMIT_LONG_TTL',
+ 'INGRESS_INSTANCE_TTL',
])('rejects a %s above the Node timer ceiling', key => {
expect(() => validateEnv({ [key]: '2147483648' })).toThrow(new RegExp(`${key} must not exceed 2147483647 ms`));
expect(() => validateEnv({ [key]: '2147483647' })).not.toThrow();
});
+
+ it('rejects a SESSION_LEASE_HEARTBEAT_MS above the Node timer ceiling even inside a longer lease', () => {
+ const lease = { SESSION_LEASE_TTL_MS: '5000000000' };
+ expect(() => validateEnv({ ...lease, SESSION_LEASE_HEARTBEAT_MS: '2147483648' })).toThrow(
+ /SESSION_LEASE_HEARTBEAT_MS must not exceed 2147483647 ms/,
+ );
+ expect(() => validateEnv({ ...lease, SESSION_LEASE_HEARTBEAT_MS: '2147483647' })).not.toThrow();
+ });
+
+ // Send verbs arm four times this budget, so its ceiling is a quarter of the timer ceiling.
+ it('rejects a PLUGIN_CAP_TIMEOUT_MS whose send-verb budget overflows the Node timer ceiling', () => {
+ expect(() => validateEnv({ PLUGIN_CAP_TIMEOUT_MS: '536870912' })).toThrow(
+ /PLUGIN_CAP_TIMEOUT_MS must not exceed 536870911 ms/,
+ );
+ expect(() => validateEnv({ PLUGIN_CAP_TIMEOUT_MS: '536870911' })).not.toThrow();
+ });
+
+ // A direct (queue-off) delivery doubles the delay on each retry, up to 2^3 for the maximum retryCount of 5.
+ it('rejects a WEBHOOK_RETRY_DELAY whose last direct-delivery backoff overflows the Node timer ceiling', () => {
+ expect(() => validateEnv({ WEBHOOK_RETRY_DELAY: '268435456' })).toThrow(
+ /WEBHOOK_RETRY_DELAY must not exceed 268435455 ms/,
+ );
+ expect(() => validateEnv({ WEBHOOK_RETRY_DELAY: '268435455' })).not.toThrow();
+ });
});
diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts
index 0a004d7c6..6d4c1a477 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -409,6 +409,15 @@ export function validateEnv(config: EnvConfig): EnvConfig {
['STATUS_ORPHAN_SWEEP_INTERVAL_MS', 'the orphan sweep reruns every millisecond'],
['S3_REPROBE_INTERVAL_MS', 'S3 is re-probed every millisecond while it is down'],
['STORAGE_EXPORT_TTL_MS', 'the export archive is deleted about 1 ms after it is written'],
+ ['SESSION_TAKEOVER_SWEEP_MS', 'the takeover sweep reruns every millisecond'],
+ ['SESSION_LEASE_HEARTBEAT_MS', 'the lease heartbeat renews every millisecond'],
+ ['SESSION_PROXY_TIMEOUT_MS', 'every proxied request times out after 1 ms'],
+ ['MEDIA_DOWNLOAD_TIMEOUT_MS', 'every media download fails'],
+ ['WEBHOOK_TIMEOUT', 'every webhook delivery fails'],
+ ['RATE_LIMIT_SHORT_TTL', 'each hit expires after 1 ms and that rate-limit tier never blocks'],
+ ['RATE_LIMIT_MEDIUM_TTL', 'each hit expires after 1 ms and that rate-limit tier never blocks'],
+ ['RATE_LIMIT_LONG_TTL', 'each hit expires after 1 ms and that rate-limit tier never blocks'],
+ ['INGRESS_INSTANCE_TTL', 'each hit expires after 1 ms and the ingress rate limits never block'],
// 0 still disables these three, so they carry only the ceiling, not the positive-only check.
['MESSAGE_REAPER_INTERVAL_MS', 'the pending message reaper reruns every millisecond'],
['WEBHOOK_RECONCILE_INTERVAL_MS', 'the webhook reconciler reruns every millisecond'],
@@ -423,6 +432,28 @@ export function validateEnv(config: EnvConfig): EnvConfig {
);
}
}
+ // Knobs armed at a multiple of their value get that fraction of the timer's ceiling. Send verbs arm
+ // four times PLUGIN_CAP_TIMEOUT_MS (SEND_CAP_TIMEOUT_FACTOR in plugin-worker-host.ts); a direct
+ // (queue-off) webhook delivery doubles WEBHOOK_RETRY_DELAY on each retry, up to 2^3 for the maximum
+ // retryCount of 5.
+ for (const [key, factor, multiple, consequence] of [
+ ['PLUGIN_CAP_TIMEOUT_MS', 4, 'send verbs wait four times this long', 'every plugin send times out'],
+ [
+ 'WEBHOOK_RETRY_DELAY',
+ 8,
+ 'the last direct-delivery retry waits eight times this long',
+ 'webhook retries fire back to back',
+ ],
+ ] as const) {
+ const raw = str(key);
+ const max = Math.floor(MAX_TIMER_MS / factor);
+ if (raw !== undefined && DECIMAL_INTEGER.test(raw) && Number(raw) > max) {
+ errors.push(
+ `${key} must not exceed ${max} ms (got "${raw}"): ${multiple}, ` +
+ `and Node's timers overflow above that and fire after 1 ms, so ${consequence}`,
+ );
+ }
+ }
// A heartbeat that does not fit inside the lease renews too late to matter: the claim lapses
// between ticks, peers adopt sessions from a perfectly healthy node, and nothing in the logs says