Skip to content

Commit

Permalink
fix: issue with service worker attachments (#28147)
Browse files Browse the repository at this point in the history
Co-authored-by: Emily Rohrbough <[email protected]>
  • Loading branch information
ryanthemanuel and emilyrohrbough authored Oct 26, 2023
1 parent 09fa9d7 commit 15f7300
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
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.3.4

_Released 11/7/2023 (PENDING)_

**Bugfixes:**

- Fixed a regression in [`13.3.2`](https://docs.cypress.io/guides/references/changelog/13.3.2) where loading a service worker and immediately reloading the page can cause a crash. Fixes [#28141](https://github.com/cypress-io/cypress/issues/28141).

## 13.3.3

_Released 10/24/2023_
Expand Down
15 changes: 10 additions & 5 deletions packages/server/lib/browsers/browser-cri-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,17 @@ export class BrowserCriClient {
// The basic approach here is we attach to targets and enable network traffic
// We must attach in a paused state so that we can enable network traffic before the target starts running.
browserClient.on('Target.attachedToTarget', async (event) => {
if (event.targetInfo.type !== 'page') {
await browserClient.send('Network.enable', protocolManager?.networkEnableOptions ?? DEFAULT_NETWORK_ENABLE_OPTIONS, event.sessionId)
}
try {
if (event.targetInfo.type !== 'page') {
await browserClient.send('Network.enable', protocolManager?.networkEnableOptions ?? DEFAULT_NETWORK_ENABLE_OPTIONS, event.sessionId)
}

if (event.waitingForDebugger) {
await browserClient.send('Runtime.runIfWaitingForDebugger', undefined, event.sessionId)
if (event.waitingForDebugger) {
await browserClient.send('Runtime.runIfWaitingForDebugger', undefined, event.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 attaching to target browser', error)
}
})

Expand Down
19 changes: 12 additions & 7 deletions packages/server/lib/browsers/cri-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,18 @@ export const create = async ({

if (fullyManageTabs) {
cri.on('Target.attachedToTarget', async (event) => {
// Service workers get attached at the page and browser level. We only want to handle them at the browser level
if (event.targetInfo.type !== 'service_worker' && event.targetInfo.type !== 'page') {
await cri.send('Network.enable', protocolManager?.networkEnableOptions ?? DEFAULT_NETWORK_ENABLE_OPTIONS, event.sessionId)
}

if (event.waitingForDebugger) {
await cri.send('Runtime.runIfWaitingForDebugger', undefined, event.sessionId)
try {
// Service workers get attached at the page and browser level. We only want to handle them at the browser level
if (event.targetInfo.type !== 'service_worker' && event.targetInfo.type !== 'page') {
await cri.send('Network.enable', protocolManager?.networkEnableOptions ?? DEFAULT_NETWORK_ENABLE_OPTIONS, event.sessionId)
}

if (event.waitingForDebugger) {
await cri.send('Runtime.runIfWaitingForDebugger', undefined, event.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 attaching to target cri', error)
}
})

Expand Down
25 changes: 25 additions & 0 deletions packages/server/test/unit/browsers/browser-cri-client_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,31 @@ describe('lib/browsers/cri-client', function () {
expect(protocolManager.connectToBrowser).to.be.calledWith(client)
})

it('creates a page client when the passed in url is found and notifies the protocol manager and fully managed tabs and attaching to target throws', async function () {
const mockPageClient = {}
const protocolManager: any = {
connectToBrowser: sinon.stub().resolves(),
}

send.withArgs('Target.getTargets').resolves({ targetInfos: [{ targetId: '1', url: 'http://foo.com' }, { targetId: '2', url: 'http://bar.com' }] })
send.withArgs('Target.setDiscoverTargets', { discover: true })
on.withArgs('Target.targetDestroyed', sinon.match.func)

send.withArgs('Network.enable').throws(new Error('ProtocolError: Inspected target navigated or closed'))

criClientCreateStub.withArgs({ target: '1', onAsynchronousError: onError, host: HOST, port: PORT, protocolManager, fullyManageTabs: true, browserClient: { on, send, close } }).resolves(mockPageClient)

const browserClient = await getClient({ protocolManager, fullyManageTabs: true })

const client = await browserClient.attachToTargetUrl('http://foo.com')

expect(client).to.be.equal(mockPageClient)
expect(protocolManager.connectToBrowser).to.be.calledWith(client)

// This would throw if the error was not caught
await on.withArgs('Target.attachedToTarget').args[0][1]({ targetInfo: { type: 'worker' } })
})

it('retries when the passed in url is not found', async function () {
sinon.stub(protocol, '_getDelayMsForRetry')
.onFirstCall().returns(100)
Expand Down
8 changes: 8 additions & 0 deletions packages/server/test/unit/browsers/cri-client_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ describe('lib/browsers/cri-client', function () {
await expect(client.send(command, { depth: -1 })).to.be.rejectedWith(`${command} will not run as the target browser or tab CRI connection has crashed`)
})

it('does not reject if attachToTarget work throws', async function () {
criStub.send.withArgs('Network.enable').throws(new Error('ProtocolError: Inspected target navigated or closed'))
await getClient({ host: '127.0.0.1', fullyManageTabs: true })

// This would throw if the error was not caught
await criStub.on.withArgs('Target.attachedToTarget').args[0][1]({ targetInfo: { type: 'worker' } })
})

context('retries', () => {
([
'WebSocket is not open',
Expand Down

5 comments on commit 15f7300

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 15f7300 Oct 26, 2023

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.3.4/linux-arm64/develop-15f730007f10705db8c6f4a3de4ddefdfc930df1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 15f7300 Oct 26, 2023

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.3.4/linux-x64/develop-15f730007f10705db8c6f4a3de4ddefdfc930df1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 15f7300 Oct 26, 2023

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.3.4/darwin-x64/develop-15f730007f10705db8c6f4a3de4ddefdfc930df1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 15f7300 Oct 26, 2023

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.3.4/darwin-arm64/develop-15f730007f10705db8c6f4a3de4ddefdfc930df1/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 15f7300 Oct 26, 2023

Choose a reason for hiding this comment

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

Circle has built the win32 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.3.4/win32-x64/develop-15f730007f10705db8c6f4a3de4ddefdfc930df1/cypress.tgz

Please sign in to comment.