fix: critical type safety issue in tracing system cp-7.57.0#21075
Conversation
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
gambinish
left a comment
There was a problem hiding this comment.
This is a clean solution to this problem
The only thing that stood out to me was the skipped flakey unit test. Outside of that, I've tested on my existing install, toggled on and off metametrics, and also did a fresh install with metametrics disabled. Was able to connect to perps and didn't see the problematic span error in the console.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #21075 +/- ##
==========================================
- Coverage 76.76% 76.75% -0.02%
==========================================
Files 3492 3500 +8
Lines 86382 86440 +58
Branches 16130 16143 +13
==========================================
+ Hits 66312 66343 +31
- Misses 15601 15616 +15
- Partials 4469 4481 +12 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|




Description
This PR fixes a critical type safety issue in the tracing system that caused runtime crashes in Perps when Sentry consent hadn't been granted yet.
What is the reason for the change?
After PR #20817 was merged on Oct 9, 2025, a bug appeared on fresh app installs where Perps would crash with "addEvent is not a function" errors. The root cause was:
TraceContextwas typed asunknowninapp/util/trace.ts, forcing developers to use unsafeas Spancasts throughout the Perps codebase (8 locations)trace()returns a fake placeholder object{ _buffered: true, _name, _id, _local: true }instead of a real Spanas Spancasts made TypeScript think the fake object was a real Span, so it didn't catch when code tried to call.addEvent()or other Span methods on the placeholderThe bug didn't appear during PR #20817 development because the developer already had Sentry consent granted, so
trace()returned real Span objects.What is the improvement/solution?
1. Changed
TraceContexttype fromunknowntoSpan | undefined:2. Removed all 8 unsafe
as Spancasts:app/components/UI/Perps/controllers/PerpsController.ts(6 locations)app/components/UI/Perps/services/PerpsConnectionManager.ts(2 locations)3. Fixed buffered trace return value to be honest
undefined:4. Fixed all files that propagated the
unknowntype:Why this is safe:
TraceContext = Span | undefined, the compiler now catches these bugs at compile timesetMeasurement(name, value, unit, activeSpan?: Span | undefined)already accepts undefinedChangelog
CHANGELOG entry: null
Related issues
Fixes: [Issue about "addEvent is not a function" crash in Perps on fresh installs]
Related PR: #20817 (introduced the unsafe casts that revealed this type system issue)
Manual testing steps
Screenshots/Recordings
N/A - Type safety fix with no UI changes
Before
as Spancasts hiding type mismatchesAfter
undefinedhandlingSimulator.Screen.Recording.-.iPhone.16.Pro.-.2025-10-11.at.14.55.13.mp4
Pre-merge author checklist
Pre-merge reviewer checklist
Technical Details
Files Changed (15 total)
Core type definition:
app/util/trace.ts- Changed TraceContext type and buffered return valueProduction code (7 files):
2.
app/components/Views/AccountStatus/index.tsx- Fixed route param type3.
app/components/Views/Login/index.tsx- Fixed route param type and added assertions4.
app/components/Views/NetworkSelector/useSwitchNetworks.ts- Fixed parentSpan type5.
app/core/createTracingMiddleware/index.ts- Fixed traceContext type6.
app/core/Performance/UIStartup.ts- Uses TraceContext (no changes needed)7.
app/components/UI/Perps/controllers/PerpsController.ts- Removed 6 unsafeas Spancasts8.
app/components/UI/Perps/services/PerpsConnectionManager.ts- Removed 2 unsafeas SpancastsTest files (7 files):
9.
app/components/Views/ChoosePassword/index.test.tsx- Fixed 3 mock objects10.
app/components/Views/ImportFromSecretRecoveryPhrase/index.test.tsx- Fixed 2 mock objects11.
app/core/Performance/UIStartup.test.ts- Fixed 3 mock return values12.
app/core/createTracingMiddleware/index.test.ts- Fixed mock type and return value13-15. Other test files use TraceContext transitively
Impact
TraceContextconsistentlyas Spancasts remain: Verified with grep, all unsafe casts removedNote
Define TraceContext as Span|undefined and return undefined for buffered traces; refactor Perps, middleware, UI startup, and tests to remove unsafe casts and align types.
TraceContextasSpan | undefinedinapp/util/trace.tsand returnundefinedwhen consent not granted (no fake buffered object).endTracebuffering intact.(…) as Spancasts and redundantparentContext/nulls inPerpsController.tsandPerpsConnectionManager.tswhen creatingtracespans.req.traceContextandparentSpanasTraceContextincreateTracingMiddlewareanduseSwitchNetworks.UIStartup: make cached span nullable and adjust tests to handleundefinedspan.onboardingTraceCtxasTraceContextinAccountStatusandLogin.ChoosePassword,ImportFromSecretRecoveryPhrase,createTracingMiddleware,UIStartup.util/tracemock and skip a flaky token selection test inBridgeDestTokenSelector.test.tsx.Written by Cursor Bugbot for commit 2df60d6. This will update automatically on new commits. Configure here.