Skip to content

Conversation

@mydea
Copy link
Member

@mydea mydea commented Oct 13, 2023

This slightly refactors the loader script to hopefully fix getsentry/sentry-javascript#9216.

  • We used to pass onLoadCallbacks around, which is unnecessary since we are actually using the same instance everywhere that we mutate, so we can just access the variable directly.
  • For the linked issue, I could replicate that sometimes SDK.init() was called before the onLoad handler was registered. I suspect this may be due to caching behavior where addEventListener('load') may fire immediately (??) - I could replicate it sometimes, sometimes not, when refreshing the page repeatedly. Anyhow, since we rely on this firing after a user had the chance to call Sentry.onLoad, I think it at least doesn't hurt to call this in the next event tick, to potentially fix racing conditions there.

@mydea mydea requested review from AbhiPrasad, Lms24 and lforst October 13, 2023 12:09
@mydea mydea self-assigned this Oct 13, 2023
@github-actions github-actions bot added Scope: Frontend Automatically applied to PRs that change frontend components Scope: Backend Automatically applied to PRs that change backend components labels Oct 13, 2023
@github-actions
Copy link
Contributor

🚨 Warning: This pull request contains Frontend and Backend changes!

It's discouraged to make changes to Sentry's Frontend and Backend in a single pull request. The Frontend and Backend are not atomically deployed. If the changes are interdependent of each other, they must be separated into two pull requests and be made forward or backwards compatible, such that the Backend or Frontend can be safely deployed independently.

Have questions? Please ask in the #discuss-dev-infra channel.

Copy link
Contributor

@lforst lforst left a comment

Choose a reason for hiding this comment

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

I have a slight feeling that we're only contributing to the confusingness of this file with this change. Ideally, we should defer the calling of init until the script tag's load event handler fired no? I won't block this though.

@codecov
Copy link

codecov bot commented Oct 13, 2023

Codecov Report

Merging #58070 (17a1f7d) into master (00fb8dd) will decrease coverage by 0.02%.
Report is 5 commits behind head on master.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master   #58070      +/-   ##
==========================================
- Coverage   79.11%   79.10%   -0.02%     
==========================================
  Files        5144     5143       -1     
  Lines      224118   224107      -11     
  Branches    37741    37740       -1     
==========================================
- Hits       177305   177273      -32     
- Misses      41136    41154      +18     
- Partials     5677     5680       +3     
Files Coverage Δ
...ews/settings/organizationGeneralSettings/index.tsx 90.90% <ø> (ø)

... and 7 files with indirect coverage changes

@mydea
Copy link
Member Author

mydea commented Oct 17, 2023

I have a slight feeling that we're only contributing to the confusingness of this file with this change. Ideally, we should defer the calling of init until the script tag's load event handler fired no? I won't block this though.

I do not disagree, however it is sadly a bit opaque when the load event for the script tag triggers etc 😬 If we can think of a better way to do this, I'd be happy!!

@mydea mydea force-pushed the fn/loader-race-condition branch from e7cf355 to bd00b4d Compare October 17, 2023 11:28
Copy link
Member Author

Choose a reason for hiding this comment

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

I extracted all of this into a function & adjusted it so that we only call setupSDK in the next tick, which hopefully makes all of this a bit clearer!

Copy link
Member Author

Choose a reason for hiding this comment

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

Adding once and passive here which IMHO make sense (the handler can be cleaned up after being called), and in older browsers will just be ignored.

Copy link
Member Author

Choose a reason for hiding this comment

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

Renaming this to be a bit clearer, naming wise.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we can get rid of this. Instead, when a user call Sentry.forceLoad(), we simply inject the SDK the same way we would for a non-lazy setup - all the other logic is not needed IMHO?

Comment on lines 75 to 76
Copy link
Contributor

Choose a reason for hiding this comment

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

Problem: Inbetween removing these and waiting for the next tick there might be an error which is not recorded. We need to remove these listeners right before we attach the actual non-buffered once, so in theory right before the call to the actual Sentry.init.

As discussed: Let's move it into the monkeypatch init.

Copy link
Member Author

Choose a reason for hiding this comment

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

moved this!

@mydea
Copy link
Member Author

mydea commented Oct 18, 2023

@lforst updated this according to what we talked about!

@mydea mydea merged commit e2ddba2 into master Oct 19, 2023
@mydea mydea deleted the fn/loader-race-condition branch October 19, 2023 08:57
mydea added a commit to getsentry/sentry-javascript that referenced this pull request Oct 19, 2023
This updates the loader based on this PR:
getsentry/sentry#58070
and also actually fixes/improves the loader tests by avoiding importing
`@sentry/browser`. This is not needed and actually obfuscates the
"real"/"normal" behavior (just look at the generated `dist` folder for a
test.

Instead, I added an eslint rule to allow `Sentry` as a global for these
cases, and just access them directly. This also uncovered an incorrect
test, which I adjusted so it works. I also added a new test to ensure
custom config works.
@github-actions github-actions bot locked and limited conversation to collaborators Nov 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loader script not reliably setting beforeNavigate context name

3 participants