Bug 446272

Summary: KRun opens URLs twice due to XDG activation integration breaking ApplicationLauncherJob::waitForStarted
Product: [Frameworks and Libraries] frameworks-kio Reporter: Fabian Vogt <fabian>
Component: generalAssignee: KIO Bugs <kio-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: aleixpol, kdelibs-bugs
Priority: NOR    
Version: 5.88.0   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Fabian Vogt 2021-11-30 13:05:25 UTC
Reproducer:

#include <QApplication>
#include <QDialog>
#include <KRun>

int main(int argc, char *argv[])
{
        QApplication app(argc, argv);
        QDialog dialog;
        dialog.show();
        new KRun(QUrl(app.arguments()[1]), NULL);
        return app.exec();
}

Run in a wayland session and pass a url, e.g. https://kde.org. The result is that https://kde.org is opened twice, sometimes even in different applications...

What happens is this:

KRun::init() does:

        if (d->runExternalBrowser(d->m_externalBrowser)) {
            return;
        }
        // Scan file to get more info
        
The issue is that runExternalBrowser fails, despite actually spawning the process sucessfully, and so it goes into the fallback path in addition.

KRunPrivate::runExternalBrowser calls KRun::runApplication:

    KIO::ApplicationLauncherJob *job = new KIO::ApplicationLauncherJob(servicePtr);
    job->setUrls(urls);
    ....
    job->start();
    job->waitForStarted();
    return job->error() ? 0 : job->pid();

This fails because job->waitForStarted(); does not wait for the XDG activation token, and so the process wasn't started yet and doesn't have a pid set.

With a debug build of KIO, there's even an assert failure:

ASSERT failure in QVector<T>::at: "index out of range", file /usr/include/qt5/QtCore/qvector.h, line 449

Thread 1 "kruntest" received signal SIGABRT, Aborted.
0x00007ffff5d1329c in __pthread_kill_implementation () from /lib64/libc.so.6
(gdb) bt
#0  0x00007ffff5d1329c in __pthread_kill_implementation () from /lib64/libc.so.6
#1  0x00007ffff5cc3366 in raise () from /lib64/libc.so.6
#2  0x00007ffff5cac7d3 in abort () from /lib64/libc.so.6
#3  0x00007ffff62fbfe7 in QMessageLogger::fatal(char const*, ...) const () from /lib64/libQt5Core.so.5
#4  0x00007ffff62fb4ca in qt_assert_x(char const*, char const*, char const*, int) () from /lib64/libQt5Core.so.5
#5  0x00007ffff6cea1c1 in QVector<long long>::at (this=0x75eaf0, i=0) at /usr/include/qt5/QtCore/qvector.h:449
#6  0x00007ffff6ce74e1 in KIO::ApplicationLauncherJob::pid (this=0x767820) at /home/fabian/kderepos/kio/src/gui/applicationlauncherjob.cpp:237
#7  0x00007ffff7eeb888 in KRun::runApplication (service=..., urls=QList<QUrl> (size = 1) = {...}, window=0x0, flags=..., flags@entry=..., suggestedFileName="", asn=...) at /usr/src/debug/kio-5.88.0-1.1.x86_64/src/widgets/krun.cpp:294
#8  0x00007ffff7eec168 in KRunPrivate::runExternalBrowser (_exec="vivaldi-stable.desktop", this=0x49bf70) at /usr/include/qt5/QtCore/qshareddata.h:159
#9  KRun::init (this=0x745b60) at /usr/src/debug/kio-5.88.0-1.1.x86_64/src/widgets/krun.cpp:427
#10 0x00007ffff6552043 in ?? () from /lib64/libQt5Core.so.5
Comment 1 Bug Janitor Service 2021-11-30 16:47:51 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/kio/-/merge_requests/645
Comment 2 Aleix Pol 2021-12-02 00:20:13 UTC
Git commit 25f3a6937a80f2748790265b9b688d64126e43d0 by Aleix Pol Gonzalez, on behalf of Aleix Pol.
Committed on 01/12/2021 at 15:53.
Pushed by apol into branch 'master'.

Fix KRun::runApplication when xdg activation is involved

Has waitForStarted account for xdg activation token request.

M  +10   -3    src/gui/kprocessrunner.cpp
M  +1    -0    src/gui/kprocessrunner_p.h

https://invent.kde.org/frameworks/kio/commit/25f3a6937a80f2748790265b9b688d64126e43d0