Skip to content

fix(node): Flush telemetry before Vercel functions are suspended - #24910

Open
sergical wants to merge 1 commit into
developfrom
fix/vercel-node-waituntil-span-flush
Open

sergical wants to merge 1 commit into
developfrom
fix/vercel-node-waituntil-span-flush

Conversation

@sergical

@sergical sergical commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

On Vercel, a function can be suspended as soon as it sends the response. Telemetry that is still buffered (spans, logs, metrics, errors) then arrives late or never.

When a response finishes, @sentry/node now uses waitUntil to keep the function alive. It waits until the response closes and the request's root span ends (2 s max), then flushes. It works with or without tracing.

Some frameworks end the root span shortly after the response closes, so the flush waits for it.

Tested with unit tests and an integration test that fails without the change, and confirmed on a Vercel deployment.

Closes #24909. Refs #23087.

Created with Claude Code

@sergical sergical self-assigned this Sep 30, 2026
@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.51 kB - -
@sentry/browser - with treeshaking flags 27.68 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 27.58 kB - -
@sentry/browser (incl. Tracing) 51.46 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 51.47 kB - -
@sentry/browser (incl. Tracing, Profiling) 54.47 kB - -
@sentry/browser (incl. Tracing, Replay) 91.05 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.03 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 95.73 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 108.72 kB - -
@sentry/browser (incl. Feedback) 47.04 kB - -
@sentry/browser (incl. sendFeedback) 34.57 kB - -
@sentry/browser (incl. FeedbackAsync) 39.69 kB - -
@sentry/browser (incl. Metrics) 30.53 kB - -
@sentry/browser (incl. Logs) 30.82 kB - -
@sentry/browser (incl. Metrics & Logs) 31.48 kB - -
@sentry/react 31.36 kB - -
@sentry/react (incl. Tracing) 53.82 kB - -
@sentry/vue 37.51 kB - -
@sentry/vue (incl. Tracing) 54.36 kB - -
@sentry/svelte 29.54 kB - -
@sentry/remix (Remix 3 client bundle) 55.75 kB - -
CDN Bundle 31.22 kB - -
CDN Bundle (incl. Tracing) 51.99 kB - -
CDN Bundle (incl. Logs, Metrics) 33.46 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 53.94 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 74.2 kB - -
CDN Bundle (incl. Tracing, Replay) 89.56 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 91.54 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 95.73 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 97.73 kB - -
CDN Bundle - uncompressed 92.14 kB - -
CDN Bundle (incl. Tracing) - uncompressed 154.53 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 98.71 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 160.48 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 228.28 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 274.26 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 280.2 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 287.96 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 293.89 kB - -
@sentry/nextjs (client) 56.33 kB - -
@sentry/sveltekit (client) 51.88 kB - -
@sentry/core/server 39.99 kB - -
@sentry/core/browser 13.63 kB - -
@sentry/node 144.67 kB +0.14% +197 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.08 kB - -
@sentry/node - without tracing 93.22 kB +0.21% +194 B 🔺
@sentry/node - without channel injection 122.93 kB +0.16% +185 B 🔺
@sentry/aws-serverless 101.5 kB +0.18% +177 B 🔺
@sentry/cloudflare (withSentry) - minified 207.1 kB - -
@sentry/cloudflare (withSentry) 515.31 kB - -

View base workflow run

@sergical
sergical marked this pull request as ready for review September 30, 2026 18:32
Comment thread packages/core/src/integrations/spanStreaming.ts
On Vercel, a function can be suspended as soon as it sends the response, so
buffered telemetry arrives late or never. When a response finishes, keep the
function alive with `waitUntil` until the response closes and the request's
root span ends (2 s max), then flush.

Closes #24909

Co-authored-by: Claude <claude@anthropic.com>
@sergical
sergical force-pushed the fix/vercel-node-waituntil-span-flush branch from 05da2d0 to 50ac024 Compare October 1, 2026 19:27
@sergical
sergical requested a review from a team as a code owner October 1, 2026 19:27
@sergical sergical changed the title fix(core): Keep Vercel Node.js instances alive until streamed spans are sent fix(node): Flush telemetry before Vercel functions are suspended Oct 1, 2026
@sergical
sergical requested review from isaacs and mydea and removed request for a team October 1, 2026 19:27
Comment on lines +44 to +48
requestContext.waitUntil(
new Promise<void>(resolve => response.once('close', resolve))
.then(() => rootSpan && waitForSpanEnd(client, rootSpan, ROOT_SPAN_END_TIMEOUT_MS))
.then(() => client.flush(2000)),
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The code waits for response.once('close', ...) without a timeout, which can cause the Vercel function to hang if a client uses HTTP/1.1 keep-alive.
Severity: MEDIUM

Suggested Fix

Wrap the promise that waits for the response.close event with a timeout mechanism, for example by using Promise.race with a setTimeout. This will prevent the waitUntil task from hanging if the close event is not emitted promptly.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/node/src/sdk/vercel.ts#L44-L48

Potential issue: In `setupVercelKeepAlive`, the code waits for the `response.close`
event to resolve a promise within a `waitUntil` task. However, if an HTTP/1.1 client
connects to a Vercel function without sending a `Connection: close` header, the
connection may be kept alive. This would delay the `response.close` event from firing,
causing the `waitUntil` task to hang until a socket timeout. While other timeouts on
span waiting and flushing prevent an indefinite hang, the function's completion is
unnecessarily delayed, impacting performance.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 50ac024. Configure here.

unsubscribe();
resolve();
};
const timer = setTimeout(done, timeout);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep-alive timer is not unref'd

Low Severity

Flagged because the review guidelines require setTimeout in @sentry/node to call unref() or safeUnref(). The keep-alive timeout in waitForSpanEnd does neither, so a recording root span can pin a Node process for up to two seconds after the response closes when VERCEL is set.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 50ac024. Configure here.

const { response } = message as { response?: HttpServerResponse };
const requestContext = (
GLOBAL_OBJ as unknown as Record<symbol, { get?(): VercelRequestContext | undefined } | undefined>
)[Symbol.for('@vercel/request-context')]?.get?.();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Untyped double-cast lacks comment

Low Severity

Flagged because the review guidelines require a comment on new as unknown as casts in SDK source. The @vercel/request-context lookup uses that double-cast with no explanation, even though nearby vercelWaitUntil already types the same global.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 50ac024. Configure here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Span streaming drops the root span on Vercel Node.js runtime (Fluid compute)

1 participant