Skip to content

Stream background traces to multiple RNDT sessions if present#55351

Closed
motiz88 wants to merge 4 commits into
react:mainfrom
motiz88:export-D90888852
Closed

Stream background traces to multiple RNDT sessions if present#55351
motiz88 wants to merge 4 commits into
react:mainfrom
motiz88:export-D90888852

Conversation

@motiz88

@motiz88 motiz88 commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

Summary:
Changelog: [Internal]

Behaviour

Fixes an edge case in the way background traces are emitted when multiple RNDT frontends (or other clients with the ReactNativeApplication domain enabled) are connected to a single Host. Previously, only one of the clients would receive the CDP events for the trace. With this diff, all of them will.

NOTE: This leaves another, even smaller edge case / inconsistency: if there are no RNDT frontends at the time a background trace ends, we still only send it to the first frontend (if any) that connects. This is logically more defensible than picking one client out of multiple active connections ( = what's fixed in this diff).

Implementation

Tracing functionality is somewhat awkwardly split between HostTarget (background traces) and TracingAgent (CDP-initiated traces). This diff doesn't attempt to fully clean this up[1], but we do reduce duplication and boilerplate by creating a helper function used by both. This function encapsulates the gnarly/surprising details of dealing with tracing::HostTracingProfile objects in a memory-efficient way:

  1. As per the existing implementation, we serialise them to JSON chunk-by-chunk, destroying the original object in the process.
  2. Each chunk is sent to all relevant sessions and destroyed before we begin serialising the next one.

[1] In future work, we should probably move the Host's tracing state and logic into its own TracingTarget class. The naturally close coupling of a Target with its corresponding Agent would be helpful here.

Differential Revision: D90888852

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 29, 2026
@meta-codesync

meta-codesync Bot commented Jan 29, 2026

Copy link
Copy Markdown

@motiz88 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D90888852.

@motiz88 motiz88 force-pushed the export-D90888852 branch 2 times, most recently from be2dfb8 to 3c335e1 Compare January 30, 2026 13:12
…ets (react#55314)

Summary:

TSIA - minor refactor for correctness and clarity before making changes to some of these methods up the stack.

Changelog: [Internal]

Reviewed By: hoxyq

Differential Revision: D91140491
Summary:

Changelog: [Internal]

TSIA - minor refactor for convenience and correctness.

NOTE: When we get C++23, we can deduplicate the identical const/non-const method bodies using the nifty [deducing `this`](https://en.cppreference.com/w/cpp/language/member_functions.html#Explicit_object_member_functions) feature.

Reviewed By: hoxyq

Differential Revision: D91140490
)

Summary:

Changelog: [Internal]

Moves state and logic related to stashing a background trace from `JReactHostInspectorTarget` (JNI wrapper boilerplate, Android-specific) to `HostTarget` (cross-platform C++).

Managing the full lifecycle of a background trace inside `HostTarget` lets us remove a fair amount of API surface:
* `HostTargetDelegate::unstable_getHostTracingProfileThatWillBeEmittedOnInitialization()`
* `HostTarget::hasActiveSessionWithFuseboxClient()`
* `TracingAgent::emitExternalHostTracingProfile()`

We also refactor some of the surrounding code and add tests (previously missing) for the behaviour of stashed background traces when there is more than one session.

Reviewed By: huntie, hoxyq

Differential Revision: D91589884
…55351)

Summary:

Changelog: [Internal]

## Behaviour

Fixes an edge case in the way background traces are emitted when multiple RNDT frontends (or other clients with the `ReactNativeApplication` domain enabled) are connected to a single Host. Previously, only one of the clients would receive the CDP events for the trace. With this diff, *all* of them will.

NOTE: This leaves another, even smaller edge case / inconsistency: if there are *no* RNDT frontends at the time a background trace ends, we still only send it to the first frontend (if any) that connects. This is logically more defensible than picking one client out of multiple active connections ( = what's fixed in this diff). 

## Implementation

Tracing functionality is somewhat awkwardly split between `HostTarget` (background traces) and `TracingAgent` (CDP-initiated traces). This diff doesn't attempt to fully clean this up[1], but we do reduce duplication and boilerplate by creating a helper function used by both. This function encapsulates the gnarly/surprising details of dealing with `tracing::HostTracingProfile` objects in a memory-efficient way:

1. As per the existing implementation, we serialise them to JSON chunk-by-chunk, destroying the original object in the process.
2. Each chunk is sent to all relevant sessions *and destroyed* before we begin serialising the next one.

[1] In future work, we should probably move the Host's tracing state and logic into its own `TracingTarget` class. The naturally close coupling of a Target with its corresponding Agent would be helpful here.

Reviewed By: huntie

Differential Revision: D90888852
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @motiz88 in a640790

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Feb 1, 2026
@meta-codesync

meta-codesync Bot commented Feb 1, 2026

Copy link
Copy Markdown

This pull request has been merged in a640790.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged This PR has been merged. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants