Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(telemetry): bound telemetry requests with an AbortSignal timeout
Rohit9545806a62c3
diff --git a/.changeset/bound-telemetry-request-timeout.md b/.changeset/bound-telemetry-request-timeout.mdnew file mode 100644index 000000000..e81074658--- /dev/null+++ b/.changeset/bound-telemetry-request-timeout.md@@ -0,0 +1,5 @@+---+'@composio/core': patch+---++Bound the best-effort telemetry requests with a timeout so a stalled telemetry endpoint cannot leave an SDK call pending indefinitely.diff --git a/ts/packages/core/src/services/telemetry/TelemetryService.ts b/ts/packages/core/src/services/telemetry/TelemetryService.tsindex 2b01ccb09..6a758003a 100644--- a/ts/packages/core/src/services/telemetry/TelemetryService.ts+++ b/ts/packages/core/src/services/telemetry/TelemetryService.ts@@ -2,8 +2,31 @@ import logger from '../../utils/logger';import { TelemetryMetricPayloadBody, TelemetryPayload } from './TelemetryService.types';const TELEMETRY_URL = 'https://telemetry.composio.dev/v1';+const TELEMETRY_TIMEOUT_MS = 3_000;export class TelemetryService {+ /**+ * POSTs a best-effort telemetry payload, bounding the request so a stalled+ * endpoint cannot leave it pending indefinitely.+ *+ * A hand-rolled timer rather than AbortSignal.timeout so it can be cleared the+ * moment the response lands: an uncleared timer is itself pending work, which on+ * workerd pins the request context open for the full timeout on every successful+ * send. Mirrors the bound applied to the npm version check.+ */+ private static postWithTimeout(url: string, payload: unknown) {+ const controller = new AbortController();+ const timer = setTimeout(() => controller.abort(), TELEMETRY_TIMEOUT_MS);+ return fetch(url, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ },+ body: JSON.stringify(payload),+ signal: controller.signal,+ }).finally(() => clearTimeout(timer));+ }+/*** Sends a metric to the Telemetry API.* @param payload - The payload to send to the Telemetry API.@@ -11,13 +34,7 @@ export class TelemetryService {*/static async sendMetric(payload: TelemetryMetricPayloadBody) {try {- const result = await fetch(`${TELEMETRY_URL}/metrics/invocations`, {- method: 'POST',- headers: {- 'Content-Type': 'application/json',- },- body: JSON.stringify(payload),- });+ const result = await this.postWithTimeout(`${TELEMETRY_URL}/metrics/invocations`, payload);return result;} catch (error) {// ignore error for now; telemetry failures should never affect SDK calls@@ -32,13 +49,7 @@ export class TelemetryService {*/static async sendErrorLog(payload: TelemetryPayload) {try {- const result = await fetch(`${TELEMETRY_URL}/errors`, {- method: 'POST',- headers: {- 'Content-Type': 'application/json',- },- body: JSON.stringify(payload),- });+ const result = await this.postWithTimeout(`${TELEMETRY_URL}/errors`, payload);return result;} catch (error) {// ignore error for now; telemetry failures should never affect SDK callsdiff --git a/ts/packages/core/test/telemetry/TelemetryService.test.ts b/ts/packages/core/test/telemetry/TelemetryService.test.tsindex 94925277c..b089ae638 100644--- a/ts/packages/core/test/telemetry/TelemetryService.test.ts+++ b/ts/packages/core/test/telemetry/TelemetryService.test.ts@@ -437,6 +437,80 @@ describe('TelemetryTransport', () => {});});+describe('TelemetryService network bounding', () => {+ afterEach(() => {+ vi.useRealTimers();+ vi.restoreAllMocks();+ vi.unstubAllGlobals();+ });++ it('bounds sendMetric with an abort signal and clears the timer once it responds', async () => {+ vi.useFakeTimers();+ const fetchMock = vi.fn<typeof fetch>(async () => Response.json({ ok: true }));+ vi.stubGlobal('fetch', fetchMock);++ await TelemetryService.sendMetric([createPayload()]);++ expect(fetchMock).toHaveBeenCalledWith(+ 'https://telemetry.composio.dev/v1/metrics/invocations',+ expect.objectContaining({ method: 'POST', signal: expect.any(AbortSignal) })+ );+ // A timer left pending after the fetch resolved keeps a workerd request context+ // open for the full timeout on every successful send.+ expect(vi.getTimerCount()).toBe(0);+ });++ it('swallows a sendMetric timeout when the endpoint never responds', async () => {+ vi.useFakeTimers();+ const fetchMock = vi.fn<typeof fetch>(+ (_input, init) =>+ new Promise<Response>((_resolve, reject) => {+ init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), {+ once: true,+ });+ })+ );+ vi.stubGlobal('fetch', fetchMock);++ const send = TelemetryService.sendMetric([createPayload()]);+ await vi.advanceTimersByTimeAsync(3_000);++ await expect(send).resolves.toBeUndefined();+ });++ it('bounds sendErrorLog with an abort signal and clears the timer once it responds', async () => {+ vi.useFakeTimers();+ const fetchMock = vi.fn<typeof fetch>(async () => Response.json({ ok: true }));+ vi.stubGlobal('fetch', fetchMock);++ await TelemetryService.sendErrorLog(createPayload());++ expect(fetchMock).toHaveBeenCalledWith(+ 'https://telemetry.composio.dev/v1/errors',+ expect.objectContaining({ method: 'POST', signal: expect.any(AbortSignal) })+ );+ expect(vi.getTimerCount()).toBe(0);+ });++ it('swallows a sendErrorLog timeout when the endpoint never responds', async () => {+ vi.useFakeTimers();+ const fetchMock = vi.fn<typeof fetch>(+ (_input, init) =>+ new Promise<Response>((_resolve, reject) => {+ init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), {+ once: true,+ });+ })+ );+ vi.stubGlobal('fetch', fetchMock);++ const send = TelemetryService.sendErrorLog(createPayload());+ await vi.advanceTimersByTimeAsync(3_000);++ await expect(send).resolves.toBeUndefined();+ });+});+beforeAll(() => {// Save the original NODE_ENVprocess.env._ORIGINAL_NODE_ENV = process.env.NODE_ENV;