Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(worker): bound Mixpanel import requests with a timeout (#15968)

chelsealong86703f5a62e5
diff --git a/worker/src/__tests__/mixpanelExportVolume.test.ts b/worker/src/__tests__/mixpanelExportVolume.test.ts
index 1269ded69c..d804de7506 100644
--- a/worker/src/__tests__/mixpanelExportVolume.test.ts
+++ b/worker/src/__tests__/mixpanelExportVolume.test.ts
@@ -12,7 +12,40 @@ describe("MixpanelClient export volume", () => {
vi.stubGlobal("fetch", fetchMock);
});
- afterEach(() => vi.unstubAllGlobals());
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.useRealTimers();
+ });
+
+ it("aborts a hung sendBatch instead of leaving flush() pending indefinitely", async () => {
+ vi.useFakeTimers();
+
+ // Simulate an unresponsive Mixpanel endpoint: the request never settles
+ // on its own, only when the abort signal fires (mirrors real fetch).
+ fetchMock.mockImplementation(
+ (_url: string, init: { signal: AbortSignal }) =>
+ new Promise((_resolve, reject) => {
+ init.signal.addEventListener("abort", () => {
+ const abortError = new Error("The operation was aborted");
+ abortError.name = "AbortError";
+ reject(abortError);
+ });
+ }),
+ );
+
+ const client = new MixpanelClient({ projectToken: "t", region: "api" });
+ client.addEvent({
+ event: "trace",
+ properties: { token: "t", distinct_id: "1", $insert_id: 1 },
+ } as unknown as MixpanelEvent);
+
+ const flushPromise = client.flush();
+ const rejection = expect(flushPromise).rejects.toThrow(/timed out/i);
+
+ await vi.advanceTimersByTimeAsync(30_000);
+
+ await rejection;
+ });
it("accumulates gzipped on-wire bytes across sendBatch chunks", async () => {
const client = new MixpanelClient({ projectToken: "t", region: "api" });
diff --git a/worker/src/env.ts b/worker/src/env.ts
index 530796ce12..b6be7c67ce 100644
--- a/worker/src/env.ts
+++ b/worker/src/env.ts
@@ -148,6 +148,9 @@ const EnvSchema = z.object({
// Delay (ms) inserted after each Mixpanel flush to throttle analytics exports
// and avoid overwhelming the target instance (see issue #12786).
LANGFUSE_MIXPANEL_FLUSH_DELAY_MS: z.coerce.number().min(0).default(100),
+ // Timeout (ms) for each Mixpanel import request, so an unresponsive endpoint
+ // cannot hold the integration job indefinitely (see issue #15958).
+ LANGFUSE_MIXPANEL_TIMEOUT_MS: z.coerce.number().positive().default(30000),
// Delay (ms) after each PostHog flush. Together with 1,000-event flushes,
// this bounds the export rate for fast-acknowledging target instances.
LANGFUSE_POSTHOG_FLUSH_DELAY_MS: z.coerce.number().min(0).default(100),
diff --git a/worker/src/features/mixpanel/mixpanelClient.ts b/worker/src/features/mixpanel/mixpanelClient.ts
index bc90ae0d17..938c216e7b 100644
--- a/worker/src/features/mixpanel/mixpanelClient.ts
+++ b/worker/src/features/mixpanel/mixpanelClient.ts
@@ -1,5 +1,6 @@
import { logger } from "@langfuse/shared/src/server";
import { gzipSync } from "zlib";
+import { env } from "../../env";
import type { MixpanelEvent } from "./transformers";
type MixpanelClientConfig = {
@@ -73,6 +74,13 @@ export class MixpanelClient {
// Create Basic Auth header (token as username, empty password)
const authHeader = `Basic ${Buffer.from(`${this.config.projectToken}:`).toString("base64")}`;
+ // Bound the request so an unresponsive endpoint cannot hold the flush
+ // (and the worker job behind it) indefinitely.
+ const abortController = new AbortController();
+ const timeoutId = setTimeout(() => {
+ abortController.abort();
+ }, env.LANGFUSE_MIXPANEL_TIMEOUT_MS);
+
try {
const response = await fetch(url, {
method: "POST",
@@ -82,6 +90,7 @@ export class MixpanelClient {
Authorization: authHeader,
},
body: compressedBody as unknown as BodyInit,
+ signal: abortController.signal,
});
if (!response.ok) {
@@ -129,8 +138,17 @@ export class MixpanelClient {
result,
});
} catch (error) {
+ if (error instanceof Error && error.name === "AbortError") {
+ const timeoutError = new Error(
+ `Mixpanel request timed out after ${env.LANGFUSE_MIXPANEL_TIMEOUT_MS}ms`,
+ );
+ logger.error("Error sending batch to Mixpanel", timeoutError);
+ throw timeoutError;
+ }
logger.error("Error sending batch to Mixpanel", error);
throw error;
+ } finally {
+ clearTimeout(timeoutId);
}
}