pywebview icon indicating copy to clipboard operation
pywebview copied to clipboard

Custom user agent and devtool breaks again in PySide6, with segmentation fault when closing the window

Open TransparentLC opened this issue 1 year ago • 4 comments

Specification

  • pywebview version: 5.0.5
  • operating system: Windows 11/Ubuntu 22.04
  • web renderer: qt (PySide6)

I believe this issue is not related to OS. PyQt5 is not tested.

Description

pip install pywebview[pyside6]
import webview
webview.create_window('Hello world', 'https://www.cloudflare.com/cdn-cgi/trace')
webview.start(user_agent='Custom UA', debug=True, gui='qt')

The custom user agent breaks again, and the devtool also breaks. Moreover, if I close one window, then the whole program freezes and crashes. On the Linux terminal it gives a segmentation fault.

I tried moving the code of customizing user agent after os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = BrowserView.inspector_port in webview/platforms/qt.py and getting the devtool fixed. Though I don't know why it works:

        if is_webengine:
            environ_append(
                'QTWEBENGINE_CHROMIUM_settings',
                '--use-fake-ui-for-media-stream',
                '--enable-features=AutoplayIgnoreWebAudio',
            )

-        user_agent = _settings['user_agent']
-        if user_agent and is_webengine:
-            self.view.page().profile().setHttpUserAgent(user_agent)

        if _settings['debug'] and is_webengine:
            # Initialise Remote debugging (need to be done only once)
            if not BrowserView.inspector_port:
                BrowserView.inspector_port = BrowserView._get_debug_port()
                os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = BrowserView.inspector_port
        else:
            self.view.setContextMenuPolicy(
                QtCore.Qt.NoContextMenu
            )  # disable right click context menu

+        user_agent = _settings['user_agent']
+        if user_agent and is_webengine:
+            self.view.page().profile().setHttpUserAgent(user_agent)

        if is_webengine:
            if _settings['private_mode']:
                self.profile = QWebEngineProfile()

For the other two issues. I compared the commits and found the changes related to them:

A commit between f27786832c18685576dbe6ccebba79fb7d8a5245 and 54cd806ead92076dfe897c41e1d105b653470657 (I checked the repo's commit history) breaks the custom user agent:

        if is_webengine:
            if _settings['private_mode']:
                self.profile = QWebEngineProfile()
            else:
                self.profile = QWebEngineProfile('pywebview')
                self.profile.setPersistentStoragePath(_profile_storage_path)
                self.cookies = {}
                cookie_store = self.profile.cookieStore()
                cookie_store.cookieAdded.connect(self.on_cookie_added)
                cookie_store.cookieRemoved.connect(self.on_cookie_removed)

-                self.view.setPage(BrowserView.WebPage(self.view, profile=self.profile))
+            self.view.setPage(BrowserView.WebPage(self, profile=self.profile))
        elif not is_webengine and not _settings['private_mode']:
            logger.warning('qtwebkit does not support private_mode')

Commit 307786101e104c29c7c18216428a66faccd14e57 causes the segmentation fault when closing the window:

        del BrowserView.instances[self.uid]
+        self.view.page().deleteLater()
        self.close()

If I revert the above two changes then the issue is resolved. Though I don't know why it works (again).

Practicalities

  • YES I am willing to work on this issue myself.

  • NO I am prepared to support this issue financially.

TransparentLC avatar Mar 30 '24 14:03 TransparentLC

Sorry for the late response. I have not tested with PyQT6, I am still on 5. i will try to test to reproduce these issues, but meanwhile could you create a PR with your fixes?

self.view.page().deleteLater() was added to address page not being released after the window close.

r0x0r avatar Apr 17 '24 12:04 r0x0r

I can create PR for fixing the devtool. But for the other two changes I'm not sure about the revert.

TransparentLC avatar Apr 19 '24 13:04 TransparentLC

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar May 20 '24 02:05 github-actions[bot]

Can you reproduce it? @r0x0r

TransparentLC avatar May 20 '24 02:05 TransparentLC

Yes, I found the bug and fixed it in master.

r0x0r avatar Jun 06 '24 11:06 r0x0r

I tested the latest commit and user agent and devtools works now, but the segmentation fault problem still exists.

https://github.com/r0x0r/pywebview/assets/47057319/1c0bab40-a824-4a31-93cf-f4d8aca3d30c

TransparentLC avatar Jun 06 '24 12:06 TransparentLC

Alright, this one should be fixed in master as well now.

r0x0r avatar Jun 06 '24 20:06 r0x0r