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

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

Open
TransparentLC opened this issue Mar 30, 2024 · 4 comments

Comments

@TransparentLC
Copy link
Contributor

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 f277868 and 54cd806 (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 3077861 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.

@r0x0r
Copy link
Owner

r0x0r commented Apr 17, 2024

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.

@TransparentLC
Copy link
Contributor Author

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

Copy link

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 github-actions bot added the stale label May 20, 2024
@TransparentLC
Copy link
Contributor Author

Can you reproduce it? @r0x0r

@github-actions github-actions bot removed the stale label May 21, 2024
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

No branches or pull requests

2 participants