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: getting focused window with destroyed webContents #33404

Merged
merged 2 commits into from Mar 29, 2022
Merged

Conversation

codebytere
Copy link
Member

@codebytere codebytere commented Mar 23, 2022

Description of Change

Closes #33326.

It's possible that getFocusedWindow could be called while a window is being destroyed or closed - if this is the case & its webContents is destroyed, a call to that method will result in an Object has been destroyed error due to

if (window.isFocused() || window.isDevToolsFocused()) return window;
.

We fix this by ensuring that getFocusedWindow only includes windows with currently-alive webContents.

Tested with https://gist.github.com/d7433e132a56679832f37a9446d4530c

Checklist

Release Notes

Notes: Fixed a potential crash in Browser.getFocusedWindow() when child windows are closed.

@pushkin-
Copy link

Thanks for the quick PR.
In my specific case by the way, I am calling BrowserWindow::destroy() and the crash I'm getting suggests that webContents is actually null. Should we check that in addition to isDestroyed()?

Copy link
Contributor

@jkleinsc jkleinsc left a comment

Choose a reason for hiding this comment

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

Can you rebase this PR? That should resolve the failing CI.

@codebytere codebytere added the semver/patch backwards-compatible bug fixes label Mar 24, 2022
@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Mar 24, 2022
Copy link
Member

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

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

Code looks good to me.

@pushkin-
Copy link

@codebytere Following up - can you speak to my comment above. Is this issue fixed when you call win.destroy() instead of close? I'm getting a crash about webContents being null which still seems like it'd be possible with your fix.

@codebytere
Copy link
Member Author

@pushkin- yes - it should work both for closed and destroyed windows. I just added an extra safeguard, though, similar to what we have in our window test helpers

@jkleinsc jkleinsc merged commit d1ea62c into main Mar 29, 2022
@jkleinsc jkleinsc deleted the fix-destroyed-wc branch March 29, 2022 16:22
@release-clerk
Copy link

release-clerk bot commented Mar 29, 2022

Release Notes Persisted

Fixed a potential crash in Browser.getFocusedWindow() when child windows are closed.

@trop
Copy link
Contributor

trop bot commented Mar 29, 2022

I was unable to backport this PR to "16-x-y" cleanly;
you will need to perform this backport manually.

@trop
Copy link
Contributor

trop bot commented Mar 29, 2022

I was unable to backport this PR to "17-x-y" cleanly;
you will need to perform this backport manually.

@trop
Copy link
Contributor

trop bot commented Mar 29, 2022

I was unable to backport this PR to "18-x-y" cleanly;
you will need to perform this backport manually.

codebytere added a commit that referenced this pull request Mar 30, 2022
* fix: getting focused window with destroyed webContents

* fix: add extra safeguards
@electron electron deleted a comment from trop bot Mar 31, 2022
@electron electron deleted a comment from trop bot Mar 31, 2022
@codebytere
Copy link
Member Author

/trop run backport-to 18-x-y

@trop
Copy link
Contributor

trop bot commented Mar 31, 2022

The backport process for this PR has been manually initiated - sending your PR to 18-x-y!

@trop
Copy link
Contributor

trop bot commented Mar 31, 2022

I have automatically backported this PR to "18-x-y", please check out #33538

@codebytere
Copy link
Member Author

/trop run backport-to 17-x-y,16-x-y

@trop
Copy link
Contributor

trop bot commented Mar 31, 2022

The backport process for this PR has been manually initiated - sending your PR to 17-x-y!

@trop
Copy link
Contributor

trop bot commented Mar 31, 2022

The backport process for this PR has been manually initiated - sending your PR to 16-x-y!

@trop
Copy link
Contributor

trop bot commented Mar 31, 2022

I have automatically backported this PR to "17-x-y", please check out #33539

@trop
Copy link
Contributor

trop bot commented Mar 31, 2022

I have automatically backported this PR to "16-x-y", please check out #33540

bavulapati pushed a commit to bavulapati/electron that referenced this pull request Apr 29, 2022
* fix: getting focused window with destroyed webContents

* fix: add extra safeguards
khalwa pushed a commit to solarwindscloud/electron that referenced this pull request Feb 22, 2023
* fix: getting focused window with destroyed webContents

* fix: add extra safeguards
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch backwards-compatible bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: BrowserWindow.getFocusedWindow crash: Cannot read properties of null (reading 'isDevToolsFocused')
4 participants