Skip to content

fix(core): do not unnecessarily defer offline transport send - #24027

Merged
isaacs merged 3 commits into
developfrom
isaacs/fix-24005
Sep 4, 2026
Merged

fix(core): do not unnecessarily defer offline transport send#24027
isaacs merged 3 commits into
developfrom
isaacs/fix-24005

Conversation

@isaacs

@isaacs isaacs commented Sep 3, 2026

Copy link
Copy Markdown
Member

makeOfflineTransport awaited the shouldSend result unconditionally. The option type allows a plain boolean, and awaiting one still costs a microtask tick, so a synchronous callback pushed the call to transport.send() into the next turn.

Only await when the callback returns a thenable. Hosts such as Douyin mini-games stop JS execution when the app goes to the background, so that one tick was enough to lose the event: the envelope had not reached the transport, and the offline store only gets written in the catch branch, which never runs when shouldSend returns true.

The same treatment is applied to options.shouldStore / shouldQueue method in the same file.

Behavior is unchanged for callbacks that return a promise.

fixes #24005
fixes JS-3560

`makeOfflineTransport` awaited the `shouldSend` result unconditionally.
The option type allows a plain boolean, and awaiting one still costs a
microtask tick, so a synchronous callback pushed the call to
`transport.send()` into the next turn.

Only await when the callback returns a thenable. Hosts such as Douyin
mini-games stop JS execution when the app goes to the background, so
that one tick was enough to lose the event: the envelope had not
reached the transport, and the offline store only gets written in the
`catch` branch, which never runs when `shouldSend` returns true.

Behavior is unchanged for callbacks that return a promise.

fixes #24005
fixes JS-3560

Co-Authored-By: yaoxp <viq_xp@126.com>
@linear-code

linear-code Bot commented Sep 3, 2026

Copy link
Copy Markdown

JS-3560

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 28.7 kB - -
@sentry/browser - with treeshaking flags 27.01 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 26.9 kB - -
@sentry/browser (incl. Tracing) 49.08 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 49.08 kB - -
@sentry/browser (incl. Tracing, Profiling) 52 kB - -
@sentry/browser (incl. Tracing, Replay) 88.63 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 77.86 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 93.31 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 106.26 kB - -
@sentry/browser (incl. Feedback) 46.19 kB - -
@sentry/browser (incl. sendFeedback) 33.76 kB - -
@sentry/browser (incl. FeedbackAsync) 38.86 kB - -
@sentry/browser (incl. Metrics) 29.67 kB - -
@sentry/browser (incl. Logs) 29.95 kB - -
@sentry/browser (incl. Metrics & Logs) 30.6 kB - -
@sentry/react 30.46 kB - -
@sentry/react (incl. Tracing) 51.29 kB - -
@sentry/vue 35.93 kB - -
@sentry/vue (incl. Tracing) 51.35 kB - -
@sentry/svelte 28.72 kB - -
CDN Bundle 30.44 kB - -
CDN Bundle (incl. Tracing) 49.61 kB - -
CDN Bundle (incl. Logs, Metrics) 32.67 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 51.54 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 73.33 kB - -
CDN Bundle (incl. Tracing, Replay) 87.17 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 89.03 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 93.1 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 95.04 kB - -
CDN Bundle - uncompressed 90.17 kB - -
CDN Bundle (incl. Tracing) - uncompressed 147.84 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 96.55 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 153.62 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 225.86 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 267.48 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 273.25 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 281.18 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 286.94 kB - -
@sentry/nextjs (client) 53.9 kB -0.01% -1 B 🔽
@sentry/sveltekit (client) 49.52 kB - -
@sentry/core/server 40.97 kB - -
@sentry/core/browser 13.53 kB - -
@sentry/node 124.81 kB +0.02% +19 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 81.51 kB - -
@sentry/node - without tracing 88.51 kB +0.03% +22 B 🔺
@sentry/node - without channel injection 104.12 kB +0.02% +19 B 🔺
@sentry/aws-serverless 96.88 kB +0.02% +16 B 🔺
@sentry/cloudflare (withSentry) - minified 201.59 kB - -
@sentry/cloudflare (withSentry) 501.43 kB - -

View base workflow run

const decision = options.shouldSend(envelope);
// avoid extra microtask tick, as some hosts stop JS execution
// when the app goes to the background.
const shouldSend = isThenable(decision) ? await decision : decision;

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.

m: I think we have a similar issue with shouldQueue/options.shouldStore below. might be worth checking and adjusting while we're at it.

@isaacs isaacs Sep 4, 2026

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.

Good catch, yes, applied that as well.

I wonder if there's a way to make our linter catch this general pattern, where we unconditionally await something that could be a non-promise. 🤔 It could be annoying to be nagged to isThenable(x) everywhere, but I'm curious now how common this is. It's basically always going to be a correctness and/or performance improvement to avoid the await for sync values.


UPDATE: ok, threw a clanker at this question, and I'm now convinced it's not worth doing, so I'm ditching the idea.

  • It's not something we can easily do with oxlint, or even scripting against the tsgo compiler we use, since none of the type checking is easily pluggable.
  • eslint rules are syntax only, not pluggable type-aware rules.
  • we CAN do it with a standalone script that uses the JS typescript lib, which I had it do, and it found 26 cases where we have a return value that's awaited, and might not contain a then function. However, apart from these two being fixed in this PR, the others are all build-time or startup paths where it doesn't matter as much.
  • isThenable(x) ? await x : x everywhere would increase bundle size for little gain, and it is uglier to human eyes.
  • We can just be mindful of this when operating in the send() path, which we'll probably do just by copying the pattern that's fixed here anyway, so there's little added benefit.

@isaacs
isaacs enabled auto-merge (squash) September 4, 2026 15:46
@isaacs
isaacs merged commit 57cbeb6 into develop Sep 4, 2026
303 checks passed
@isaacs
isaacs deleted the isaacs/fix-24005 branch September 4, 2026 16:20
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.

makeOfflineTransport awaits a synchronous shouldSend, delaying transport.send() by a microtask

3 participants