fix(upload): guard against envelope.data being null after async compress await - #1107
Open
Vitaliy Ponomarenko (ponomarenko) wants to merge 2 commits into
Open
Conversation
…ess await When upload() awaits compress(payload), the JS event loop is yielded. During that window, a page navigation can trigger clarity.stop() which calls envelope.stop() and sets envelope.data = null. On resumption, accessing envelope.data.sequence throws: TypeError: Cannot read properties of null (reading 'sequence') The existing `if (!envelope.data) return` check is placed before the await and therefore does not protect the post-await code path. Fix: add a second null guard immediately after the await so we exit cleanly when stop() has already torn down the envelope. Also add a null guard in delay() which reads envelope.data.sequence without any prior null check.
Copilot started reviewing on behalf of
Vitaliy Ponomarenko (ponomarenko)
June 30, 2026 11:45
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an async race in clarity-js upload scheduling where envelope.data can become null during the await compress(payload) window (e.g., during SPA navigations that trigger clarity.stop()), preventing a TypeError from reading envelope.data.sequence.
Changes:
- Add a post-
awaitnull guard inupload()to avoid accessingenvelope.data.sequenceafter shutdown. - Add a defensive null guard in
delay()to avoid readingenvelope.data.sequencewhenenvelope.dataisnull.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Author
|
Any updates? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
upload()is anasyncfunction. After theawait compress(payload)call, the JS event loop is yielded. During that window a page navigation can triggerclarity.stop(), which callsenvelope.stop()and setsenvelope.data = null.When the microtask resumes, the very next line:
throws an unhandled promise rejection that Angular's Zone.js surfaces as an error.
The existing guard added by #894 (
if (!envelope.data) return;) is placed before theawaitand therefore does not cover this post-awaitwindow.This error is reproducible on Angular SPAs with frequent route transitions where Clarity flushes data at the moment the user navigates away.
Fix
1. Add a second null guard in
upload()immediately after theawait:2. Add a null guard in
delay()which also readsenvelope.data.sequencewithout any prior null check:Root cause diagram