Bug 446534

Summary: System Monitor segfaults in KDescendantsProxyModel::mapFromSource() when viewing process list in tree mode
Product: plasma-systemmonitor Reporter: Alek Evans <kdebugs>
Component: generalAssignee: Fabian Vogt <fabian>
Status: RESOLVED FIXED    
Severity: crash CC: ahiemstra, fabian, nate, plasma-bugs
Priority: NOR    
Version: 5.23.3   
Target Milestone: ---   
Platform: Neon Packages   
OS: Linux   
Latest Commit: Version Fixed In: 5.23.5
Attachments: Dr Konqi backtrace
Valgrind output of system monitor

Description Alek Evans 2021-12-05 21:07:13 UTC
Created attachment 144245 [details]
Dr Konqi backtrace

SUMMARY
System Monitor crashes with a segfault while viewing the process list in tree mode, the bug has been narrowed down to a child process being unable to find its parent item and thus segfaulting.

STEPS TO REPRODUCE
1. Open the system monitor
2. Open a Process list view
3. Set the Process list to tree mode

OBSERVED RESULT
It crashes in random intervals when a parent process exits before all child processes exit.

EXPECTED RESULT
The program handles this properly and uses the next available parent as its parent.

SOFTWARE/OS VERSIONS
KDE Plasma Version: 5.23.3
KDE Frameworks Version: 5.88.0
Qt Version: 5.15.3

ADDITIONAL INFORMATION
Comment 1 Alek Evans 2021-12-05 21:07:57 UTC
Created attachment 144246 [details]
Valgrind output of system monitor
Comment 2 Fabian Vogt 2021-12-06 18:25:41 UTC
I can reproduce this easily with:
1. A debug build of kitemmodels
2. Start plasma-systemmonitor, view the process list
3. Run bash -c "/bin/sleep 6 & /bin/sleep 3" until systemmonitor crashes

Result: qt.core.qabstractitemmodel.checkindex: Index QModelIndex(-1,-1,0x0,QObject(0x0)) is not valid (expected valid)
file:///usr/share/ksysguard/sensorfaces/org.kde.ksysguard.processtable/contents/ui/ProcessTableView.qml:163:17: Unable to assign [undefined] to QString
ASSERT: "sourceLastChild.isValid()" in file /home/fabian/kderepos/kitemmodels/src/core/kdescendantsproxymodel.cpp, line 534
<CRASH>
Comment 3 Nate Graham 2021-12-07 03:49:57 UTC
Thread 1 (Thread 0x7fb3076e19c0 (LWP 3254536)):
[KCrash Handler]
#4  0x00007fb2f3ce95cc in KDescendantsProxyModel::mapFromSource (this=0x55ece089a870, sourceIndex=...) at /usr/include/x86_64-linux-gnu/qt5/QtCore/qmap.h:488
#5  0x00007fb2f3ce9efe in KDescendantsProxyModelPrivate::sourceDataChanged (this=<optimized out>, topLeft=..., bottomRight=...) at ./src/core/kdescendantsproxymodel.cpp:1310
#6  0x00007fb30c044dce in QtPrivate::QSlotObjectBase::call (a=0x7ffd73d7f570, r=0x55ece089a870, this=0x55ece14bdc80) at ../../include/QtCore/../../src/corelib/kernel/qobjectdefs_impl.h:398
#7  doActivate<false> (sender=0x55ece0f705f0, signal_index=3, argv=0x7ffd73d7f570) at kernel/qobject.cpp:3886
#8  0x00007fb30c03e197 in QMetaObject::activate (sender=sender@entry=0x55ece0f705f0, m=m@entry=0x7fb30c2a8020 <QAbstractItemModel::staticMetaObject>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7ffd73d7f570) at kernel/qobject.cpp:3946
#9  0x00007fb30bfafa69 in QAbstractItemModel::dataChanged (this=this@entry=0x55ece0f705f0, _t1=..., _t2=..., _t3=...) at .moc/moc_qabstractitemmodel.cpp:557
#10 0x00007fb30bfe5348 in QSortFilterProxyModelPrivate::_q_sourceDataChanged (this=0x55ece089ab70, source_top_left=..., source_bottom_right=..., roles=...) at itemmodels/qsortfilterproxymodel.cpp:1539
#11 0x00007fb30bfe65ea in QSortFilterProxyModel::qt_static_metacall (_o=<optimized out>, _c=<optimized out>, _id=<optimized out>, _a=<optimized out>) at itemmodels/qsortfilterproxymodel.h:199
Comment 4 Fabian Vogt 2021-12-07 21:40:21 UTC
Fixed with https://invent.kde.org/plasma/libksysguard/-/merge_requests/201
Comment 5 Fabian Vogt 2021-12-09 08:25:45 UTC
Git commit a0d70929a1b5e38bd8bf61e1895321124acf03a7 by Fabian Vogt.
Committed on 09/12/2021 at 07:36.
Pushed by fvogt into branch 'master'.

Handle process parent changes in ProcessDataModel

When the PPID of a process changes, it moves around in the model, changing the
layout. This needs to be announced properly, otherwise users of the model get
confused, leading to weird behaviour and crashes.

The added code is pretty much a direct copy from ProcessModel.

M  +29   -0    processcore/process_data_model.cpp

https://invent.kde.org/plasma/libksysguard/commit/a0d70929a1b5e38bd8bf61e1895321124acf03a7
Comment 6 Fabian Vogt 2021-12-09 08:26:12 UTC
Git commit 311faef0ef0e5f60eebed2a5a00c43f5cb60aab1 by Fabian Vogt.
Committed on 09/12/2021 at 08:26.
Pushed by fvogt into branch 'Plasma/5.23'.

Handle process parent changes in ProcessDataModel

When the PPID of a process changes, it moves around in the model, changing the
layout. This needs to be announced properly, otherwise users of the model get
confused, leading to weird behaviour and crashes.

The added code is pretty much a direct copy from ProcessModel.


(cherry picked from commit a0d70929a1b5e38bd8bf61e1895321124acf03a7)

M  +29   -0    processcore/process_data_model.cpp

https://invent.kde.org/plasma/libksysguard/commit/311faef0ef0e5f60eebed2a5a00c43f5cb60aab1