Skip to content

Enable recording session if first RUM message happened before init#1777

Merged
louiszawadzki merged 3 commits into
developfrom
louiszawadzki/rum-2293/fix-sr-race-condition-with-rum-event
Jan 3, 2024
Merged

Enable recording session if first RUM message happened before init#1777
louiszawadzki merged 3 commits into
developfrom
louiszawadzki/rum-2293/fix-sr-race-condition-with-rum-event

Conversation

@louiszawadzki

Copy link
Copy Markdown
Contributor

What does this PR do?

This fixes a race condition that was quite present in RN - see description below.

The solution I choose was to move the responsibility of checking for initialization from startRecording into checkStatusAndApplySample.
The startRecording method is easier to work with if it just performs an atomic action (like stopRecording).

It seems fit to put the check in checkStatusAndApplySample since it's the method that registers the currentRumSessionId that was creating the lock before.

Motivation

Description of the race condition

This can happen in any Android app, it’s just that conditions for creating the race condition are more present in RN since we collect JS framerate through RUM events on every frame.

The race condition happens in SessionReplayFeature.kt, in the onInitialize function:

        // first line making the race condition possible
        sdkCore.setEventReceiver(SESSION_REPLAY_FEATURE_NAME, this)
        dataWriter = createDataWriter()
        sessionReplayRecorder = sessionReplayRecorderProvider(dataWriter, appContext)
        @Suppress("ThreadSafety") // TODO REPLAY-1861 can be called from any thread
        sessionReplayRecorder.registerCallbacks()
        initialized.set(true) 
        // end of race condition

This is called when we call SessionReplay.enable.

By setting the event receiver, we listen on all RUM events. Now if a RUM event happens before we call initialized.set(true), here is what is going to happen in SessionReplayFeature:

So far the Session Replay is not recording, and on the next RUM event, even if initialized.set(true) has been called:

So we fail to ever initialize the SDK.

Additional Notes

Anything else we should know when reviewing?

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

@louiszawadzki
louiszawadzki requested review from a team as code owners December 19, 2023 16:48
@louiszawadzki

Copy link
Copy Markdown
Contributor Author

Other solutions I had:

Moving sdkCore.setEventReceiver(SESSION_REPLAY_FEATURE_NAME, this) call after initialized.set(true) in onInitialize

This alone does not fix the underlying issue of the lock happening because of the session Id being set. This might be a second step we can take, but to me it makes sense to have initialized.set(true) at the very end.

Returning whether the recording started from startRecording

In this solution we lose the early return syntax and it makes both functions harder to read in my opinion.

@louiszawadzki
louiszawadzki force-pushed the louiszawadzki/rum-2293/fix-sr-race-condition-with-rum-event branch from 3712085 to 14858da Compare December 19, 2023 17:03
@louiszawadzki
louiszawadzki force-pushed the louiszawadzki/rum-2293/fix-sr-race-condition-with-rum-event branch from 14858da to 7d3a21d Compare December 20, 2023 09:01
@codecov-commenter

codecov-commenter commented Dec 20, 2023

Copy link
Copy Markdown

Codecov Report

Merging #1777 (20857f2) into develop (1972792) will increase coverage by 0.01%.
Report is 20 commits behind head on develop.
The diff coverage is 100.00%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1777      +/-   ##
===========================================
+ Coverage    83.46%   83.47%   +0.01%     
===========================================
  Files          469      469              
  Lines        16431    16434       +3     
  Branches      2465     2466       +1     
===========================================
+ Hits         13714    13718       +4     
+ Misses        2038     2034       -4     
- Partials       679      682       +3     
Files Coverage Δ
...oid/sessionreplay/internal/SessionReplayFeature.kt 99.05% <100.00%> (+0.03%) ⬆️

... and 22 files with indirect coverage changes

@louiszawadzki
louiszawadzki merged commit c49f702 into develop Jan 3, 2024
@louiszawadzki
louiszawadzki deleted the louiszawadzki/rum-2293/fix-sr-race-condition-with-rum-event branch January 3, 2024 10:25
@xgouchet xgouchet added this to the 2.5.0 milestone Feb 19, 2024
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.

5 participants