Bug 454619

Summary: Threading-related crash in Gwenview in file KIO worker
Product: [Frameworks and Libraries] frameworks-kio Reporter: Kai Uwe Broulik <kde>
Component: generalAssignee: David Faure <faure>
Status: RESOLVED FIXED    
Severity: crash CC: faure, kdelibs-bugs
Priority: NOR    
Version: git master   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Kai Uwe Broulik 2022-05-30 17:01:35 UTC
Gwenview crashes when closing it while it's still listing directories.

STEPS TO REPRODUCE
1. Open an image in a folder with loads of images (my folder has 4000+ images) in Gwenview
2. Close Gwenview right away

OBSERVED RESULT
Boom, seems the thread doesn't exit cleanly, exit() is called but we don't wait for it to finish(?)

EXPECTED RESULT
No boom, threads exit gracefully

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: git master as of 2022-05-30

ADDITIONAL INFORMATION
Thread 1 (Thread 0x7fe77ea4d700 (LWP 75770)):
[KCrash Handler]
#3  QHashNode<KUserId, QString>::same_key (key0=..., h0=1464629782, this=0x41) at /usr/include/x86_64-linux-gnu/qt5/QtCore/qhash.h:156
#4  QHash<KUserId, QString>::findNode (h=1464629782, akey=..., this=0x7fe79fc12a30 <staticUserCache>) at /usr/include/x86_64-linux-gnu/qt5/QtCore/qhash.h:931
#5  QHash<KUserId, QString>::findNode (this=this@entry=0x7fe79fc12a30 <staticUserCache>, akey=..., ahp=ahp@entry=0x0) at /usr/include/x86_64-linux-gnu/qt5/QtCore/qhash.h:950
#6  0x00007fe79fc06f95 in QHash<KUserId, QString>::find (akey=..., this=0x7fe79fc12a30 <staticUserCache>) at /usr/include/x86_64-linux-gnu/qt5/QtCore/qrefcount.h:102
#7  getUserName (uid=...) at ./src/ioslaves/file/file_unix.cpp:174
#8  createUDSEntry (filename=..., path=..., entry=..., details=..., details@entry=..., fullPath=...) at ./src/ioslaves/file/file_unix.cpp:465
#9  0x00007fe79fc07d34 in FileProtocol::listDir (this=0x7fe76c005010, url=...) at /usr/include/x86_64-linux-gnu/qt5/QtCore/qstringbuilder.h:164
#10 0x00007fe7b7bd11b6 in KIO::SlaveBase::dispatch (this=0x7fe76c005020, command=71, data=...) at ./src/core/slavebase.cpp:1283
#11 0x00007fe7b7bd1a06 in KIO::SlaveBase::dispatchLoop (this=0x7fe76c005020) at ./src/core/slavebase.cpp:337
#12 0x00007fe7b7c9fd43 in KIO::WorkerThread::run (this=0x55bee2611130) at /usr/include/c++/9/bits/unique_ptr.h:360

It always crashes somewhere accessing the staticUserCache in file_unix.cpp

It does not happen with KIO_ENABLE_WORKER_THREADS=0
Comment 2 David Faure 2022-06-04 07:34:37 UTC
Git commit 215c03d98fb7f47cdc673178cedb9a50589767c7 by David Faure.
Committed on 03/06/2022 at 19:48.
Pushed by dfaure into branch 'master'.

kio_file: fix data race on static user cache

Now that the kio_file code runs in threads, static data leads to data
races and should be thread_local instead.

Reproduced the issue by running the new test
./jobtest multipleListRecursive
in a TSAN build, after adding QThread::msleep(50) in WorkerThread::run.
Running this test alone will actually fail the test in any case, but that
doesn't matter, TSAN tells us about the data race.
Even with this fix, it still shouts about KUser not being threadsafe,
I'll fix this in KCoreAddons separately.

M  +47   -13   autotests/jobtest.cpp
M  +1    -0    autotests/jobtest.h
M  +2    -3    src/ioslaves/file/file_unix.cpp

https://invent.kde.org/frameworks/kio/commit/215c03d98fb7f47cdc673178cedb9a50589767c7