Skip to content

Conversation

@philipphofmann
Copy link
Member

📜 Description

Fix a crash when serializing the trace context after initializing the SentryHub manually.

💡 Motivation and Context

A user reported a crash on Discord when running the following code

let sentryOption = Options()
sentryOption.dsn = dsn
        
let scope = Scope()
self.sentryHub = SentryHub(client: SentryClient(options: sentryOption), andScope: scope)

let error = NSError(domain: "abc", code: 12)
sentryHub.capture(error: error)

image

💚 How did you test it?

Unit tests.

📝 Checklist

You have to check all boxes before merging:

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Fix a crash when serializing the trace context after initializing the
SentryHub manually.
@codecov
Copy link

codecov bot commented Nov 3, 2023

Codecov Report

Merging #3374 (598bd45) into main (c471221) will increase coverage by 0.021%.
The diff coverage is 100.000%.

Impacted file tree graph

@@              Coverage Diff              @@
##              main     #3374       +/-   ##
=============================================
+ Coverage   89.053%   89.074%   +0.021%     
=============================================
  Files          521       521               
  Lines        56045     56135       +90     
  Branches     20170     20212       +42     
=============================================
+ Hits         49910     50002       +92     
  Misses        5220      5220               
+ Partials       915       913        -2     
Files Coverage Δ
Sources/Sentry/SentryClient.m 97.588% <100.000%> (+0.008%) ⬆️
Sources/Sentry/SentryNetworkTracker.m 92.571% <100.000%> (+0.085%) ⬆️
Sources/Sentry/SentryPropagationContext.m 100.000% <ø> (ø)
Sources/Sentry/SentryTraceContext.m 92.035% <100.000%> (+0.773%) ⬆️
...erformance/Network/SentryNetworkTrackerTests.swift 97.336% <100.000%> (+0.029%) ⬆️
Tests/SentryTests/SentryClientTests.swift 96.028% <100.000%> (+0.002%) ⬆️
Tests/SentryTests/SentryHubTests.swift 98.381% <100.000%> (+0.015%) ⬆️
...entryTests/Transaction/SentryTraceStateTests.swift 100.000% <100.000%> (ø)

... and 16 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c471221...598bd45. Read the comment docs.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2023

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 1237.76 ms 1244.58 ms 6.82 ms
Size 22.85 KiB 414.11 KiB 391.26 KiB

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
279841c 1250.80 ms 1263.08 ms 12.29 ms
c319795 1205.12 ms 1231.20 ms 26.08 ms
89bc37d 1228.20 ms 1257.10 ms 28.90 ms
1437c68 1244.86 ms 1254.18 ms 9.32 ms
5616e0a 1237.00 ms 1260.43 ms 23.43 ms
67460f4 1244.56 ms 1255.96 ms 11.40 ms
3a31fc9 1237.35 ms 1249.02 ms 11.67 ms
01a28a9 1200.78 ms 1227.90 ms 27.12 ms
72c8d84 1238.96 ms 1247.34 ms 8.38 ms
e324230 1225.84 ms 1250.40 ms 24.57 ms

App size

Revision Plain With Sentry Diff
279841c 22.84 KiB 403.19 KiB 380.34 KiB
c319795 20.76 KiB 431.99 KiB 411.22 KiB
89bc37d 20.76 KiB 401.53 KiB 380.77 KiB
1437c68 22.85 KiB 410.96 KiB 388.11 KiB
5616e0a 22.85 KiB 407.45 KiB 384.60 KiB
67460f4 20.76 KiB 426.15 KiB 405.39 KiB
3a31fc9 20.76 KiB 414.45 KiB 393.69 KiB
01a28a9 22.85 KiB 405.39 KiB 382.54 KiB
72c8d84 22.85 KiB 408.88 KiB 386.03 KiB
e324230 22.85 KiB 408.87 KiB 386.02 KiB

Previous results on branch: fix/crash-when-initializing-manually

Startup times

Revision Plain With Sentry Diff
c7bedd1 1223.18 ms 1244.20 ms 21.02 ms
121d6d3 1211.39 ms 1229.22 ms 17.83 ms
6ccf54e 1238.06 ms 1239.10 ms 1.04 ms
0519fc7 1237.80 ms 1240.32 ms 2.52 ms

App size

Revision Plain With Sentry Diff
c7bedd1 22.85 KiB 414.08 KiB 391.23 KiB
121d6d3 22.85 KiB 411.85 KiB 389.00 KiB
6ccf54e 22.85 KiB 413.96 KiB 391.11 KiB
0519fc7 22.85 KiB 414.08 KiB 391.23 KiB

* @param options The current active options.
* @param userSegment You can retrieve this usually from the `scope.userObject.segment`.
*/
- (SentryTraceContext *)getTraceContext:(SentryOptions *)options
Copy link
Member

Choose a reason for hiding this comment

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

The way this is named makes me think it's retrieving something like from an ivar, but really it's creating a new thing every time it's called. Can we rename it to e.g. 'traceContextForOptions:userSegment:`?

Copy link
Member

@armcknight armcknight Nov 3, 2023

Choose a reason for hiding this comment

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

Really, I would prefer that we remove this method from SentryPropagationContext altogether and just call [[SentryTraceContext alloc] initWithTraceId:...] in the places where this method is called, I don't understand why this should be wrapped in SentryPropagationContext at all, the indirection at the call sites is confusing.

Now that most of the ivars are gone (except for self.traceId, which could just be accessed from the callsites and passed to the trace context init) it's just taking parameters and repackaging them.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is an excellent point. I fully I agree 🥇. I moved it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Honestly, objc is already verbose as it is, I don't think we should keeping add to it, which also increase maintenance, because if we need to change the initialisation to add/remove more attributes, we need to do it everywhere instead of one center place.

If getting traceContext from the propagation context was not good enough (because the scope is not always from the hub), we could move this function to the scope itself and get its traceContext from itself. Here is not big of a deal, this new initialisation is used only twice (for now), my fear is that we start doing this everywhere.

If the problem is the calculated property we change it to a function, no problem.

Copy link
Contributor

@brustolin brustolin left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@armcknight armcknight left a comment

Choose a reason for hiding this comment

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

LGTM, just needs a changelog update and one small fixup!

@philipphofmann philipphofmann merged commit 313b1d9 into main Nov 8, 2023
@philipphofmann philipphofmann deleted the fix/crash-when-initializing-manually branch November 8, 2023 08:29
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.

4 participants