Skip to content

Commit

Permalink
fix: Unhandled "WebSocket connection closed" when CDP connection is u…
Browse files Browse the repository at this point in the history
…nstable (#29830)

* unit and integration tests that reproduce websocket disconnected unhandled exception

* WIP: command queue

* complete command queue and retry refactor

* cri-client changes pass tests; modify certain tests for readability and accuracy

* removes unnecessary logic from command queue, adds unit tests for command queue

* rm unused cdp state - this should be reserved for future refactor

* small edits to cri-client: better error handling, more comprehensive comments

* comment re: queue property

* rearrange cri client member methods for readability

* further edits

* Changelog

* Update cli/CHANGELOG.md

Co-authored-by: Mike McCready <66998419+MikeMcC399@users.noreply.github.com>

* fix continuous retry on close

* split heavier debugs to verbose

---------

Co-authored-by: Mike McCready <66998419+MikeMcC399@users.noreply.github.com>
Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
  • Loading branch information
3 people committed Jul 16, 2024
1 parent cbbc8ee commit 8a48ee7
Show file tree
Hide file tree
Showing 6 changed files with 699 additions and 259 deletions.
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ _Released 7/16/2024 (PENDING)_

**Bugfixes:**

- Fixed an issue where unhandled `WebSocket connection closed` exceptions would be thrown when CDP connections rapidly connect, disconnect, and connect again while there are pending commands. Fixes [#29572](https://github.com/cypress-io/cypress/issues/29572).
- CLI output properly displays non-JSON response bodies when a Test Replay upload attempt returns a non-JSON response body for a non-200 status code. Addressed in [#29801](https://github.com/cypress-io/cypress/pull/29801).
- Fixed an issue where the ReadStream used to upload a Test Replay recording could erroneously be re-used when retrying in cases of retryable upload failures. Fixes [#29227](https://github.com/cypress-io/cypress/issues/29227).
- Fixed an issue where command snapshots were not being captured within the `cy.origin()` command within Test Replay. Addressed in [#29828](https://github.com/cypress-io/cypress/pull/29828).
Expand Down
86 changes: 86 additions & 0 deletions packages/server/lib/browsers/cdp-command-queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import type ProtocolMapping from 'devtools-protocol/types/protocol-mapping'
import pDefer, { DeferredPromise } from 'p-defer'
import type { CdpCommand } from './cdp_automation'
import Debug from 'debug'

const debug = Debug('cypress:server:browsers:cdp-command-queue')
const debugVerbose = Debug('cypress:server:browsers:cd-command-queue')

type CommandReturn<T extends CdpCommand> = ProtocolMapping.Commands[T]['returnType']

export type Command<T extends CdpCommand> = {
command: T
params?: object
deferred: DeferredPromise<CommandReturn<T>>
sessionId?: string
}

export class CDPCommandQueue {
private queue: Command<any>[] = []

public get entries () {
return [...this.queue]
}

public add <TCmd extends CdpCommand> (
command: TCmd,
params: ProtocolMapping.Commands[TCmd]['paramsType'][0],
sessionId?: string,
): Promise<CommandReturn<TCmd>> {
debug('enqueing command %s', command)
debugVerbose('enqueing command %s with params %o', command, params)

const deferred = pDefer<CommandReturn<TCmd>>()

const commandPackage: Command<TCmd> = {
command,
params,
deferred,
sessionId,
}

this.queue.push(commandPackage)

debug('Command enqueued; new length: %d', this.queue.length)
debugVerbose('Queue Contents: %O', this.queue)

return deferred.promise
}

public clear () {
debug('clearing command queue')
this.queue = []
}

public extract<T extends CdpCommand> (search: Partial<Command<T>>): Command<T> | undefined {
// this should find, remove, and return if found a given command

const index = this.queue.findIndex((enqueued) => {
for (let k of Object.keys(search)) {
if (search[k] !== enqueued[k]) {
return false
}
}

return true
})

debug('extracting %o from commands at index %d', search, index)

if (index === -1) {
return undefined
}

const [extracted] = this.queue.splice(index, 1)

return extracted
}

public shift () {
return this.queue.shift()
}

public unshift (value: Command<any>) {
return this.queue.unshift(value)
}
}
Loading

4 comments on commit 8a48ee7

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8a48ee7 Jul 16, 2024

Choose a reason for hiding this comment

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

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.13.1/linux-arm64/develop-8a48ee7cdf92a1659524876cd25457fd51fbfeb3/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8a48ee7 Jul 16, 2024

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.13.1/linux-x64/develop-8a48ee7cdf92a1659524876cd25457fd51fbfeb3/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8a48ee7 Jul 16, 2024

Choose a reason for hiding this comment

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

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.13.1/darwin-arm64/develop-8a48ee7cdf92a1659524876cd25457fd51fbfeb3/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8a48ee7 Jul 16, 2024

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.13.1/darwin-x64/develop-8a48ee7cdf92a1659524876cd25457fd51fbfeb3/cypress.tgz

Please sign in to comment.