Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/loopover-miner/lib/orb-export.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export function latestClosedAt(batch: OrbExportRow[]): string | null;

export const DEFAULT_AMS_COLLECTOR_URL: string;

export const DEFAULT_ORB_EXPORT_TIMEOUT_MS: number;

export function resolveAmsCollectorUrl(env?: Record<string, string | undefined>): string;

export function sendAmsExportBatch(options: {
Expand All @@ -61,6 +63,7 @@ export function sendAmsExportBatch(options: {
collectorUrl?: string;
collectorToken?: string | undefined;
fetchFn?: typeof fetch;
timeoutMs?: number;
}): Promise<AmsExportSendResult>;

export type ParsedOrbExportArgs = { json: boolean; enable: boolean; send: boolean; dryRun: boolean } | { error: string };
Expand Down
14 changes: 13 additions & 1 deletion packages/loopover-miner/lib/orb-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,18 @@ export function resolveAmsCollectorUrl(env = process.env) {
* response, `{ sent: 0, error }` otherwise — a network failure or non-2xx never throws, matching this module's
* fail-open posture (a telemetry hiccup must never break the miner's real work).
*/
export async function sendAmsExportBatch({ batch, secret, collectorUrl = resolveAmsCollectorUrl(), collectorToken, fetchFn = fetch }) {
// Bound a single AMS-collector POST so a hung/black-holed collector can't stall the export indefinitely (#7237).
// 10s matches this package's other default request timeouts (live-issue-snapshot.js / opportunity-fanout.js).
export const DEFAULT_ORB_EXPORT_TIMEOUT_MS = 10_000;

export async function sendAmsExportBatch({
batch,
secret,
collectorUrl = resolveAmsCollectorUrl(),
collectorToken,
fetchFn = fetch,
timeoutMs = DEFAULT_ORB_EXPORT_TIMEOUT_MS,
}) {
if (!Array.isArray(batch) || batch.length === 0) return { sent: 0 };
const instanceId = amsInstanceId(secret);
const body = JSON.stringify({ instanceId, events: batch });
Expand All @@ -192,6 +203,7 @@ export async function sendAmsExportBatch({ batch, secret, collectorUrl = resolve
...(collectorToken ? { authorization: `Bearer ${collectorToken}` } : {}),
},
body,
signal: AbortSignal.timeout(timeoutMs),
});
if (!res.ok) return { sent: 0, error: `http_${res.status}` };
} catch (error) {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/miner-orb-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
ORB_EXPORT_ENABLED_BY_DEFAULT,
DEFAULT_AMS_COLLECTOR_URL,
DEFAULT_ORB_EXPORT_TIMEOUT_MS,
amsInstanceId,
buildAnonymizedOrbBatch,
collectOrbExportBatch,
Expand Down Expand Up @@ -231,4 +232,25 @@ describe("sendAmsExportBatch (#5681)", () => {
expect(result.sent).toBe(0);
expect(result.error).toBeTruthy();
});

it("bounds the request with an AbortSignal.timeout (default 10s), honoring an explicit timeoutMs (#7237)", async () => {
expect(DEFAULT_ORB_EXPORT_TIMEOUT_MS).toBe(10_000);

const fetchFn = vi.fn().mockResolvedValue({ ok: true, status: 200 });
await sendAmsExportBatch({ batch, secret: "s".repeat(64), fetchFn }); // no timeoutMs -> default applies
const [, init] = fetchFn.mock.calls[0] as [string, RequestInit];
expect(init.signal).toBeInstanceOf(AbortSignal);

fetchFn.mockClear();
await sendAmsExportBatch({ batch, secret: "s".repeat(64), fetchFn, timeoutMs: 250 }); // explicit override
const [, override] = fetchFn.mock.calls[0] as [string, RequestInit];
expect(override.signal).toBeInstanceOf(AbortSignal);
});

it("catches an aborted (timed-out) request via the existing catch, without throwing (#7237)", async () => {
const fetchFn = vi.fn().mockRejectedValue(new DOMException("The operation was aborted.", "TimeoutError"));
const result = await sendAmsExportBatch({ batch, secret: "s".repeat(64), fetchFn });
expect(result.sent).toBe(0);
expect(result.error).toContain("aborted");
});
});