Bug 500173

Summary: Spurious windows while generating HTML preview in Wayland
Product: [Applications] konqueror Reporter: Konstantin Ivanov <kivanov1992>
Component: generalAssignee: Konqueror Bugs <konqueror-bugs-null>
Status: ASSIGNED ---    
Severity: normal CC: 66.ghostnation, akb825, cyrond, dolphin-bugs-null, jjm, kivanov1992, nate, nicolas.fella, sitter, stefano.crocco, zawertun
Priority: NOR    
Version First Reported In: 24.12.2   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed/Implemented In: 25.04.0
Sentry Crash Report:
Attachments: Notice the Wayland icon appearing for a short time in the panel on the left, and the preview flickering in the center
A patch for kio-extras

Description Konstantin Ivanov 2025-02-16 09:52:06 UTC
Created attachment 178429 [details]
Notice the Wayland icon appearing for a short time in the panel on the left, and the preview flickering in the center

SUMMARY

When HTML file previews are generated in Dolphin under Wayland session, extra windows briefly appear. These windows have the default Wayland icon, no decorations, and contain the preview itself. They windows grab focus so using anything is nearly impossible when many previews are generated at once. This only happens in Wayland session, everything's fine in Xorg. It doesn't happen to other file types as far as I've noticed (tried text or images).


STEPS TO REPRODUCE
(I include the commands I ran in the Dolphin's terminal, but it can be reproduced in other ways, for instance, when you already have a folder with many html files such as a local website mirror)
1. Clear thumbnails cache (rm -r ~/.cache/thumbnails/)
2. Make an empty directory at least one step deep (mkdir -p test/html)
3. Create some basic html files in it (for i in {1..10}; do echo "<body>Test</body>" > test/html/test$i.html; done)
4. Open directory ./test/html/ or even ./test/ in Dolphin (cd test/html)

OBSERVED RESULT
Several windows containing rendered html appear in the middle of the screen and grab focus.

EXPECTED RESULT
Previews should be generated in the background without popping up extra windows.

SOFTWARE/OS VERSIONS
Operating System: EndeavourOS 
KDE Plasma Version: 6.3.0
KDE Frameworks Version: 6.10.0
Qt Version: 6.8.2
Kernel Version: 6.13.2-arch1-1 (64-bit)
Graphics Platform: Wayland
Processors: 4 × Intel® Core™ i5-2520M CPU @ 2.50GHz
Memory: 7.6 GiB of RAM
Graphics Processor: Mesa Intel® HD Graphics 3000


ADDITIONAL INFORMATION

I assume that this has something to do with how the preview plugin works, and it is actually made by Konqueror, and they use QWebEngineView (I base my conclusions on searching sources for "thumbcreator html" in GitHub, and I'm by no means a specialist). The closest I've come to pinpointing the bug is somewhere inside the function KIO::ThumbnailResult WebArchiveCreator::create (https://github.com/KDE/konqueror/blob/56c8860135e64a04235fc44bb609408847c0ca2c/plugins/webarchiver/thumbnailer/webarchivecreator.cpp#L91).
Maybe it's rather a Qt's roblem and not KDE's, but I have no skills to determine that :(
Comment 1 Stefano Crocco 2025-02-16 18:10:47 UTC
Yes, I can confirm that the problem comes from the thumbnailer component of the WebArchiver plugin. The thumbnailer uses a QWebEngineView to render the html page on an internal image. Unfortunately, for this to work, the view should be shown. The trick the plugin uses is to move the view outside the visible area of the screen (at coordinate 5000 x 5000). This works fine with X11, but it seems to fail with Wayland. I don't know if this is expected or three's a bug somewhere. Is this a recent issue? If so, do you remember you had just upgraded something Qt or Wayland related when it started happening? If this is the case, then maybe there's a bug in the upgraded software. If it's an older issue, then I'll need to see whether there's a way to render the web page without showing the view or if there's some other way to prevent Wayland to show the window.
Comment 2 Konstantin Ivanov 2025-02-16 20:07:34 UTC
(In reply to Stefano Crocco from comment #1)

Hello Stefano,

Thank you for the reply.

>Is this a recent issue? If so, do you remember you had just upgraded something Qt or Wayland related when it started happening?

Unfortunately, this is my first (ever) experience with Wayland and KDE, but I'm willing to try some older release. I'm guessing Kubuntu 20.04.6 would work since it was released in 2023, but if you can point me to a better alternative then I'm game.

>The trick the plugin uses is to move the view outside the visible area of the screen (at coordinate 5000 x 5000). This works fine with X11, but it seems to fail with Wayland.

I was under the impression that it's impossible for an application to move its window with Wayland (only compositor can do that), but I might be wrong.

>I'll need to see whether there's a way to render the web page without showing the view

Apparently, it's not an easy task, here's someone else struggling with it in 2016 (trying to do it WM-agnostic way): https://forum.qt.io/topic/64410/the-invisible-qapplication
Comment 3 Stefano Crocco 2025-02-16 20:25:17 UTC
Thanks for your detailed answer. I, too, don't have any experience with Wayland, but I think I now remember having read about some limitations on moving windows on Wayland. I'll look into it as soon as  I have a bit of time.

For the time being, I don't think you need to try older releases: I can try setting up some virtual machines. I was asking because if you started noticing this issue, for example, after switching to Qt 6.8, I could have started investigating the changes in that version.

I'll keep looking for ways to make previews work correctly on Wayland, too. If I can't find something quickly, I think the best thing to do right now would be to disable thumbnails on Wayland, at least for the time being, because I think this is quite an annoying bug (and I find it strange that nobody has reported it before).
Comment 4 Nicolas Fella 2025-02-16 21:16:20 UTC
This used to work correctly on Wayland. I'd suspect the problem is with a recent Qt version
Comment 5 Konstantin Ivanov 2025-02-17 18:41:29 UTC
(In reply to Nicolas Fella from comment #4)
> This used to work correctly on Wayland. I'd suspect the problem is with a
> recent Qt version

You're right, I tested it in Ubuntu 21.04, and there's no such bug in Wayland session, although I have to point out that this is on different GPU. Here's the version info:
konqueror 20.12.3
dolphin 20.12.3
----------------------
Operating System: Ubuntu 21.04
KDE Plasma Version: 5.21.4
KDE Frameworks Version: 5.80.0
Qt Version: 5.15.2
Kernel Version: 5.11.0-49-generic
OS Type: 64-bit
Graphics Platform: Wayland
Processors: 4 × Intel® Core™ i5 CPU M 450 @ 2.40GHz
Memory: 3,7 GiB of RAM
Graphics Processor: AMD REDWOOD
Comment 6 Konstantin Ivanov 2025-02-19 08:28:11 UTC
Here's an update and a possible workaround: I've posted the story about this issue on another forum (in Russian, https://www.linux.org.ru/forum/development/17886472), and a good Samaritan there suggested that it's possible to patch kio-extras to utilize Qt Platform Abstaraction forcing off-screen rendering, and he even made a patch. It's just one line in thumbnail.cpp before QApplication app declaration:

    qputenv("QT_QPA_PLATFORM", "offscreen");

IMHO it's a wise choice anyway because no thumnailer plugin should be allowed to draw on the screen. I've tested it, and it works for me (just /lib/qt6/plugins/kio/thumbnail.so needs to be recompiled).
I'm attaching a patch to this report because I'm not sure how to proceed from here. Is this a good solution? Would someone like to make a merge request? I've never done it but I can try if you approve of it.
Comment 7 Konstantin Ivanov 2025-02-19 08:29:20 UTC
Created attachment 178571 [details]
A patch for kio-extras
Comment 8 Stefano Crocco 2025-02-19 20:06:24 UTC
(In reply to Konstantin Ivanov from comment #6)
> Here's an update and a possible workaround: I've posted the story about this
> issue on another forum (in Russian,
> https://www.linux.org.ru/forum/development/17886472), and a good Samaritan
> there suggested that it's possible to patch kio-extras to utilize Qt
> Platform Abstaraction forcing off-screen rendering, and he even made a
> patch. It's just one line in thumbnail.cpp before QApplication app
> declaration:
> 
>     qputenv("QT_QPA_PLATFORM", "offscreen");
> 
> IMHO it's a wise choice anyway because no thumnailer plugin should be
> allowed to draw on the screen. I've tested it, and it works for me (just
> /lib/qt6/plugins/kio/thumbnail.so needs to be recompiled).
> I'm attaching a patch to this report because I'm not sure how to proceed
> from here. Is this a good solution? Would someone like to make a merge
> request? I've never done it but I can try if you approve of it.

Thanks for the suggestion. In my opinion, it's worth trying to do a merge request for kio-extras. As for "approving" your suggestion, I think it's better leaving it to the kio-extras people because they know the code better than I do.

In the meanwhile, I tried reproducing the bug in virtual machines with older Qt versions: I couldn't reproduce it with a version of KDE Neon using Qt 6.8.0 but it happened with a version using Qt 6.8.2, so I think it's something which happened between these two releases. Unfortunately, I think that the KDE frameworks version were different between the two cases, so I'm going to try writing a minimal Qt-only program showing this issue. I don't think I'll have time to do that before the week-end, however.
Comment 9 Nicolas Fella 2025-02-19 20:29:44 UTC
I can reproduce with this program. I haven't really tried to reproduce it without a QWebEngineView, but it might be possible

#include <QApplication>
#include <QTimer>
#include <QUrl>
#include <QWebEngineView>

int main(int argc, char **argv) {
  QApplication app(argc, argv);

  QTimer::singleShot(0, [&app] {
    const QUrl indexUrl = QUrl::fromLocalFile("/home/nico/doc/html/bluezqt-adapter.html");

    bool rendered = false;

    QWebEngineView view;
    QObject::connect(&view, &QWebEngineView::loadFinished, [&rendered] {
      QTimer::singleShot(1000, [&rendered] { rendered = true; });
    });

    view.resize(500, 500);

    view.load(indexUrl);
    view.setAttribute(Qt::WA_OutsideWSRange);
    view.setWindowFlags(view.windowFlags() | Qt::BypassWindowManagerHint |
                        Qt::FramelessWindowHint);
    view.move(5000, 5000);
    view.show();

    while (!rendered)
      qApp->processEvents(QEventLoop::WaitForMoreEvents);

    QPixmap pix(500, 500);
    pix.fill(QColor(245, 245, 245));

    view.render(&pix);
    view.hide();
    app.quit();
  });

  return app.exec();
}
Comment 10 Nicolas Fella 2025-02-19 20:30:35 UTC
Using the offscreen platform sounds like a much more robust approach than the current code, so please do make a MR
Comment 11 Bug Janitor Service 2025-02-23 09:59:40 UTC
A possibly relevant merge request was started @ https://invent.kde.org/network/kio-extras/-/merge_requests/412
Comment 12 Stefano Crocco 2025-02-23 10:33:10 UTC
(In reply to Nicolas Fella from comment #9)
> I can reproduce with this program. I haven't really tried to reproduce it
> without a QWebEngineView, but it might be possible
> 
> #include <QApplication>
> #include <QTimer>
> #include <QUrl>
> #include <QWebEngineView>
> 
> int main(int argc, char **argv) {
>   QApplication app(argc, argv);
> 
>   QTimer::singleShot(0, [&app] {
>     const QUrl indexUrl =
> QUrl::fromLocalFile("/home/nico/doc/html/bluezqt-adapter.html");
> 
>     bool rendered = false;
> 
>     QWebEngineView view;
>     QObject::connect(&view, &QWebEngineView::loadFinished, [&rendered] {
>       QTimer::singleShot(1000, [&rendered] { rendered = true; });
>     });
> 
>     view.resize(500, 500);
> 
>     view.load(indexUrl);
>     view.setAttribute(Qt::WA_OutsideWSRange);
>     view.setWindowFlags(view.windowFlags() | Qt::BypassWindowManagerHint |
>                         Qt::FramelessWindowHint);
>     view.move(5000, 5000);
>     view.show();
> 
>     while (!rendered)
>       qApp->processEvents(QEventLoop::WaitForMoreEvents);
> 
>     QPixmap pix(500, 500);
>     pix.fill(QColor(245, 245, 245));
> 
>     view.render(&pix);
>     view.hide();
>     app.quit();
>   });
> 
>   return app.exec();
> }

It seems that the issue is related to the Qt::WA_OutsideWSRange window attribute and that moving the window has no effect at all. I could reproduce the issue with the following program:

int main(int argc, char **argv) {
  QApplication app(argc, argv);
  QWidget *mw = new QWidget();
  QVBoxLayout *l = new QVBoxLayout(mw);
  mw->setLayout(l);
  QCheckBox *check = new QCheckBox("Enable WA_OutsideWSRange", mw);
  l->addWidget(check);
  QPushButton *btn = new QPushButton("show window", mw);
  l->addWidget(btn);
  auto showWindow = [&]() {
    QWidget *widget = new QWidget;
    if (check->isChecked()) {
      widget->setAttribute(Qt::WA_OutsideWSRange);
    }
    widget->show();
  };
  QObject::connect(btn, &QPushButton::clicked, mw, showWindow);
  mw->show();
  app.exec();
  return 0;
}

It just shows a check box and a button: when the button is pressed, a new toplevel widget is shown. If the checkbox is checked, the Qt::WA_OutsideWSRange attribute is set on the widget before showing it. With Qt 6.8.2, the new widget is always shown, while with Qt 6.8.0 (in the virtual machine) the widget is only shown when the checkbox is not checked. I don't know whether this is caused by the change in Qt version or in the Wayland version, since the virtual machine has version 1.23.0 while the real machine has 1.23.1.

In any case, I created a Qt bug report: https://bugreports.qt.io/browse/QTBUG-134012
Comment 13 Konstantin Ivanov 2025-02-24 14:04:05 UTC
Git commit 1fe1548a7e595107d5ed00b30b031b74ac658ed4 by Konstantin Ivanov.
Committed on 24/02/2025 at 13:45.
Pushed by nicolasfella into branch 'master'.

Use offscreen platform plugin for thumbnailers

Some thumbnailer plugins can cause spurious windows to appear (see referenced bugreport for an example).
This can be disruptive when viewing a directory with many html files in Dolphin.
The proposed solution is to use offscreen platform abstraction plugin for all thumbnailers.

M  +6    -0    thumbnail/thumbnail.cpp

https://invent.kde.org/network/kio-extras/-/commit/1fe1548a7e595107d5ed00b30b031b74ac658ed4
Comment 14 Nicolas Fella 2025-02-24 14:04:44 UTC
Git commit 0cdbd0f5d33d62a32c368e6ce30d960e484016fe by Nicolas Fella, on behalf of Konstantin Ivanov.
Committed on 24/02/2025 at 14:04.
Pushed by nicolasfella into branch 'release/24.12'.

Use offscreen platform plugin for thumbnailers

Some thumbnailer plugins can cause spurious windows to appear (see referenced bugreport for an example).
This can be disruptive when viewing a directory with many html files in Dolphin.
The proposed solution is to use offscreen platform abstraction plugin for all thumbnailers.
(cherry picked from commit 1fe1548a7e595107d5ed00b30b031b74ac658ed4)

M  +6    -0    thumbnail/thumbnail.cpp

https://invent.kde.org/network/kio-extras/-/commit/0cdbd0f5d33d62a32c368e6ce30d960e484016fe
Comment 15 Nicolas Fella 2025-02-24 14:07:22 UTC
>  I don't know whether this is caused by the change in Qt version or in the Wayland version, since the virtual machine has version 1.23.0 while the real machine has 1.23.1.

It's almost certainly the Qt change
Comment 16 Nate Graham 2025-03-05 16:27:18 UTC
*** Bug 500985 has been marked as a duplicate of this bug. ***
Comment 17 Nicolas Fella 2025-03-05 16:28:23 UTC
I reverted the change for 24.12 since it was causing issues
Comment 18 Konstantin Ivanov 2025-03-05 16:59:34 UTC
(In reply to Nicolas Fella from comment #17)
> I reverted the change for 24.12 since it was causing issues

Can you have a look at the new MR: https://invent.kde.org/network/kio-extras/-/merge_requests/415? I committed a somewhat hackish solution, and we are thinking whether it's the only possible one.
Comment 19 Nicolas Fella 2025-03-13 09:09:15 UTC
*** Bug 499778 has been marked as a duplicate of this bug. ***
Comment 20 Nicolas Fella 2025-03-25 20:55:02 UTC
Git commit e11dfd86bd075afc7ff5c5eec70bc5d2377c4168 by Nicolas Fella, on behalf of Konstantin Ivanov.
Committed on 25/03/2025 at 20:54.
Pushed by nicolasfella into branch 'master'.

Extend using offscreen platform for thumbnail creation by using original platform theme.

A previous commit 1fe1548a introduced offscreen platform to the thumbnailer but also a regression where directory thumbnails were built using the default platform theme. This results in e.g. incorrect colors being used.

To preserve the original platform theme we create an auxiliary QApplication and then set the platform theme with QT_QPA_PLATFORMTHEME environment variable.

M  +7    -4    thumbnail/CMakeLists.txt
M  +13   -0    thumbnail/thumbnail.cpp

https://invent.kde.org/network/kio-extras/-/commit/e11dfd86bd075afc7ff5c5eec70bc5d2377c4168
Comment 21 Nicolas Fella 2025-03-25 20:55:50 UTC
Git commit 4fe57a690816188ab2a182a546b6fd3f34d5fabd by Nicolas Fella, on behalf of Konstantin Ivanov.
Committed on 25/03/2025 at 20:55.
Pushed by nicolasfella into branch 'release/25.04'.

Extend using offscreen platform for thumbnail creation by using original platform theme.

A previous commit 1fe1548a introduced offscreen platform to the thumbnailer but also a regression where directory thumbnails were built using the default platform theme. This results in e.g. incorrect colors being used.

To preserve the original platform theme we create an auxiliary QApplication and then set the platform theme with QT_QPA_PLATFORMTHEME environment variable.
(cherry picked from commit e11dfd86bd075afc7ff5c5eec70bc5d2377c4168)

M  +7    -4    thumbnail/CMakeLists.txt
M  +13   -0    thumbnail/thumbnail.cpp

https://invent.kde.org/network/kio-extras/-/commit/4fe57a690816188ab2a182a546b6fd3f34d5fabd
Comment 22 Nicolas Fella 2025-03-31 09:21:36 UTC
*** Bug 502232 has been marked as a duplicate of this bug. ***
Comment 23 Konstantin Ivanov 2025-05-13 20:38:23 UTC
Should probably be reopened because of https://bugs.kde.org/show_bug.cgi?id=504126
Comment 24 Bug Janitor Service 2025-05-25 11:41:56 UTC
A possibly relevant merge request was started @ https://invent.kde.org/network/konqueror/-/merge_requests/424
Comment 25 Konstantin Ivanov 2025-06-16 07:42:25 UTC
Resolved once more: https://invent.kde.org/network/kio-extras/-/merge_requests/440