fix(core): Measure callback-style native module calls until completion - #6561
Open
alwx wants to merge 5 commits into
Open
fix(core): Measure callback-style native module calls until completion#6561alwx wants to merge 5 commits into
alwx wants to merge 5 commits into
Conversation
`wrapTurboModule` only knew two completion signals: a plain return and a thenable return. Bridge methods that report completion through success/failure callbacks return `undefined`, so they were closed on return and recorded as sync calls with a near-zero duration. RN's `genMethod` confirms the shape — for `type: 'async'` the generated wrapper returns nothing and hands the trailing callbacks to `enqueueNativeCall`. The same source fixes the argument convention: last argument is the success callback, second-to-last the failure one, and a non-function may never follow a function. Only the Old Architecture bridge guarantees that, so failure callbacks are counted as errors there and left unflagged on the New Architecture rather than guessing and corrupting `errorCount`. The crash-attribution frame is still popped synchronously. Holding it until a callback that may never fire would risk blaming this module for an unrelated later native crash — a regression on the very feature the frame exists for. Only the timing record is deferred. Records are bounded without timers: a cap plus an amortised age sweep on insert. A call closed out that way is still counted, with a zero duration — dropping it would hide the method from the aggregate entirely, which is worse than the ~0ms this fixes. The same clamp covers a callback firing implausibly late, which on the New Architecture may well be a long-lived subscription handler rather than a completion callback. The callback machinery lives in its own module because `wrapTurboModule.ts` would otherwise exceed oxlint's `max-lines`. Fixes #6542
Contributor
Semver Impact of This PR⚪ None (no version bump detected) 📋 Changelog PreviewThis is how your changes will appear in the changelog.
🤖 This preview updates automatically when you update the PR. |
…inline A method that invokes its trailing callback inline and then returns a thenable produced two aggregate rows for one invocation: the callback closed the record as `sync`, and the promise handlers recorded it again as `async`. The throw path already consulted `abandon()`'s return value; the thenable path discarded it. Whichever completion signal lands first now wins. Reported by Warden on #6561.
alwx
marked this pull request as ready for review
August 5, 2026 09:14
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 8ef7342. Configure here.
Collaborator
|
All good! LGTM after dealing with the AI review |
📲 Install BuildsAndroid
|
Contributor
iOS (legacy) Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 3b6e9f9+dirty | 3851.90 ms | 1233.33 ms | -2618.57 ms |
| 5a23c47+dirty | 3855.46 ms | 1221.95 ms | -2633.50 ms |
| 7c6200e+dirty | 3854.87 ms | 1211.83 ms | -2643.03 ms |
| 100ce80+dirty | 3842.93 ms | 1229.52 ms | -2613.41 ms |
| 0bd8916+dirty | 3842.33 ms | 1230.76 ms | -2611.58 ms |
| 5789645+dirty | 3841.18 ms | 1220.67 ms | -2620.52 ms |
| a0d8cf8+dirty | 3842.33 ms | 1212.40 ms | -2629.93 ms |
| a9df5ce+dirty | 3852.73 ms | 1221.38 ms | -2631.35 ms |
| 9c84b9a+dirty | 3849.11 ms | 1218.66 ms | -2630.45 ms |
| 21a1e70+dirty | 3834.15 ms | 1218.43 ms | -2615.73 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 3b6e9f9+dirty | 5.15 MiB | 6.68 MiB | 1.53 MiB |
| 5a23c47+dirty | 4.98 MiB | 6.46 MiB | 1.49 MiB |
| 7c6200e+dirty | 4.98 MiB | 6.56 MiB | 1.58 MiB |
| 100ce80+dirty | 5.15 MiB | 6.67 MiB | 1.51 MiB |
| 0bd8916+dirty | 5.15 MiB | 6.69 MiB | 1.53 MiB |
| 5789645+dirty | 4.98 MiB | 6.50 MiB | 1.52 MiB |
| a0d8cf8+dirty | 5.15 MiB | 6.67 MiB | 1.51 MiB |
| a9df5ce+dirty | 4.98 MiB | 6.55 MiB | 1.57 MiB |
| 9c84b9a+dirty | 4.98 MiB | 6.55 MiB | 1.57 MiB |
| 21a1e70+dirty | 4.98 MiB | 6.46 MiB | 1.49 MiB |
Contributor
iOS (new) Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 5fe1c6c+dirty | 1201.36 ms | 1209.15 ms | 7.78 ms |
| 7c6200e+dirty | 3828.16 ms | 1217.20 ms | -2610.95 ms |
| 41d6254+dirty | 3849.78 ms | 1233.91 ms | -2615.86 ms |
| 5789645+dirty | 3841.36 ms | 1214.81 ms | -2626.55 ms |
| a9df5ce+dirty | 3856.02 ms | 1225.22 ms | -2630.80 ms |
| 9c84b9a+dirty | 3833.41 ms | 1221.79 ms | -2611.62 ms |
| 1a2e7e0+dirty | 3852.95 ms | 1233.78 ms | -2619.18 ms |
| a636fa4+dirty | 3846.16 ms | 1210.10 ms | -2636.05 ms |
| 9b5615c+dirty | 3837.21 ms | 1215.33 ms | -2621.89 ms |
| 3d31fcf+dirty | 3857.46 ms | 1237.17 ms | -2620.29 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 5fe1c6c+dirty | 3.38 MiB | 4.77 MiB | 1.39 MiB |
| 7c6200e+dirty | 5.08 MiB | 6.68 MiB | 1.60 MiB |
| 41d6254+dirty | 5.15 MiB | 6.70 MiB | 1.54 MiB |
| 5789645+dirty | 4.98 MiB | 6.50 MiB | 1.52 MiB |
| a9df5ce+dirty | 4.98 MiB | 6.55 MiB | 1.57 MiB |
| 9c84b9a+dirty | 4.98 MiB | 6.55 MiB | 1.57 MiB |
| 1a2e7e0+dirty | 4.98 MiB | 6.46 MiB | 1.49 MiB |
| a636fa4+dirty | 4.98 MiB | 6.55 MiB | 1.57 MiB |
| 9b5615c+dirty | 4.98 MiB | 6.51 MiB | 1.53 MiB |
| 3d31fcf+dirty | 4.98 MiB | 6.56 MiB | 1.58 MiB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

📢 Type of change
📜 Description
wrapTurboModuleonly recognised two completion signals: a plain return and a thenable return. Bridge methods that report completion through success/failure callbacks returnundefined, so they were closed on return and recorded as sync calls with a near-zero duration — wrongturbo_module.*durations, andnative.turbo_moduleslow-call breadcrumbs that could never fire.Trailing completion callbacks are now wrapped, so the record closes when the callback fires. Details:
genMethodfixes the shape: last argument is the success callback, second-to-last the failure one, and a non-function argument may never follow a function.'promise'-typed methods never receive callbacks and are skipped — the thenable path already covers them.errorCount; those calls close without an error flag.kindstays'async'— no change toTurboModuleCallKind, the aggregate keys, or the API report.kind: 'sync', so RN's'sync'method type is not mislabelled.The callback machinery lives in a new
turboModuleCallbacks.tsbecausewrapTurboModule.tswould otherwise exceed oxlint'smax-lines. No public API change —api-extractorreports the surface unchanged.Fire-and-forget
type: 'async'methods that take no callback still have no completion signal at all and remain ~0ms. Not addressed here.💡 Motivation and Context
Fixes #6542, found by Cursor Bugbot on #6504. Callbacks are the dominant async shape on the Old Architecture bridge and are still used by some TurboModules, so this covered a large share of the calls the instrumentation reported on.
💚 How did you test it?
18 new tests — 16 unit (
test/turbomodule/wrapTurboModule.test.ts) and 2 end-to-end through the integration (test/integrations/turboModuleContext.test.ts), covering: duration measured to the callback, inline settle stayingsync, legacy failure-callback error attribution, no guessing on New Architecture, repeated callback invocations recorded once, receiver/argument/return pass-through, throwing callbacks, cap eviction, age sweep, the late-callback clamp,'promise'-typed methods untouched, non-trailing function arguments ignored, slow-call breadcrumbs, and span credit going to the span open at call start.Full local run:
jest1859 passed (+334 tools), oxlint 0/0, oxfmt clean,tsc -p tsconfig.build.jsonclean,api-extractorup to date,madgeno cycles.Not verified on a real device or simulator — the RN bridge method shape is modelled in tests, not executed.
📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
loggerimported from@sentry/coreinturbomodule/is the Sentry Logs API, not the debug logger (that'sdebug). The 6 pre-existinglogger.warncalls inwrapTurboModule.tsand 3 inwrapNativeModules.tstherefore capture log events from the wrap hot path — with a recursion flavour, since capturing a log goes through the wrappedRNSentry.captureEnvelope. New code here usesdebug; the pre-existing calls are left for a separate fix so this diff stays scoped.## UnreleasedinCHANGELOG.mdhas two### Fixesheadings (pre-existing onmain).sentry-conventionsattributes and the public-API lock are tracked in Turbo Modules: Documentation, migration guide, and public API #6168.