Skip to content

fix(cloudflare): Wait for span links to be set#21167

Open
JPeer264 wants to merge 3 commits into
developfrom
jp/wait-for-alarm
Open

fix(cloudflare): Wait for span links to be set#21167
JPeer264 wants to merge 3 commits into
developfrom
jp/wait-for-alarm

Conversation

@JPeer264
Copy link
Copy Markdown
Member

This came up in #21101 where the link was not set in time and the tests failed. With this PR we are waiting for the links to be actually set, before the rootspan ends. Retrieving the stored span shouldn't take too long - and the work was also before already executed during the response.

@JPeer264 JPeer264 requested a review from a team as a code owner May 26, 2026 11:12
@JPeer264 JPeer264 requested review from andreiborza and mydea and removed request for a team May 26, 2026 11:12
@JPeer264 JPeer264 self-assigned this May 26, 2026
Comment thread packages/cloudflare/src/wrapMethodWithSentry.ts Outdated
Copy link
Copy Markdown
Member

@isaacs isaacs left a comment

Choose a reason for hiding this comment

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

I think the clanker's objection is valid, unless it's impossible to have a link promise and a non-then-able result. Might be good to add a test showing that case, if possible.

If I'm misreading it, happy to update to 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If I understand the clanker's objection, seems like there should be a waitIUntil here to handle the linkPromise if the result is not thenable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

whoopsie. That was actually a good catch. I actually wanted to go away from waitUntil, as with that it is a little unreliable if a trace link is created or not.

I'll change the logic a little so we don't have duplicated code for sync and async returns.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok I had to change it to a sync KV update to make it work for sync alarms. I also didn't want to return a promise for sync alarms, as this would change the users method to be a promise.

I now added also a sync alarm test as proof.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

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 1 potential issue.

Fix All in Cursor

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

Reviewed by Cursor Bugbot for commit 3d9cf8c. Configure here.

return undefined;
}
): StoredSpanContext | undefined {
return originalStorage.kv.get<StoredSpanContext>(getTraceLinkKey(methodName));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

getStoredSpanContext lost error handling, may crash user code

Medium Severity

getStoredSpanContext had its try-catch removed in this refactor, but storeSpanContext still has one. If originalStorage.kv is undefined (e.g., on legacy KV-backed Durable Objects where the sync KV API isn't available), calling originalStorage.kv.get(...) throws a TypeError. The caller in wrapMethodWithSentry.ts invokes getStoredSpanContext inside the startSpan callback but outside the try-catch block that wraps the user's handler, so the error propagates through handleCallbackErrors which re-throws, crashing the user's alarm handler with a Sentry-internal error.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 3d9cf8c. Configure here.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 27, 2026

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.27 kB - -
@sentry/browser - with treeshaking flags 25.69 kB - -
@sentry/browser (incl. Tracing) 45.25 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.49 kB - -
@sentry/browser (incl. Tracing, Profiling) 50.23 kB - -
@sentry/browser (incl. Tracing, Replay) 84.86 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.36 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 89.57 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 102.18 kB - -
@sentry/browser (incl. Feedback) 44.46 kB - -
@sentry/browser (incl. sendFeedback) 32.09 kB - -
@sentry/browser (incl. FeedbackAsync) 37.21 kB - -
@sentry/browser (incl. Metrics) 28.37 kB - -
@sentry/browser (incl. Logs) 28.59 kB - -
@sentry/browser (incl. Metrics & Logs) 29.29 kB - -
@sentry/react 29.01 kB - -
@sentry/react (incl. Tracing) 47.49 kB - -
@sentry/vue 32.19 kB - -
@sentry/vue (incl. Tracing) 47.12 kB - -
@sentry/svelte 27.3 kB - -
CDN Bundle 29.68 kB - -
CDN Bundle (incl. Tracing) 47.78 kB - -
CDN Bundle (incl. Logs, Metrics) 31.17 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.03 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.48 kB - -
CDN Bundle (incl. Tracing, Replay) 85.28 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.43 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.15 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.31 kB - -
CDN Bundle - uncompressed 87.69 kB - -
CDN Bundle (incl. Tracing) - uncompressed 144.15 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 92.18 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 147.91 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 216.91 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 262.93 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 266.67 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 276.62 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 280.36 kB - -
@sentry/nextjs (client) 49.98 kB - -
@sentry/sveltekit (client) 45.73 kB - -
@sentry/core/server 76.42 kB - -
@sentry/core/browser 63.17 kB - -
@sentry/node-core 62.26 kB +0.01% +1 B 🔺
@sentry/node 130.74 kB +0.01% +3 B 🔺
@sentry/node - without tracing 74.7 kB +0.01% +1 B 🔺
@sentry/aws-serverless 86.91 kB +0.01% +1 B 🔺
@sentry/cloudflare (withSentry) - minified 172.12 kB -0.18% -295 B 🔽
@sentry/cloudflare (withSentry) 429.49 kB -0.28% -1.19 kB 🔽

View base workflow run

@JPeer264 JPeer264 requested a review from isaacs May 27, 2026 09:49
return res;
};

const onRejected = (e: unknown) => {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

note: I outsourced that since it was used 4 times

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.

2 participants