https://bugs.kde.org/show_bug.cgi?id=528656
Bug ID: 528656
Summary: Race in KGameItem/KGameImageProvider causes garbled
sprites and heap corruption (e.g. in KNetWalk's "New")
Classification: Frameworks and Libraries
Product: libkdegames
Version First unspecified
Reported In:
Platform: Gentoo Packages
OS: Linux
Status: REPORTED
Severity: crash
Priority: NOR
Component: general
Assignee: [email protected]
Reporter: [email protected]
Target Milestone: ---
Created attachment 198320
--> https://bugs.kde.org/attachment.cgi?id=198320&action=edit
knetwalk-ok.png
DESCRIPTION
Clicking "New" in KNetWalk sometimes leads to messed up computer/server icons
or crashes. However, the root bug is in libkdegames.
STEPS TO REPRODUCE
1. Open KNetWalk
2. Wait 250ms
3. Click on "New" to start a new game
OBSERVED RESULT
See attached knetwalk-bug{1,2,3}.png.
EXPECTED RESULT
See attached knetwalk-ok.png.
SYSTEM
Operating System: Gentoo Linux (x86_64)
KDE Plasma Version: 6.8.2
KDE Frameworks Version: 6.31.0
KDE Gear 26.08.3
Qt Version: 6.11.3
Kernel Version: 7.3.3-dist-hardened (64-bit)
Graphics Platform: Wayland
Processors: 28 × Intel Core i7-14700K
Memory: 96 GiB of ECC RAM
Graphics Processor: Intel Graphics
RELATED BUGS
- https://bugs.kde.org/show_bug.cgi?id=486016 (crash on "New" with glibc heap
errors): the same race. Its most detailed crash log (attachment 170470) shows
the loader thread aborting in malloc inside libKDEGames6 while the main thread
draws the SVG from QQuickPixmap::load, i.e. a synchronous load.
- https://bugs.kde.org/show_bug.cgi?id=514486 (pieces shown at reduced size
after a new game): the same race. In its screenshot, the server is drawn as a
tiny globe in the corner of a box; the reporter's workaround, switching the
theme, works because it makes every sprite get drawn again.
- https://bugs.kde.org/show_bug.cgi?id=352039 (icons bigger in the first game
after launch, smaller after "New"): a separate knetwalk bug, fixed by patch 3
(see "Fix" in the analysis below).
ADDITIONAL INFORMATION
This is a non-deterministic issue I sometimes experience.
When opening KNetWalk everything looks fine, but if I wait ~250ms and click
"New" to start a new game, sometimes graphical issues appear, such as
incorrectly
sized computer/server icons and/or white backgrounds (see screenshots).
More rarely, the game would crash due to glibc assertions, such as:
- corrupted double-linked list
- corrupted size vs. prev_size
- double free or corruption (!prev)
- free(): corrupted unsorted chunks
- malloc(): invalid next size (unsorted)
- malloc(): invalid size (unsorted)
- malloc(): unaligned tcache chunk detected
QtSvg may also warn:
```
qt.svg.draw: Shape of type 10 ignored because it will take too long to
rasterize (bounding rect= QRectF(-1.17671e+308,1.48428e-308
1.01581e+307x1.52856e-309))
```
Note that I don't even have to click on any cells. I simply click on "New".
Because the crashes are rare, I have patched knetwalk to start new games every
250ms by itself. Spawning multiple short-lived (2s) instances leads to frequent
crashes. I have attached `stack-trace.txt`.
Recompiling knetwalk (unpatched), qtsvg, and libkdegames with
CXXFLAGS=CFLAGS="-O2 -pipe -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer
-ggdb3 -fsanitize=thread" and running knetwalk over and over until the
graphical glitches appear exposes the problem:
```
WARNING: ThreadSanitizer: data race (pid=3354710)
Read of size 8 at 0x7220000a4b70 by main thread:
#0 QArrayDataPointer<QTransform>::end()
/usr/include/qt6/QtCore/qarraydatapointer.h:114 (libQt6Svg.so.6+0x84633)
(BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#1 QList<QTransform>::end() /usr/include/qt6/QtCore/qlist.h:713
(libQt6Svg.so.6+0x84633)
#2 QList<QTransform>::last() /usr/include/qt6/QtCore/qlist.h:735
(libQt6Svg.so.6+0x84633)
#3 QList<QTransform>::takeLast() /usr/include/qt6/QtCore/qlist.h:647
(libQt6Svg.so.6+0x84633)
#4 QStack<QTransform>::pop() /usr/include/qt6/QtCore/qstack.h:20
(libQt6Svg.so.6+0x84633)
#5 QSvgTransformStyle::revert(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgstyle.cpp:474
(libQt6Svg.so.6+0x84633)
#6 QSvgStaticStyle::revert(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgstyle.cpp:698
(libQt6Svg.so.6+0x849d9) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#7 QSvgNode::revertStyle(QPainter*, QSvgExtraStates&) const
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgnode.cpp:291
(libQt6Svg.so.6+0x87a38) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#8 QSvgNode::draw(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgnode.cpp:109
(libQt6Svg.so.6+0x87a38)
#9 QSvgG::drawCommand(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgstructure.cpp:44
(libQt6Svg.so.6+0x892ca) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#10 QSvgNode::draw(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgnode.cpp:105
(libQt6Svg.so.6+0x87a11) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#11 QSvgDocument::draw(QPainter*, QString const&, QRectF const&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgdocument.cpp:318
(libQt6Svg.so.6+0x93e9d) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#12 QSvgRenderer::render(QPainter*, QString const&, QRectF const&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgrenderer.cpp:497
(libQt6Svg.so.6+0x94464) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#13 KGameImageProvider::requestImage(QString const&, QSize*, QSize const&)
/usr/src/debug/kde-apps/libkdegames-26.08.3/libkdegames-26.08.3/src/kgameimageprovider.cpp:54
(libKDEGames6.so.6+0x71d43) (BuildId: 3d2ee82cce01ba89ea79122247a2d408b317a13e)
#14 createPixmapDataSync
/usr/src/debug/dev-qt/qtdeclarative-6.11.3/qtdeclarative-everywhere-src-6.11.3/src/quick/util/qquickpixmapcache.cpp:1599
(libQt6Quick.so.6+0x3548aa) (BuildId: 5e5c7a00364fc1bcdbe5b0966befb12586bd2ddb)
#15 QQuickPixmap::load(QQmlEngine*, QUrl const&, QRect const&, QSize
const&, QFlags<QQuickPixmap::Option>, QQuickImageProviderOptions const&, int,
int, double)
/usr/src/debug/dev-qt/qtdeclarative-6.11.3/qtdeclarative-everywhere-src-6.11.3/src/quick/util/qquickpixmapcache.cpp:1942
(libQt6Quick.so.6+0x3548aa)
#16 MainWindow::startNewGame()
/usr/src/debug/kde-apps/knetwalk-26.08.3/knetwalk-26.08.3/src/mainwindow.cpp:203
(knetwalk+0x20d6b) (BuildId: cc86056b50b95acc7331277481939713ecdae4db)
#17 QtPrivate::FunctorCall<std::integer_sequence<unsigned long>,
QtPrivate::List<>, void, void (MainWindow::*)()>::call(void (MainWindow::*)(),
MainWindow*, void**) /usr/include/qt6/QtCore/qobjectdefs_impl.h:128
(knetwalk+0x23b09) (BuildId: cc86056b50b95acc7331277481939713ecdae4db)
#18 void QtPrivate::FunctionPointer<void
(MainWindow::*)()>::call<QtPrivate::List<>, void>(void (MainWindow::*)(),
MainWindow*, void**) /usr/include/qt6/QtCore/qobjectdefs_impl.h:175
(knetwalk+0x23b09)
#19 QtPrivate::QCallableObject<void (MainWindow::*)(), QtPrivate::List<>,
void>::impl(int, QtPrivate::QSlotObjectBase*, QObject*, void**, bool*)
/usr/include/qt6/QtCore/qobjectdefs_impl.h:546 (knetwalk+0x23b09)
#20 QtPrivate::QSlotObjectBase::call(QObject*, void**)
/usr/src/debug/dev-qt/qtbase-6.11.3/qtbase-everywhere-src-6.11.3/src/corelib/kernel/qobjectdefs_impl.h:462
(libQt6Core.so.6+0x329c74) (BuildId: 683e1370d8c750c495e86c26712c4e93f51464db)
#21 void doActivate<false>(QObject*, int, void**)
/usr/src/debug/dev-qt/qtbase-6.11.3/qtbase-everywhere-src-6.11.3/src/corelib/kernel/qobject.cpp:4393
(libQt6Core.so.6+0x329c74)
#22 __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:59
(libc.so.6+0x27580) (BuildId: 54d2f2753c75230b3fdf433c7dd7150752b1c3e8)
Previous write of size 8 at 0x7220000a4b70 by thread T18:
#0 QtPrivate::QGenericArrayOps<QTransform>::eraseLast()
/usr/include/qt6/QtCore/qarraydataops.h:623 (libQt6Svg.so.6+0x846a1) (BuildId:
818c963e76a94738c2d33f9d7a60c6c20b563902)
#1 QList<QTransform>::takeLast()::{lambda()#1}::operator()() const
/usr/include/qt6/QtCore/qlist.h:646 (libQt6Svg.so.6+0x846a1)
#2 QScopeGuard<QList<QTransform>::takeLast()::{lambda()#1}>::~QScopeGuard()
/usr/include/qt6/QtCore/qscopeguard.h:47 (libQt6Svg.so.6+0x846a1)
#3 QScopeGuard<QList<QTransform>::takeLast()::{lambda()#1}>::~QScopeGuard()
/usr/include/qt6/QtCore/qscopeguard.h:44 (libQt6Svg.so.6+0x846a1)
#4 QList<QTransform>::takeLast() /usr/include/qt6/QtCore/qlist.h:648
(libQt6Svg.so.6+0x846a1)
#5 QStack<QTransform>::pop() /usr/include/qt6/QtCore/qstack.h:20
(libQt6Svg.so.6+0x846a1)
#6 QSvgTransformStyle::revert(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgstyle.cpp:474
(libQt6Svg.so.6+0x846a1)
#7 QSvgStaticStyle::revert(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgstyle.cpp:698
(libQt6Svg.so.6+0x849d9) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#8 QSvgNode::revertStyle(QPainter*, QSvgExtraStates&) const
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgnode.cpp:291
(libQt6Svg.so.6+0x87a38) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#9 QSvgNode::draw(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgnode.cpp:109
(libQt6Svg.so.6+0x87a38)
#10 QSvgG::drawCommand(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgstructure.cpp:44
(libQt6Svg.so.6+0x892ca) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#11 QSvgNode::draw(QPainter*, QSvgExtraStates&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgnode.cpp:105
(libQt6Svg.so.6+0x87a11) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#12 QSvgDocument::draw(QPainter*, QString const&, QRectF const&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgdocument.cpp:318
(libQt6Svg.so.6+0x93e9d) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#13 QSvgRenderer::render(QPainter*, QString const&, QRectF const&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgrenderer.cpp:497
(libQt6Svg.so.6+0x94464) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#14 KGameImageProvider::requestImage(QString const&, QSize*, QSize const&)
/usr/src/debug/kde-apps/libkdegames-26.08.3/libkdegames-26.08.3/src/kgameimageprovider.cpp:54
(libKDEGames6.so.6+0x71d43) (BuildId: 3d2ee82cce01ba89ea79122247a2d408b317a13e)
#15 QQuickPixmapReader::processJob(QQuickPixmapReply*, QUrl const&, QString
const&, QQmlImageProviderBase::ImageType, QSharedPointer<QQuickImageProvider>
const&)
/usr/src/debug/dev-qt/qtdeclarative-6.11.3/qtdeclarative-everywhere-src-6.11.3/src/quick/util/qquickpixmapcache.cpp:932
(libQt6Quick.so.6+0x3517df) (BuildId: 5e5c7a00364fc1bcdbe5b0966befb12586bd2ddb)
#16 QQuickPixmapReader::processJobs()
/usr/src/debug/dev-qt/qtdeclarative-6.11.3/qtdeclarative-everywhere-src-6.11.3/src/quick/util/qquickpixmapcache.cpp:888
(libQt6Quick.so.6+0x3517df)
Location is heap block of size 120 at 0x7220000a4b00 allocated by main
thread:
#0 operator new(unsigned long) <null> (libtsan.so.2+0xa98a3) (BuildId:
ab5c6d10a561ee9ef5482d42958a77e92971c60b)
#1 parseTransform
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvghandler.cpp:1117
(libQt6Svg.so.6+0xa19d5) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#2 parseStyle
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvghandler.cpp:1498
(libQt6Svg.so.6+0xa19d5)
#3 QSvgHandler::startElement(QStringView, QXmlStreamAttributes const&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvghandler.cpp:3902
(libQt6Svg.so.6+0xa4e93) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#4 QSvgHandler::parse()
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvghandler.cpp:3797
(libQt6Svg.so.6+0xa9e7b) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#5 QSvgHandler::init()
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvghandler.cpp:3662
(libQt6Svg.so.6+0xaabbf) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#6 QSvgHandler::QSvgHandler(QIODevice*, QFlags<QtSvg::Option>,
QtSvg::AnimatorType)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvghandler.cpp:3633
(libQt6Svg.so.6+0xac67a) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#7 QSvgDocument::load(QByteArray const&, QFlags<QtSvg::Option>,
QtSvg::AnimatorType)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgdocument.cpp:231
(libQt6Svg.so.6+0xac67a)
#8 QSvgDocument::load(QString const&, QFlags<QtSvg::Option>,
QtSvg::AnimatorType)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgdocument.cpp:197
(libQt6Svg.so.6+0xad016) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#9 loadDocument<QString>
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgrenderer.cpp:414
(libQt6Svg.so.6+0xad51b) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#10 QSvgRenderer::load(QString const&)
/usr/src/debug/dev-qt/qtsvg-6.11.3/qtsvg-everywhere-src-6.11.3/src/svg/qsvgrenderer.cpp:436
(libQt6Svg.so.6+0xad9f1) (BuildId: 818c963e76a94738c2d33f9d7a60c6c20b563902)
#11 KGameImageProvider::reloadRenderer()
/usr/src/debug/kde-apps/libkdegames-26.08.3/libkdegames-26.08.3/src/kgameimageprovider.cpp:24
(libKDEGames6.so.6+0x7169f) (BuildId: 3d2ee82cce01ba89ea79122247a2d408b317a13e)
#12 KGameImageProvider::KGameImageProvider(KGameThemeProvider*)
/usr/src/debug/kde-apps/libkdegames-26.08.3/libkdegames-26.08.3/src/kgameimageprovider.cpp:19
(libKDEGames6.so.6+0x718a4) (BuildId: 3d2ee82cce01ba89ea79122247a2d408b317a13e)
#13 KGameThemeProvider::setDeclarativeEngine(QString const&, QQmlEngine*)
/usr/src/debug/kde-apps/libkdegames-26.08.3/libkdegames-26.08.3/src/kgamethemeprovider.cpp:344
(libKDEGames6.so.6+0x7e4a2) (BuildId: 3d2ee82cce01ba89ea79122247a2d408b317a13e)
#14 GameView::GameView(QWidget*)
/usr/src/debug/kde-apps/knetwalk-26.08.3/knetwalk-26.08.3/src/gameview.cpp:36
(knetwalk+0x172e0) (BuildId: cc86056b50b95acc7331277481939713ecdae4db)
#15 MainWindow::MainWindow(QWidget*)
/usr/src/debug/kde-apps/knetwalk-26.08.3/knetwalk-26.08.3/src/mainwindow.cpp:65
(knetwalk+0x21969) (BuildId: cc86056b50b95acc7331277481939713ecdae4db)
#16 main
/usr/src/debug/kde-apps/knetwalk-26.08.3/knetwalk-26.08.3/src/main.cpp:89
(knetwalk+0xc698) (BuildId: cc86056b50b95acc7331277481939713ecdae4db)
Thread T18 'QQuickPixmapRea' (tid=3354736, running) created by main thread
at:
#0 pthread_create <null> (libtsan.so.2+0x59987) (BuildId:
ab5c6d10a561ee9ef5482d42958a77e92971c60b)
#1 QThread::start(QThread::Priority)
/usr/src/debug/dev-qt/qtbase-6.11.3/qtbase-everywhere-src-6.11.3/src/corelib/thread/qthread_unix.cpp:891
(libQt6Core.so.6+0x23ddc2) (BuildId: 683e1370d8c750c495e86c26712c4e93f51464db)
#2 __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:59
(libc.so.6+0x27580) (BuildId: 54d2f2753c75230b3fdf433c7dd7150752b1c3e8)
SUMMARY: ThreadSanitizer: data race
/usr/include/qt6/QtCore/qarraydatapointer.h:114 in
QArrayDataPointer<QTransform>::end()
```
ANALYSIS
KGameItem.qml contains two nested "Image" QML types. Simplified:
Image {
id: frontend
asynchronous: false // implicit
Image {
asynchronous: true
source: ...
visible: false
onStatusChanged: {
if (status == Image.Ready) parent.source = source;
}
onSourceChanged: {
if (status == Image.Ready) parent.source = source;
}
}
}
This "nested Images" idea was added by
https://github.com/KDE/libkdegames/commit/41fb995832a6 and polished in
https://github.com/KDE/libkdegames/commit/bdcd5f05076e. Images with
"asynchronous: true" queue a job on a QThread owned by QQuickPixmapCache
(QQuickPixmapReader), one per QQmlEngine, created lazily on the first
asynchronous load. Images with "asynchronous: false" load synchronously on the
GUI thread. The idea is to perform rasterization asynchronously and only update
the GUI when the images are ready, without any flickering or freezing.
However, due to a bug, sometimes the GUI thread performs rasterization at the
same time as QQuickPixmapReader and both end up using a single QSvgRenderer
concurrently. QSvgRenderer is reentrant, but not thread-safe:
- https://doc.qt.io/qt-6.11/qsvgrenderer.html
- https://doc.qt.io/qt-6/threads-reentrancy.html
It's simple to add a mutex and avoid the race condition, but why is the GUI
thread given uncached images in the first place? Also, why are only computer
and server images messed up and never cable images? Finally, if you instrument
the code, why are there requests for sizes like 175x0 or 0x0?
The following is from Claude Opus 5.5 (medium). I have gone through it and I
think it makes sense. BTW, the "pointless renders" issue could arguably be
treated as a separate bug, but it's related to this issue, so I am reporting it
here for simplicity.
All numbers come from KNetWalk with libkdegames instrumented to log every
KGameImageProvider::requestImage() call (thread, requested size, start/end
time) and every source and status change inside KGameItem, plus a knetwalk
patch that starts a new game every 250 ms. Unless stated otherwise, a batch is
64 instances running for 2 s each (about 7 games per instance) at Easy
difficulty with a 1000x1000 window, on the offscreen QPA with the software Qt
Quick backend.
## Why KGameItem renders on the GUI thread
The backend's onSourceChanged handler reads a stale status.
QQuickImageBase::setSource() emits sourceChanged() *before* calling load()
(qquickimagebase.cpp:114-118). Inside onSourceChanged, "status" still describes
the *previous* source. So "if (status == Image.Ready) parent.source = source"
copies a URL that has not been loaded yet to the frontend. The frontend is a
synchronous Image, so it renders that URL itself on the GUI thread
(createPixmapDataSync, which is exactly the main-thread stack in the TSan
report). Meanwhile the QQuickPixmapReader thread keeps rendering the backend's
queued requests.
The handler is also unnecessary. Its comment says the status "does not change"
on a cache hit, but QQuickImageBase::loadPixmap() resets status to Null without
emitting (qquickimagebase.cpp:318). A cache hit therefore still emits
statusChanged (Null -> Ready), and onStatusChanged alone already covers it. The
instrumented log shows exactly that:
backend.sourceChanged status=1 source=computer1/140x140 -> COPY TO FRONTEND
<- status=1 belongs to 175x175
frontend.source=computer1/140x140
requestImage thread=GUI source=Default/computer1/140x140
<- synchronous render
backend.statusChanged status=1 source=computer1/140x140
<- would have been enough
The stale status can only be "Ready" if the previous URL was a cache hit. That
happens on the second and later games, once the first game's sprites (including
intermediate sizes, see below) are in QQuickPixmapCache. That's why the problem
only appears when clicking "New" after the first board has been drawn.
## Pointless renders
Most of the rendering work is wasted: 9,791 of 11,860 provider renders in a
batch were for a zero-width or zero-height size (".../0x0", ".../175x0"). There
are two causes:
1. Binding glitch in KGameItem.qml. The guard "width*height==0 ? """ looks at
the live width/height, but the URL is built from the separate "size" property,
which can still hold the old value ("0x0") when "source" re-evaluates. The
provider receives ".../0x0" and, because of its "width == 0 || height == 0"
branch, renders the element at its natural SVG size (cables 19x19, computers
18x17). The result is thrown away immediately because a correct source follows
right after. Every cable, computer and server does this on every new game.
2. Imperative node setup in knetwalk's logic.js createNode().
createObject(cell) is called first, and spriteKey, anchors.fill and
anchors.margins are set afterwards. Each node therefore requests 0x0, then
175x175 (cell size), then 140x140 (after margins), and only the last one is
used. Cable.qml sets "anchors.fill: parent" declaratively, so cables skip the
intermediate size.
## Why mostly computer icons
The GUI-thread render only happens for nodes (computers and the server), never
for cables. It needs the sequence "source changes again right after a cache
hit", and only nodes get the extra anchors.margins change (175x175 cache hit ->
140x140). In the batch, every GUI-thread render was a computer or the server at
140x140, and every cable render happened on the reader thread, where renders
are serialized.
The corruption also needs the two threads to draw the *same SVG element*,
because QtSvg keeps render state in the parsed document and changes it while
drawing:
- QSvgTransformStyle::m_oldWorldTransform is a per-element QStack that apply()
pushes and revert() pops (qsvgstyle.cpp:468-474, the exact location in the TSan
report). Other styles save and restore their values in the same way.
- QSvgNode::draw() decrements a nesting-depth counter (remainingNestedNodes) in
the document-wide QSvgExtraStates and restores it afterwards with a
QScopedValueRollback.
When the GUI thread draws computer1 at 140x140 while the reader thread draws
computer1 at 0x0 for another cell, the threads pop each other's saved
transforms and restore each other's stale counter values. That gives:
- garbage transforms: wrongly sized or offset icons, and the 1e308 "too long to
rasterize" warning,
- concurrent changes to the saved state (the QStack, but also copied brushes
and pens): the glibc heap aborts,
- a nesting counter stuck at zero, after which the renderer stops drawing and
logs "qt.svg.draw: Too many nested nodes at path exceeding max nested limit of
32" (or "at g", "at rect") for every element (seen in the "cache: false"
experiment below).
I haven't pinned down which of these produces the white backgrounds.
The same-element overlap happened in 39 of 64 instances (28 times for
computers, 22 for the server), each time between the GUI-thread render and the
reader's pointless 0x0 render of the same element. Computers are also much
bigger targets than cables: 26-35 SVG nodes with 11-17 transforms, against 3
nodes and 2 transforms for a cable. A computer render takes milliseconds, while
a cable render takes tens of microseconds. The server is exposed in the same
way, but there is only one per board, so it shows up much less often.
## Fix
Three patches will be sent to invent.kde.org. Two debug patches will be
attached here.
1. libkdegames, "KGameItem: never render sprites synchronously on the GUI
thread". This is the root fix, and it fixes bugs 486016 and 514486. It removes
the backend's onSourceChanged handler (onStatusChanged already handles cache
hits) and builds the URL in one binding from
Math.round(width)/Math.round(height), with the zero guard on the rounded
values, instead of the separate "size"/"sourceUrl" properties. With only this
patch: GUI-thread renders went from 136 to 0, zero-size renders from 9,791 to
0, instances with a same-element overlap from 39 to 0, and total provider
renders from 11,860 to 2,070. Every node still ends on its final 140x140
sprite. The patch keeps one thing the removed handler did: when a sprite is
cleared (empty spriteKey or zero size), the visible image is cleared too,
because onStatusChanged also handles the Null status, which Qt Quick only
reports for an empty source. kbreakout, which also uses KGameItem, renders its
paddle on the GUI thread once or twice per start without this patch, and never
with it.
2. libkdegames, "Make KGameImageProvider thread-safe" (a mutex around
requestImage()). This is still needed after patch 1, as a second line of
defense for bugs 486016 and 514486. A KGameItem with "cache: false" can never
hit the cache, so its frontend always loads synchronously on the GUI thread,
and the same goes for any plain synchronous Image using the provider. To
confirm, I added "cache: false" to knetwalk's CanvasItem.qml on top of patch 1.
Without the mutex, 3 to 7 of 32 instances crashed in each of four repetitions
("double free or corruption (!prev)", "corrupted size vs. prev_size",
"malloc(): invalid size (unsorted)", SIGSEGV), and each repetition logged 4,600
to 7,900 "Too many nested nodes" warnings. With the mutex, there were no
crashes and no warnings. One access remains unsynchronized: the provider reads
KGameThemeProvider::currentTheme() on the reader thread while a theme switch
can change it on the GUI thread.
3. knetwalk, "Create nodes with declarative geometry". This adds Node.qml,
which declares anchors.fill and anchors.margins like Cable.qml does, and passes
spriteKey as an initial property. That avoids the intermediate 175x175 render.
It also makes the inset a binding that follows the cell size, which fixes
https://bugs.kde.org/show_bug.cgi?id=352039 (computer icons are bigger in the
first game after launch). The old code assigned cell.width / 10 once, after
creating the node. The first board is built before the window has been laid
out, while every cell is still 0 pixels wide, so the inset became 0, and every
computer and the server filled its whole cell for the rest of that game. Later
boards are built into the laid-out grid and get the 10% inset, so the icons
seem to shrink after the first "New". In my runs (1000x1000 window, 175 px
cells), unpatched computers were 175 px in the first game and 140 px in later
games; with patch 3, they were 140 px in every game. Resizing the window during
a game should cause the same problem, because the inset never followed the cell
size (from reading the code; I haven't checked resizing in the running game).
The intermediate size can still appear when a cell is resized after its node
was created: in 2 of 16 runs, one computer was requested at 175x175 during the
first game. With patch 1 that render happens on the loader thread and is
harmless. With unpatched libkdegames, this patch alone also avoided every
GUI-thread render in this test, but only because it avoids the particular
trigger. Patch 1 is the actual fix.
Timing, measured with only the requestImage() logging (no QML logging). All
combinations ran interleaved in random order, 40 instances (about 265 games)
each. "Change" is the new time minus the unpatched time, so negative means
faster (95% confidence intervals):
patches applied GUI thread blocked per "New" change
none 18.7 ms
1 9.0 ms -9.7 ± 1.6 ms
2 18.2 ms -0.4 ± 2.1 ms (noise)
3 16.2 ms -2.5 ± 1.9 ms
1 + 2 + 3 8.0 ms -10.6 ± 1.6 ms
On top of patch 1, adding patch 2 or patch 3 does not change the GUI-thread
time measurably. Patch 3 still saves about 1.1 ms of rendering per "New" on the
reader thread. Patch 2 has no measurable cost when the lock is uncontended.
These timings were taken before patch 1 gained the Null handling, which only
matters when a sprite is cleared; that doesn't happen in knetwalk's normal
play.
--
You are receiving this mail because:
You are watching all bug changes.