[webengine] fix intermittent "unknown error while getting elements" on Qt 6.11
Partial fix for #8925
On Qt 6.11, window._qutebrowser is sometimes undefined when find_css runs. You can verify it mid-bug with :jseval --world=application typeof window._qutebrowser which sometimes returns "undefined". The DocumentCreation injection isn't firing after forward/backward navigation.
Because find_css blindly assumes webelem.js has been injected, the JS throws a TypeError which gets handed back by Qt as None.
This fix bundles the wrap_global init with the find_css call in one runJavaScript instead of relying on the injection having already run, and the init has an initialized["scripts"] guard so it's a no-op if the injection did work.
Note that this only fixes hints. Scroll, stylesheets, and caret are probably broken for the same reason but at different call sites; I'm not sure if you'd rather fix those the same way or go a different direction, but this fix has been working for me.
Thanks, this solved the issue for me!
qutebrowser v3.7.0 Git commit: Backend: QtWebEngine 6.11 based on Chromium 140.0.7339.225 with security patches up to 146.0.7680.80 (plus any distribution patches) (source: api) Qt: 6.11.0
This is trully amazing! With this fix I am unable to reproduce the issue (unknown error while getting ..) in single-process too, for instance https://github.com/qutebrowser/qutebrowser/issues/5569#issuecomment-2859809548 I was always able to trigger this behavior (unknwn error) on single-process and rarely on per-site or per-site-instance. That means I'm back on single-process instance, no more endless QtWebengineProcess trees and that is trully exciting. Thank you!
Thanks, this solved the issue for me!
qutebrowser v3.7.0 Git commit: Backend: QtWebEngine 6.11 based on Chromium 140.0.7339.225 with security patches up to 146.0.7680.80 (plus any distribution patches) (source: api) Qt: 6.11.0
Albeit the bug is not fully fixed.
Upon loading a page (most commonly occurs when a page is opened in background, without being focused even once) it sometimes still has the bug occur. Pressing f<Esc> or refreshing the page fixes it.
Symptoms:
-
a scrollbar appears on the page, despite
scrolling.barbeing set tonever -
page navigation keys (hjkl, space) don't work, only standard Chromium PageUp/PageUp and Shift+Space. Mouse controls work fine.
-
console spews the following upon any navigation keypress:
Uncaught TypeError: Cannot read properties of undefined (reading 'scroll') at anonymous:2:21
The script in question looks like this:
"use strict"; window._qutebrowser.scroll.delta_page(0.0, 0.15);
This is trully amazing! With this fix I am unable to reproduce the issue (unknown error while getting ..) in single-process too, for instance #5569 (comment) I was always able to trigger this behavior (unknwn error) on single-process and rarely on per-site or per-site-instance. That means I'm back on single-process instance, no more endless QtWebengineProcess trees and that is trully exciting. Thank you!
I'm really glad it's working for you! The single-process finding is interesting, and I'm happy that the fix covers that case as well.
Uncaught TypeError: Cannot read properties of undefined (reading 'scroll')
Yes, as expected, I believe it is the same root cause. The scroll/other calls make bare runJavaScript calls that assume window._qutebrowser.scroll was already initialized by injection, and Qt 6.11 is skipping on background loaded pages.
I kept this PR scoped to find_css since this was the most visible symptom to me, but I assume the fix is just applying this change to all other window._qutebrowser.<modules> call sites. I'm happy to expand this to the other webelem functions in one go, I just want to hear from a maintainer on whether they'd prefer that in this PR or a separate one.
This worked for me! thanks so much
This worked for me! thanks so much
Glad to hear it!
@The-Compiler would love to get your eyes on this when you have a chance :)
I wanted to give this a closer look, but let me give you some thoughts before that: It's nice to have a workaround for an annoying problem, but at the same time it's quite a hack.
Have you explored the (IMHO much cleaner) workaround from https://github.com/qutebrowser/qutebrowser/issues/7662#issuecomment-1520674002 to see if that helps too? If so I'd prefer that.
If loading the whole script for every invocation is really needed, given that this is only a workaround needed with Qt 6.11.0 (and maybe .1), it should be limited to the affected versions (via version.qtwebengine_versions()) and have a # WORKAROUND comment linking to an issue.
It should probably also use javascript.wrap_global() instead of hand-rolling things, to avoid running things more times than needed. It's been a while since I wrote that code though, so I don't remember the details off-hand.
Finally, I'm worried about any global state that could not be working properly when doing things that way, so that's something I want to look into before merging this.
If this stays in for longer, I'd rather refactor the scripts entirely so that they don't rely on window._qutebrowser at all anymore (given that now everything is self-contained). I'm not convinced it's a good idea though, and it probably shouldn't be part of this PR.
@The-Compiler Thanks for the detailed feedback! Just as a few notes though:
I didn't consider/haven't explored the profile().scripts() approach from #7662 when writing this. I noticed the initialization/call ordering problem and fixed it at the call site. I'm happy to explore that direction if you think it's cleaner, though I did notice you described it as a hack with open questions around deduplication and shared state.
With that in mind, this PR does use wrap_global(), so scripts won't run more than once per page if injection did happen correctly.
I'd argue this fix is probably cleaner because it makes each call site self-contained, rather than relying on prior injection having succeeded, it doesn't require deduplication logic, and as I mentioned, we use wrap_global() to ensure no redundant execution on pages where injection worked correctly. I believe the profile().scripts() approach relies on Qt's injection mechanism at a different level, so if that regresses, nothing ends up being gained.
I'm happy to add a workaround comment and a version gate if you'd prefer to keep this scoped to the Qt versions that are affected though!
Have you explored the (IMHO much cleaner) workaround from https://github.com/qutebrowser/qutebrowser/issues/7662#issuecomment-1520674002 to see if that helps too? If so I'd prefer that.
I tested that patch on Qt 6.11.0 and it completely fixes the issue, while this PR only does so for the hints.
Have you explored the (IMHO much cleaner) workaround from #7662 (comment) to see if that helps too? If so I'd prefer that.
I made these edits and it worked for me. Although, I don't understand why a post from 2023 addresses an issue that only recently surfaced for me.
Arch Linux qutebrowser 3.7.0-1 qt6-webengine 6.11.1-4 python-pyqt6-webengine 6.11.0-1
Have you explored the (IMHO much cleaner) workaround from #7662 (comment) to see if that helps too? If so I'd prefer that.
Strange that it's working for others, but for me the linked workaround did not resolve the hint issue, while this PR did. I tested it on two machines, though the setup is the same:
* Gentoo
* qutebrowser-3.7.0
* pyqt6-webengine-6.11.0
* qtwebengine-6.11.1