https://bugs.kde.org/show_bug.cgi?id=528715

            Bug ID: 528715
           Summary: konsolepart:
                    terminalProcessId()/foregroundProcessName() crash with
                    null activeSession() after the program exits, before
                    the part is deleted
    Classification: Applications
           Product: konsole
      Version First 25.12.3
       Reported In:
          Platform: TuxedoOS
                OS: Linux
            Status: REPORTED
          Severity: crash
          Priority: NOR
         Component: kpart
          Assignee: [email protected]
          Reporter: [email protected]
  Target Milestone: ---

Hello,

I am writing a Qt/KDE application that embeds konsolepart and uses the
TerminalInterface. Sometimes the application crashes when the program in the
terminal exits.

The crash is in Konsole::Session::processId(), called from
Part::terminalProcessId(). My application calls terminalProcessId() regularly
to see if the program still runs. In my application it happens rarely, because
I do not ask in every event loop iteration. The attached program asks in every
iteration, and then it crashes every time (10 of 10 runs, also without load).

I think the reason is this. When the program exits, the session is deleted
first, but the part lives a little longer (it deletes itself with
deleteLater()). In this short time activeSession() returns nullptr, but
Part::terminalProcessId() does not check it:

int Part::terminalProcessId()
{
    Q_ASSERT(activeSession());
    return activeSession()->processId();
}

The Q_ASSERT does nothing in a release build, so processId() is called on a
null pointer. The same is in foregroundProcessId(), foregroundProcessName() and
sendInput(). I checked master, the code is the same there.

The documentation of TerminalInterface says for terminalProcessId(): "If no
process is currently running, returns 0." So I would expect 0 here and not a
crash. And the part has no signal which I could use to stop asking before this
happens.

Steps to reproduce:
Build and run the attached program (konsolepart-crash.tar.gz):
QT_QPA_PLATFORM=offscreen ./konsolepart-crash
It crashes every time. Without the terminalProcessId() call it ends normally (5
of 5 runs).

Backtrace (full output in backtrace.txt in the attachment):

#0  Konsole::Session::processId() const () at
/lib/x86_64-linux-gnu/libkonsoleprivate.so.25.12.3
#1  ??? () at /lib/x86_64-linux-gnu/libQt6Core.so.6
#2  QTimer::timeout(QTimer::QPrivateSignal) () at
/lib/x86_64-linux-gnu/libQt6Core.so.6

Part::terminalProcessId() is not in the backtrace because it ends in a tail
call. I have no debug symbols for Konsole, the package is a TUXEDO rebuild.

Maybe related: bug 463075 (Dolphin, same crash place, closed as "likely fixed")
and bug 342216 ("exit" and F4 at the same time in Dolphin).

A simple fix could be to return 0 (or an empty string) when activeSession() is
nullptr.

In my application I now check myself if the process still lives before I call
the part.

Konsole 25.12.3, Qt 6.10.2, KDE Frameworks 6.24, TUXEDO OS (Kubuntu based),
Wayland.

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to