fix(observability): prevent "Converting circular structure to JSON" crash from circular cy.task payloads [SDK-6016]#1118
Open
harshit-browserstack wants to merge 1 commit into
Open
Conversation
…Converting circular structure to JSON" crash [SDK-6016] The Cypress Observability instrumentation captures command args (command.attributes.args) and cy.log items raw and ships them to the Node plugin via cy.task(), which JSON-serializes the payload. When a command/log arg is a circular Cypress runtime object (e.g. one whose renderOptions.host references itself), serialization throws "Converting circular structure to JSON" and aborts the entire test run. Add a WeakSet-based getCircularReplacer() and a sanitizeForTask() that decycles the payload (circular refs -> "[Circular]") before handing it to cy.task, applied at both flush points (beforeEach/afterEach) and the command:enqueued log stringify. sanitizeForTask wraps serialization in a try/catch so o11y instrumentation can never break the user's test run. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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
SDK-6016 When BrowserStack Test Observability is enabled, the Cypress instrumentation captures command args (
command.attributes.args) andcy.logitems raw and ships them to the Node plugin viacy.task(...), which JSON-serializes the payload (browser → Node). If a command/log arg is a circular Cypress runtime object — e.g. one whoserenderOptions.hostreferences itself — serialization throws:…and aborts the entire test run. There was no try/catch around the flush, so an o11y serialization failure breaks the user's tests — contrary to the graceful-degradation contract.
This commonly surfaces in component-testing setups (
@cypress/vite-dev-server/@cypress/vue) where mount/dev-server config objects are self-referential; e2e args are usually primitive and never trip it, which is why it stayed latent.Fix
getCircularReplacer()and asanitizeForTask()that decycles the payload (circular refs →"[Circular]") before it is handed tocy.task.beforeEach/afterEach) and at thecommand:enqueuedlog stringify.sanitizeForTaskwraps serialization in a try/catch, so o11y instrumentation can never break the user's test run.Non-circular fields are preserved; only true cycles are replaced.
Verification
renderOptions.hostobject throws the exact error under rawJSON.stringify; withsanitizeForTaskit ships cleanly.cfg.renderOptions.host = cfg) withtestObservability: truecompleted 1/1 passed with no "Converting circular structure" in logs — build0e384b2ee0de66beb4fe0254a85d1c66e937e02b.Scope
Fix-only — single file (
bin/testObservability/cypress/index.js), no version bump, no CHANGELOG (owned by the release PR).🤖 Generated with Claude Code