Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hang when Network.enable is not implemented for a target #30727

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
## 13.16.2

_Released 12/17/2024 (PENDING)_

**Bugfixes:**

- Fixed an issue where targets may hang if `Network.enable` is not implemented for the target. Addresses [#29876](https://github.com/cypress-io/cypress/issues/29876).

## 13.16.1

_Released 12/03/2024_
Expand Down
5 changes: 2 additions & 3 deletions packages/server/lib/browsers/browser-cri-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,8 @@ export class BrowserCriClient {
try {
await browserClient.send('Runtime.runIfWaitingForDebugger', undefined, sessionId)
} catch (error) {
// it's possible that the target was closed before we could enable
// network and continue, in that case, just ignore
debug('error running Runtime.runIfWaitingForDebugger:', error)
// it's possible that the target was closed before we could tell it to run, in that case, just ignore
debug('error running Runtime.runIfWaitingForDebugger: %o', error)
}
}

Expand Down
16 changes: 10 additions & 6 deletions packages/server/lib/browsers/cri-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ export class CriClient implements ICriClient {
this._isChildTarget = !!this.host

if (this._isChildTarget) {
// If crash listeners are added at the browser level, tabs/page connections do not
// emit them.
// If crash listeners are added at the browser level, tabs/page connections do not emit them.
this.cdpConnection.on('Target.targetCrashed', async (event) => {
debug('crash event detected', event)
if (event.targetId !== this.targetId) {
Expand Down Expand Up @@ -368,13 +367,18 @@ export class CriClient implements ICriClient {
if (event.targetInfo.type !== 'service_worker' && event.targetInfo.type !== 'page' && event.targetInfo.type !== 'other') {
await this.cdpConnection.send('Network.enable', this.protocolManager?.networkEnableOptions ?? DEFAULT_NETWORK_ENABLE_OPTIONS, event.sessionId)
}
} catch (error) {
// it's possible that the target was closed before we could enable network, in that case, just ignore
debug('error attaching to target cri: %o', { error, event })
}

if (event.waitingForDebugger) {
if (event.waitingForDebugger) {
try {
await this.cdpConnection.send('Runtime.runIfWaitingForDebugger', undefined, event.sessionId)
} catch (error) {
// it's possible that the target was closed before we could tell it to run, in that case, just ignore
debug('error running Runtime.runIfWaitingForDebugger: %o', { error, event })
}
} catch (error) {
// it's possible that the target was closed before we could enable network and continue, in that case, just ignore
debug('error attaching to target cri', error)
}
}

Expand Down
51 changes: 41 additions & 10 deletions packages/server/test/unit/browsers/cri-client_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import EventEmitter from 'events'
import { ProtocolManagerShape } from '@packages/types'
import type { CriClient } from '../../../lib/browsers/cri-client'
import pDefer from 'p-defer'
import type Protocol from 'devtools-protocol'
const { expect, proxyquire, sinon } = require('../../spec_helper')

const DEBUGGER_URL = 'http://foo'
Expand Down Expand Up @@ -108,11 +109,9 @@ describe('lib/browsers/cri-client', function () {
it('does not enable network', async () => {
await Promise.all(['service_worker', 'page', 'other'].map((type) => {
return fireCDPEvent('Target.attachedToTarget', {
// do not need entire event payload for this test
// @ts-ignore
targetInfo: {
type,
},
} as Protocol.Target.TargetInfo,
})
}))

Expand All @@ -123,30 +122,62 @@ describe('lib/browsers/cri-client', function () {
describe('target type is something other than service worker, page, or other', () => {
it('enables network', async () => {
await fireCDPEvent('Target.attachedToTarget', {
// do not need entire event payload for this test
// @ts-ignore
targetInfo: {
type: 'somethin else',
},
type: 'iframe',
} as Protocol.Target.TargetInfo,
})

expect(criStub.send).to.have.been.calledWith('Network.enable')
})
})

describe('target is waiting for debugger', () => {
const sessionId = 'abc123'

it('sends Runtime.runIfWaitingForDebugger', async () => {
const sessionId = 'abc123'
await fireCDPEvent('Target.attachedToTarget', {
waitingForDebugger: true,
sessionId,
targetInfo: { type: 'service_worker' } as Protocol.Target.TargetInfo,
})

expect(criStub.send).to.have.been.calledWith('Runtime.runIfWaitingForDebugger', undefined, sessionId)
})

it('does not send Runtime.runIfWaitingForDebugger if not waiting for debugger', async () => {
await fireCDPEvent('Target.attachedToTarget', {
waitingForDebugger: false,
sessionId,
targetInfo: { type: 'service_worker' } as Protocol.Target.TargetInfo,
})

expect(criStub.send).not.to.have.been.calledWith('Runtime.runIfWaitingForDebugger')
})

it('sends Runtime.runIfWaitingForDebugger even if Network.enable throws', async () => {
criStub.send.withArgs('Network.enable').throws(new Error('ProtocolError: Inspected target closed'))

await fireCDPEvent('Target.attachedToTarget', {
waitingForDebugger: true,
sessionId,
// @ts-ignore
targetInfo: { type: 'service_worker' },
targetInfo: { type: 'iframe' } as Protocol.Target.TargetInfo,
})

expect(criStub.send).to.have.been.calledWith('Network.enable').and.to.have.thrown()
expect(criStub.send).to.have.been.calledWith('Runtime.runIfWaitingForDebugger', undefined, sessionId)
})

it('continues even if Runtime.runIfWaitingForDebugger throws', async () => {
criStub.send.withArgs('Runtime.runIfWaitingForDebugger').throws(new Error('ProtocolError: Inspected target closed'))

await fireCDPEvent('Target.attachedToTarget', {
waitingForDebugger: true,
sessionId,
targetInfo: { type: 'service_worker' } as Protocol.Target.TargetInfo,
})

expect(criStub.send).to.have.been.calledWith('Runtime.runIfWaitingForDebugger', undefined, sessionId).and.to.have.thrown()
})
})
})

Expand Down
Loading