https://bugs.kde.org/show_bug.cgi?id=527869
Bug ID: 527869
Summary: plasmashell crash in Akonadi::ItemChangeLog: Merkuro
contact AvatarImageProvider uses Akonadi from the
QQuickPixmapReader thread
Classification: Applications
Product: Merkuro
Version First 26.08.2
Reported In:
Platform: Arch Linux
OS: Linux
Status: REPORTED
Severity: crash
Priority: NOR
Component: general
Assignee: [email protected]
Reporter: [email protected]
CC: [email protected]
Target Milestone: ---
Product: Merkuro (contacts / `org.kde.merkuro.contact.applet`). Root cause in
merkuro, global-state hazard in Akonadi.
Severity: crash
## Versions
- Arch Linux, Wayland
- plasma-workspace 6.8.1, KF 6.31.0, Qt 6.12.0
- akonadi 26.08.2, merkuro 26.08.2, kdepim-addons 26.08.2
- Contacts come from a CardDAV address book (DAV groupware resource)
## Summary
plasmashell segfaults on the main thread while
`Akonadi::MonitorPrivate::emitItemsNotification` detaches a contact
`Akonadi::Item`.
The process-global, unsynchronized `Akonadi::ItemChangeLog` hash `m_addedTags`
was being modified concurrently by another thread.
The core shows that this other thread is Qt Quick's `QQuickPixmapReader`
thread, where Merkuro's `AvatarImageProvider` / `AvatarResponse` creates
Akonadi Items and `ItemFetchJob`s.
## Backtrace (crashing thread, symbolized from the core)
```
#5 QHashPrivate::Span<Node<Akonadi::ItemPrivate*, QList<Akonadi::Tag>>>::at
(this=0x2000562d6790d048) qhash.h:324
#8 QHash<Akonadi::ItemPrivate*, QList<Akonadi::Tag>>::operator[]
(this=0x562d6885ba20, key=0x7fe3c80a8e90) qhash.h:1119
#9 Akonadi::ItemChangeLog::addedTags itemchangelog.cpp:48
#10 Akonadi::ItemPrivate::ItemPrivate(const ItemPrivate&) item_p.h:194
#13 QSharedDataPointerBase<..., Akonadi::ItemPrivate>::detach_helper
qshareddata.h:338
#14-16 QSharedDataPointer<ItemPrivate>::detach / detachIfImplicit
Akonadi::Item::setParentCollection
Akonadi::MonitorPrivate::emitItemsNotification
monitor_p.cpp:1010
Akonadi::MonitorPrivate::emitNotification
monitor_p.cpp:524
Akonadi::ChangeRecorderPrivate::emitNotification
changerecorder_p.cpp:216
Akonadi::MonitorPrivate::flushPipeline
monitor_p.cpp:946
Akonadi::MonitorPrivate::dataAvailable
monitor_p.cpp:955
KJob::result -> QObject::event -> ... QCoreApplication::exec
```
The item being detached was a contact: Item id 1288, remoteId
`https://<carddav>/addressbooks/<user>/default/<uuid>.vcf`.
## Evidence from the core
1. The `m_addedTags` QHash is internally inconsistent.
`ItemChangeLog::sInstance->m_addedTags.d` = `{size = 2, numBuckets = 128,
spans = 0x7fe3c03cc9e8}`, but its only span holds one node.
2. The main thread inserted into a stale table.
The iterator returned by `tryEmplace()` is `{d = 0x7fe3c0415240 (current d),
bucket = 0xffffdaec22f55b63}`.
Decoding the bucket gives a span at `0x562d6790d048`. That span belongs to a
different `Data` (1 span, 128 buckets), and its single node, in bucket 99,
holds key `0x7fe3c80a8e90`, the new `ItemPrivate` being constructed in frame
#10.
So `findBucketWithHash()`/`insert()` ran against the old `d`, while
`++d->size` and `toIterator(d)` ran against a new `d`.
On a single thread, `QHash::tryEmplace_impl` (qhash.h:1462) always
recomputes `bucket` after reassigning `d`. Some other thread must therefore
have replaced `m_addedTags.d` during the call.
3. Akonadi runs on the QQuickPixmapReader thread.
A heap scan for `Akonadi::Session` objects found 4. Two have thread affinity
to the main thread (`QAdoptedThread`). The other two (`0x7fe3c03f6480`,
`0x7fe3c0b18d00`) have `QObjectPrivate::threadData->thread` pointing to a
`QQuickPixmapReader` object. Akonadi jobs created without a session use
`Session::defaultSession()`, a per-thread `QThreadStorage`, so jobs were
started on the pixmap reader thread.
4. Merkuro's async image provider is the only Akonadi code that can run on that
thread.
`src/contacts/avatarimageprovider.cpp` (unchanged on master):
```cpp
QQuickImageResponse *AvatarImageProvider::requestImageResponse(const QString
&id, const QSize &)
{
return new AvatarResponse(id); // runs on the
QQuickPixmapReader thread
}
AvatarResponse::AvatarResponse(const QString &itemId)
{
auto item = ContactManager::create(nullptr,
nullptr)->getItem(itemId.toInt()); // Akonadi::Item ctor/copies
if (!item.hasPayload<KContacts::Addressee>()) {
auto job = new Akonadi::ItemFetchJob(item); // job + default
Session on this thread
...
```
The Qt docs for `QQuickAsyncImageProvider::requestImageResponse` say: *"this
method may be called by multiple threads, so ensure the implementation of this
method is reentrant."*
Every `Akonadi::Item` construction, copy, detach and destruction reads and
writes `ItemChangeLog::instance()`'s plain `QHash`es (`item_p.h:189-201`,
`itemchangelog.cpp`). These have no locking, so doing this off the GUI thread
races with the GUI thread's ETM/Monitor.
## Steps to reproduce (likely)
Not reproduced on demand; it is a timing race. The conditions present at the
crash:
1. Merkuro contact applet in the panel/desktop, showing contacts that have
avatars (`image://avatar/<itemId>`).
2. Contacts synced from a CardDAV address book, so Item change notifications
keep arriving.
3. Avatar images are requested, e.g. when opening the applet or scrolling,
while a contact change notification is processed on the main thread.
## Suggested fix
Merkuro: do no Akonadi work in `requestImageResponse()`/`AvatarResponse`'s
constructor on the calling thread. For example, construct the response,
`moveToThread(qApp->thread())`, then start the lookup and `ItemFetchJob` via
`QMetaObject::invokeMethod(response, ..., Qt::QueuedConnection)`. Emit
`finished()` from there. This also stops creating a default Akonadi `Session`
per pixmap-reader thread.
Akonadi (secondary): `ItemChangeLog` makes even unrelated `Akonadi::Item`
copies on different threads unsafe, i.e. `Item` is not reentrant. At minimum
this should be documented. Possibly also guard it with a mutex, or assert/warn
when used off the thread that created `ItemChangeLog`.
--
You are receiving this mail because:
You are watching all bug changes.