Skip to content

Commit

Permalink
fix: electron crashing prematurely when window closes prematurely whi…
Browse files Browse the repository at this point in the history
…le browser launch process is ongoing (#27167)

* chore: add system test for desired behavior

* fix: issue where process was exiting when window was being closed while launch process was occuring

* chore: add changelog entry
  • Loading branch information
AtofStryker committed Jun 29, 2023
1 parent e0d814c commit f05f623
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 15 deletions.
2 changes: 2 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ _Released 07/05/2023 (PENDING)_
- Fixed issues where commands would fail with the error `must only be invoked from the spec file or support file`. Fixes [#27149](https://github.com/cypress-io/cypress/issues/27149) and [#27163](https://github.com/cypress-io/cypress/issues/27163).
- Fixed an issue where chrome was not recovering from browser crashes properly. Fixes [#24650](https://github.com/cypress-io/cypress/issues/24650).
- Fixed a race condition that was causing a GraphQL error to appear on the [Debug page](https://docs.cypress.io/guides/cloud/runs#Debug) when viewing a running Cypress Cloud build. Fixed in [#27134](https://github.com/cypress-io/cypress/pull/27134).
- Fixed a race condition in electron where the test window exiting prematurely during the browser launch process was causing the whole test run to fail. Addressed in [#27167](https://github.com/cypress-io/cypress/pull/27167).


**Dependency Updates:**

Expand Down
44 changes: 29 additions & 15 deletions packages/server/lib/browsers/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,39 @@ export = {
}
},
async onNewWindow (this: BrowserWindow, e, url) {
const _win = this
let _win: BrowserWindow | null = this

const child = await _this._launchChild(e, url, _win, projectRoot, state, options, automation)

// close child on parent close
_win.on('close', () => {
if (!child.isDestroyed()) {
child.destroy()
}
_win.on('closed', () => {
// in some cases, the browser/test will close before _launchChild completes, leaving a destroyed/stale window object.
// in these cases, we want to proceed to the next test/open window without critically failing
_win = null
})

// add this pid to list of pids
tryToCall(child, () => {
if (instance && instance.pid) {
if (!instance.allPids) throw new Error('Missing allPids!')

instance.allPids.push(child.webContents.getOSProcessId())
try {
const child = await _this._launchChild(e, url, _win, projectRoot, state, options, automation)

// close child on parent close
_win.on('close', () => {
if (!child.isDestroyed()) {
child.destroy()
}
})

// add this pid to list of pids
tryToCall(child, () => {
if (instance && instance.pid) {
if (!instance.allPids) throw new Error('Missing allPids!')

instance.allPids.push(child.webContents.getOSProcessId())
}
})
} catch (e) {
if (_win === null) {
debug(`The window was closed while launching the child process. This usually means the browser or test errored before fully completing the launch process. Cypress will proceed to the next test`)
} else {
throw e
}
})
}
},
}

Expand Down
82 changes: 82 additions & 0 deletions system-tests/__snapshots__/browser_crash_handling_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,85 @@ This can happen for many different reasons:
`

exports['Browser Crash Handling / when the window closes mid launch of the browser process / passes'] = `
====================================================================================================
(Run Starting)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Cypress: 1.2.3 │
│ Browser: FooBrowser 88 │
│ Specs: 2 found (abort_beforeunload_event_child.cy.ts, abort_beforeunload_event.cy.ts) │
│ Searched: cypress/e2e/abort_beforeunload_event_child.cy.ts, cypress/e2e/abort_beforeunload_e │
│ vent.cy.ts │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
────────────────────────────────────────────────────────────────────────────────────────────────────
Running: abort_beforeunload_event_child.cy.ts (1 of 2)
✓ will exit even if an beforeload event dialog is present in a child window
1 passing
(Results)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Tests: 1 │
│ Passing: 1 │
│ Failing: 0 │
│ Pending: 0 │
│ Skipped: 0 │
│ Screenshots: 0 │
│ Video: false │
│ Duration: X seconds │
│ Spec Ran: abort_beforeunload_event_child.cy.ts │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
────────────────────────────────────────────────────────────────────────────────────────────────────
Running: abort_beforeunload_event.cy.ts (2 of 2)
✓ will exit even if an beforeload event dialog is present
1 passing
(Results)
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Tests: 1 │
│ Passing: 1 │
│ Failing: 0 │
│ Pending: 0 │
│ Skipped: 0 │
│ Screenshots: 0 │
│ Video: false │
│ Duration: X seconds │
│ Spec Ran: abort_beforeunload_event.cy.ts │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
====================================================================================================
(Run Finished)
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ✔ abort_beforeunload_event_child.cy.t XX:XX 1 1 - - - │
│ s │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ✔ abort_beforeunload_event.cy.ts XX:XX 1 1 - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
✔ All specs passed! XX:XX 2 2 - - -
`
9 changes: 9 additions & 0 deletions system-tests/projects/e2e/blocking_beforeunload_event.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>

<script>
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = 'Dialog';
return;
})
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('will exit even if an beforeload event dialog is present', function () {
cy.visit('/blocking_beforeunload_event.html')
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('will exit even if an beforeload event dialog is present in a child window', function () {
cy.window().invoke('open', '/blocking_beforeunload_event.html')
})
12 changes: 12 additions & 0 deletions system-tests/test/browser_crash_handling_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ describe('Browser Crash Handling', () => {
expectedExitCode: 1,
})
})

context('when the window closes mid launch of the browser process', () => {
systemTests.it('passes', {
browser: 'electron',
spec: 'abort_beforeunload_event_child.cy.ts,abort_beforeunload_event.cy.ts',
snapshot: true,
expectedExitCode: 0,
config: {
video: false,
},
})
})
})

10 comments on commit f05f623

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 29, 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/12.17.0/linux-arm64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 29, 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/12.17.0/linux-x64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 29, 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/12.17.0/darwin-x64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 29, 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/12.17.0/win32-x64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 29, 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/12.17.0/darwin-arm64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 30, 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/12.17.0/linux-arm64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 30, 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/12.17.0/linux-x64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 30, 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/12.17.0/darwin-arm64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 30, 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/12.17.0/darwin-x64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on f05f623 Jun 30, 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/12.17.0/win32-x64/develop-f05f6236b9ae86f78cc0c40e492ec04e52438d56/cypress.tgz

Please sign in to comment.