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: electron crashing prematurely when window closes prematurely while browser launch process is ongoing #27167

Merged
merged 3 commits into from
Jun 29, 2023
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
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).
- 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).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change


**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,
},
})
})
})