Skip to content

fix: WebContents.capturePage can freeze browser process - #27892

Closed
CezaryKulakowski wants to merge 1 commit into
electron:masterfrom
CezaryKulakowski:ckulakowski/capturePagefix
Closed

CezaryKulakowski wants to merge 1 commit into
electron:masterfrom
CezaryKulakowski:ckulakowski/capturePagefix

Conversation

@CezaryKulakowski

Copy link
Copy Markdown
Contributor

When BrowserWindow.webContents.capturePage is called on hidden
page we try to resolve its promise on the worker thread instead of
the main thread of the browser process. It happens because when page
is hidden compositor calls the callback OnCapturePageDone immediately
without posting it to the main thread). When it happens worker thread
dead locks as it tries to acquire lock kept by the main thread (the
lock which guards the promise returned by capturePage).

This change fixes the problem by assuring that promise is always
resolved on the main thread of the browser process.

Description of Change

Checklist

Release Notes

Notes: Fixed possible dead lock on call to WebContents.capturePage

When BrowserWindow.webContents.capturePage is called on hidden
page we try to resolve its promise on the worker thread instead of
the main thread of the browser process. It happens because when page
is hidden compositor calls the callback OnCapturePageDone immediately
without posting it to the main thread). When it happens worker thread
dead locks as it tries to acquire lock kept by the main thread (the
lock which guards the promise returned by capturePage).

This change fixes the problem by assuring that promise is always
resolved on the main thread of the browser process.
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Feb 24, 2021
@CezaryKulakowski

Copy link
Copy Markdown
Contributor Author

Fixes #27891.

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice find, LGTM

Failing tests on mac are related.

@CezaryKulakowski

Copy link
Copy Markdown
Contributor Author

Test fails on mac as it looks like webContents.on('hide') is not called after call to win.hide(). I'll look at it during the weekend.

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Mar 3, 2021
expect(imgBuffer[25]).to.equal(6);
});

it('does not freeze browser process when called on hidden page', async() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you also fix the lint error?

/home/builduser/project/src/electron/spec-main/api-browser-window-spec.ts
  1357:75  error  Missing space before function parentheses  space-before-function-paren
  1363:22  error  There should be no space after this paren  space-in-parens

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@zcbenz we should go with this fix: #27883 (see my comment in that pull request).

@zcbenz

zcbenz commented Mar 9, 2021

Copy link
Copy Markdown
Contributor

Closing in favor of #27883.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants