Bug 448454

Summary: kwin_wayland crashes in KWin::Platform::requestOutputsChange() when external monitor is disconnected while multi-monitor mode is set to 'Switch to external screen'
Product: [Plasma] kwin Reporter: Patrick Silva <bugseforuns>
Component: wayland-genericAssignee: KWin default assignee <kwin-bugs-null>
Status: RESOLVED FIXED    
Severity: crash CC: kde, matejm98mthw, nate, xaver.hugl
Priority: NOR    
Version: master   
Target Milestone: ---   
Platform: Neon   
OS: Linux   
Latest Commit: Version Fixed In: 5.24

Description Patrick Silva 2022-01-14 12:05:41 UTC
STEPS TO REPRODUCE
1. use Wayland session
2. connect a external monitor to hdmi port
3. set multi-monitor mode to ''Switc to external screen'
4. disconnect the hdmi cable

OBSERVED RESULT
kwin_crashes and I only see the mouse pointer on the laptop screen

EXPECTED RESULT
laptop screen is reactivated without any crash

SOFTWARE/OS VERSIONS
Operating System: KDE neon Unstable Edition
KDE Plasma Version: 5.24.80
KDE Frameworks Version: 5.91.0
Qt Version: 5.15.3
Graphics Platform: Wayland


Thread 8 (Thread 0x7fed859b0700 (LWP 6219)):
#0  0x00007fed9ca9faff in __GI___poll (fds=0x7fed6c005240, nfds=1, timeout=-1) at ../sysdeps/unix/sysv/linux/poll.c:29
#1  0x00007fed9b00f36e in g_main_context_poll (priority=<optimized out>, n_fds=1, fds=0x7fed6c005240, timeout=<optimized out>, context=0x7fed6c000c20) at ../../../glib/gmain.c:4346
#2  g_main_context_iterate (context=context@entry=0x7fed6c000c20, block=block@entry=1, dispatch=dispatch@entry=1, self=<optimized out>) at ../../../glib/gmain.c:4042
#3  0x00007fed9b00f4a3 in g_main_context_iteration (context=0x7fed6c000c20, may_block=may_block@entry=1) at ../../../glib/gmain.c:4108
#4  0x00007fed9d3f7842 in QEventDispatcherGlib::processEvents (this=0x7fed6c000b60, flags=...) at kernel/qeventdispatcher_glib.cpp:423
#5  0x00007fed9d39baeb in QEventLoop::exec (this=this@entry=0x7fed859afbc0, flags=..., flags@entry=...) at ../../include/QtCore/../../src/corelib/global/qflags.h:141
#6  0x00007fed9d1b5322 in QThread::exec (this=this@entry=0x559037d15540) at ../../include/QtCore/../../src/corelib/global/qflags.h:121
#7  0x00007fed9ea88559 in QQmlThreadPrivate::run (this=0x559037d15540) at qml/ftw/qqmlthread.cpp:155
#8  0x00007fed9d1b6503 in QThreadPrivate::start (arg=0x559037d15540) at thread/qthread_unix.cpp:331
#9  0x00007fed9cf9b609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#10 0x00007fed9caac293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 7 (Thread 0x7fed9549f700 (LWP 6214)):
#0  futex_wait_cancelable (private=<optimized out>, expected=0, futex_word=0x559037925be8) at ../sysdeps/nptl/futex-internal.h:183
#1  __pthread_cond_wait_common (abstime=0x0, clockid=0, mutex=0x559037925b98, cond=0x559037925bc0) at pthread_cond_wait.c:508
#2  __pthread_cond_wait (cond=cond@entry=0x559037925bc0, mutex=mutex@entry=0x559037925b98) at pthread_cond_wait.c:647
#3  0x00007fed9600db1b in cnd_wait (mtx=0x559037925b98, cond=0x559037925bc0) at ../include/c11/threads_posix.h:155
#4  util_queue_thread_func (input=input@entry=0x559037926f00) at ../src/util/u_queue.c:294
#5  0x00007fed9600d71b in impl_thrd_routine (p=<optimized out>) at ../include/c11/threads_posix.h:87
#6  0x00007fed9cf9b609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#7  0x00007fed9caac293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 6 (Thread 0x7fed87fff700 (LWP 6216)):
#0  futex_wait_cancelable (private=<optimized out>, expected=0, futex_word=0x559037925be8) at ../sysdeps/nptl/futex-internal.h:183
#1  __pthread_cond_wait_common (abstime=0x0, clockid=0, mutex=0x559037925b98, cond=0x559037925bc0) at pthread_cond_wait.c:508
#2  __pthread_cond_wait (cond=cond@entry=0x559037925bc0, mutex=mutex@entry=0x559037925b98) at pthread_cond_wait.c:647
#3  0x00007fed9600db1b in cnd_wait (mtx=0x559037925b98, cond=0x559037925bc0) at ../include/c11/threads_posix.h:155
#4  util_queue_thread_func (input=input@entry=0x559037927170) at ../src/util/u_queue.c:294
#5  0x00007fed9600d71b in impl_thrd_routine (p=<optimized out>) at ../include/c11/threads_posix.h:87
#6  0x00007fed9cf9b609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#7  0x00007fed9caac293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 5 (Thread 0x7fed94c9e700 (LWP 6215)):
#0  futex_wait_cancelable (private=<optimized out>, expected=0, futex_word=0x559037925be8) at ../sysdeps/nptl/futex-internal.h:183
#1  __pthread_cond_wait_common (abstime=0x0, clockid=0, mutex=0x559037925b98, cond=0x559037925bc0) at pthread_cond_wait.c:508
#2  __pthread_cond_wait (cond=cond@entry=0x559037925bc0, mutex=mutex@entry=0x559037925b98) at pthread_cond_wait.c:647
#3  0x00007fed9600db1b in cnd_wait (mtx=0x559037925b98, cond=0x559037925bc0) at ../include/c11/threads_posix.h:155
#4  util_queue_thread_func (input=input@entry=0x559037927130) at ../src/util/u_queue.c:294
#5  0x00007fed9600d71b in impl_thrd_routine (p=<optimized out>) at ../include/c11/threads_posix.h:87
#6  0x00007fed9cf9b609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#7  0x00007fed9caac293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 4 (Thread 0x7fed877fe700 (LWP 6217)):
#0  0x00007fed9ca9faff in __GI___poll (fds=0x7fed74005240, nfds=2, timeout=-1) at ../sysdeps/unix/sysv/linux/poll.c:29
#1  0x00007fed9b00f36e in g_main_context_poll (priority=<optimized out>, n_fds=2, fds=0x7fed74005240, timeout=<optimized out>, context=0x7fed74000c20) at ../../../glib/gmain.c:4346
#2  g_main_context_iterate (context=context@entry=0x7fed74000c20, block=block@entry=1, dispatch=dispatch@entry=1, self=<optimized out>) at ../../../glib/gmain.c:4042
#3  0x00007fed9b00f4a3 in g_main_context_iteration (context=0x7fed74000c20, may_block=may_block@entry=1) at ../../../glib/gmain.c:4108
#4  0x00007fed9d3f7842 in QEventDispatcherGlib::processEvents (this=0x7fed74000b60, flags=...) at kernel/qeventdispatcher_glib.cpp:423
#5  0x00007fed9d39baeb in QEventLoop::exec (this=this@entry=0x7fed877fdbe0, flags=..., flags@entry=...) at ../../include/QtCore/../../src/corelib/global/qflags.h:141
#6  0x00007fed9d1b5322 in QThread::exec (this=<optimized out>) at ../../include/QtCore/../../src/corelib/global/qflags.h:121
#7  0x00007fed9d1b6503 in QThreadPrivate::start (arg=0x5590377dcf10) at thread/qthread_unix.cpp:331
#8  0x00007fed9cf9b609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#9  0x00007fed9caac293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 3 (Thread 0x7fed95ca0700 (LWP 6213)):
#0  futex_wait_cancelable (private=<optimized out>, expected=0, futex_word=0x559037925be8) at ../sysdeps/nptl/futex-internal.h:183
#1  __pthread_cond_wait_common (abstime=0x0, clockid=0, mutex=0x559037925b98, cond=0x559037925bc0) at pthread_cond_wait.c:508
#2  __pthread_cond_wait (cond=cond@entry=0x559037925bc0, mutex=mutex@entry=0x559037925b98) at pthread_cond_wait.c:647
#3  0x00007fed9600db1b in cnd_wait (mtx=0x559037925b98, cond=0x559037925bc0) at ../include/c11/threads_posix.h:155
#4  util_queue_thread_func (input=input@entry=0x559037926cd0) at ../src/util/u_queue.c:294
#5  0x00007fed9600d71b in impl_thrd_routine (p=<optimized out>) at ../include/c11/threads_posix.h:87
#6  0x00007fed9cf9b609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#7  0x00007fed9caac293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 2 (Thread 0x7fed975d1700 (LWP 6212)):
#0  0x00007fed9ca9faff in __GI___poll (fds=0x7fed900053c0, nfds=3, timeout=-1) at ../sysdeps/unix/sysv/linux/poll.c:29
#1  0x00007fed9b00f36e in g_main_context_poll (priority=<optimized out>, n_fds=3, fds=0x7fed900053c0, timeout=<optimized out>, context=0x7fed90001ce0) at ../../../glib/gmain.c:4346
#2  g_main_context_iterate (context=context@entry=0x7fed90001ce0, block=block@entry=1, dispatch=dispatch@entry=1, self=<optimized out>) at ../../../glib/gmain.c:4042
#3  0x00007fed9b00f4a3 in g_main_context_iteration (context=0x7fed90001ce0, may_block=may_block@entry=1) at ../../../glib/gmain.c:4108
#4  0x00007fed9d3f7842 in QEventDispatcherGlib::processEvents (this=0x7fed90000b60, flags=...) at kernel/qeventdispatcher_glib.cpp:423
#5  0x00007fed9d39baeb in QEventLoop::exec (this=this@entry=0x7fed975d0bb0, flags=..., flags@entry=...) at ../../include/QtCore/../../src/corelib/global/qflags.h:141
#6  0x00007fed9d1b5322 in QThread::exec (this=this@entry=0x7fed9fa03d80 <(anonymous namespace)::Q_QGS__q_manager::innerFunction()::holder>) at ../../include/QtCore/../../src/corelib/global/qflags.h:121
#7  0x00007fed9f97ff4b in QDBusConnectionManager::run (this=0x7fed9fa03d80 <(anonymous namespace)::Q_QGS__q_manager::innerFunction()::holder>) at qdbusconnection.cpp:179
#8  0x00007fed9d1b6503 in QThreadPrivate::start (arg=0x7fed9fa03d80 <(anonymous namespace)::Q_QGS__q_manager::innerFunction()::holder>) at thread/qthread_unix.cpp:331
#9  0x00007fed9cf9b609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#10 0x00007fed9caac293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 1 (Thread 0x7fed98282680 (LWP 6205)):
#0  0x000055903874aa60 in ?? ()
#1  0x00007fed9d3a60ee in QMetaObject::cast (this=this@entry=0x7fed96dceba0 <KWin::DrmOutput::staticMetaObject>, obj=0x5590384e2b40) at kernel/qmetaobject.cpp:382
#2  0x00007fed9d3a6129 in QMetaObject::cast (this=this@entry=0x7fed96dceba0 <KWin::DrmOutput::staticMetaObject>, obj=<optimized out>) at kernel/qmetaobject.cpp:371
#3  0x00007fed96d82d29 in qobject_cast<KWin::DrmOutput*> (object=<optimized out>) at /usr/include/x86_64-linux-gnu/qt5/QtCore/qobject.h:519
#4  KWin::DrmBackend::applyOutputChanges (this=0x559037796bb0, config=...) at ./src/backends/drm/drm_backend.cpp:639
#5  0x00007fed9f74822b in KWin::Platform::requestOutputsChange (this=0x559037796bb0, config=0x559038881fd0) at ./src/platform.cpp:133
#6  0x00007fed9d3d400e in QtPrivate::QSlotObjectBase::call (a=0x7ffe7ea58120, r=0x559037782510, this=0x5590377bd1b0) at ../../include/QtCore/../../src/corelib/kernel/qobjectdefs_impl.h:398
#7  doActivate<false> (sender=0x5590377bd080, signal_index=3, argv=0x7ffe7ea58120) at kernel/qobject.cpp:3886
#8  0x00007fed9d3cd3d7 in QMetaObject::activate (sender=<optimized out>, m=m@entry=0x7fed9f3cdb00 <KWaylandServer::OutputManagementV2Interface::staticMetaObject>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7ffe7ea58120) at kernel/qobject.cpp:3946
#9  0x00007fed9f28ae43 in KWaylandServer::OutputManagementV2Interface::configurationChangeRequested (this=<optimized out>, _t1=<optimized out>) at ./obj-x86_64-linux-gnu/src/server/KWaylandServer_autogen/EWIEGA46WW/moc_outputmanagement_v2_interface.cpp:136
#10 0x00007fed9ac38ff5 in ffi_call_unix64 () at ../src/x86/unix64.S:101
#11 0x00007fed9ac3840a in ffi_call_int (cif=<optimized out>, fn=<optimized out>, rvalue=<optimized out>, avalue=<optimized out>, closure=<optimized out>) at ../src/x86/ffi64.c:669
#12 0x00007fed9c188628 in wl_closure_invoke (closure=0x5590387d9ca0, flags=2, target=<optimized out>, opcode=5, data=<optimized out>) at ../src/connection.c:1018
#13 0x00007fed9c1849e2 in wl_client_connection_data (fd=<optimized out>, mask=<optimized out>, data=0x5590387b7ac0) at ../src/wayland-server.c:432
#14 0x00007fed9c18665a in wl_event_loop_dispatch (loop=0x559037792c10, timeout=timeout@entry=0) at ../src/event-loop.c:1027
#15 0x00007fed9f29da4c in KWaylandServer::Display::dispatchEvents (this=<optimized out>) at ./src/server/display.cpp:114
#16 0x00007fed9d3d400e in QtPrivate::QSlotObjectBase::call (a=0x7ffe7ea58830, r=0x5590377cd370, this=0x559037ad36d0) at ../../include/QtCore/../../src/corelib/kernel/qobjectdefs_impl.h:398
#17 doActivate<false> (sender=0x5590377b24f0, signal_index=3, argv=0x7ffe7ea58830) at kernel/qobject.cpp:3886
#18 0x00007fed9d3cd3d7 in QMetaObject::activate (sender=sender@entry=0x5590377b24f0, m=m@entry=0x7fed9d638b40 <QSocketNotifier::staticMetaObject>, local_signal_index=local_signal_index@entry=0, argv=argv@entry=0x7ffe7ea58830) at kernel/qobject.cpp:3946
#19 0x00007fed9d3d8093 in QSocketNotifier::activated (this=this@entry=0x5590377b24f0, _t1=..., _t2=<optimized out>, _t3=...) at .moc/moc_qsocketnotifier.cpp:178
#20 0x00007fed9d3d8833 in QSocketNotifier::event (this=0x5590377b24f0, e=0x7ffe7ea58b00) at kernel/qsocketnotifier.cpp:302
#21 0x00007fed9dec0dc3 in QApplicationPrivate::notify_helper (this=this@entry=0x55903777fc30, receiver=receiver@entry=0x5590377b24f0, e=e@entry=0x7ffe7ea58b00) at kernel/qapplication.cpp:3632
#22 0x00007fed9dec9bb8 in QApplication::notify (this=0x7ffe7ea58f30, receiver=0x5590377b24f0, e=0x7ffe7ea58b00) at kernel/qapplication.cpp:3156
#23 0x00007fed9d39cfea in QCoreApplication::notifyInternal2 (receiver=0x5590377b24f0, event=0x7ffe7ea58b00) at ../../include/QtCore/5.15.3/QtCore/private/../../../../../src/corelib/thread/qthread_p.h:332
#24 0x00007fed9d3f4a6b in QEventDispatcherUNIXPrivate::activateSocketNotifiers (this=0x55903778c230) at kernel/qeventdispatcher_unix.cpp:304
#25 0x00007fed9d3f4ecb in QEventDispatcherUNIX::processEvents (this=<optimized out>, flags=...) at kernel/qeventdispatcher_unix.cpp:511
#26 0x000055903670a321 in QUnixEventDispatcherQPA::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) ()
#27 0x00007fed9d39baeb in QEventLoop::exec (this=this@entry=0x7ffe7ea58c90, flags=..., flags@entry=...) at ../../include/QtCore/../../src/corelib/global/qflags.h:141
#28 0x00007fed9d3a3c94 in QCoreApplication::exec () at ../../include/QtCore/../../src/corelib/global/qflags.h:121
#29 0x00005590365ecb5c in main (argc=<optimized out>, argv=<optimized out>) at ./src/main_wayland.cpp:727
Comment 1 Nate Graham 2022-01-14 21:22:26 UTC
Cannot reproduce with those exact steps and hardware. No crash for me.
Comment 2 Matej Mrenica 2022-01-15 11:12:16 UTC
I think I have the same issue: https://pastebin.com/F0zTDwhz but I got it by different approach.
1. external monitor connected via HDMI
2. external monitor is the only one enabled and it's also set as primary
3. I left my pc running with the screen locked
4. when I returned, I logged in and saw only a black screen with a cursor, kwin, plasmashell and all apps were killed.
This is on Arch Linux, Plasma 5.24 Beta.
Comment 3 hexchain 2022-01-16 05:38:59 UTC
I have the same issue as #2: gdb bt -full https://pastebin.pl/view/04d358b4

My setup:
laptop with internal screen (1920x1080), eDP, lid closed
external monitor (2560x1600), HDMI

Steps:
1. Enter Wayland session
2. Turn off the external monitor via its power button
3. Turn on the monitor
4. Notice that kwin has crashed, the desktop itself still seems to function but any applications opened prior to step 2 are lost

Versions:
Operating System: Arch Linux
KDE Plasma Version: 5.23.90
KDE Frameworks Version: 5.90.0
Qt Version: 5.15.2
Kernel Version: 5.15.10-zen1-1-zen (64-bit)
Graphics Platform: Wayland
Processors: 16 × AMD Ryzen 7 4800H with Radeon Graphics
Memory: 29.8 GiB of RAM
Graphics Processor: AMD RENOIR
Comment 4 hexchain 2022-01-16 06:00:14 UTC
Some extra kwin debug logs, produced with:

> QT_LOGGING_RULES="kwin_wayland_drm.debug=true;kwin_core.debug=true;kwin_wayland_backend.debug=true"

http://ix.io/3Mn2
Comment 5 Bug Janitor Service 2022-01-18 11:18:18 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/1905
Comment 6 Zamundaaa 2022-01-18 11:27:45 UTC
I can't reproduce the bug, can someone here verify that this works?
Comment 7 Matej Mrenica 2022-01-18 12:54:58 UTC
(In reply to Zamundaaa from comment #6)
> I can't reproduce the bug, can someone here verify that this works?

I built kwin from git (Plasma 5.24 + your patch) and if I unplug my monitor I get a crash https://pastebin.com/aSQWaQuM
Comment 8 hexchain 2022-01-18 12:58:41 UTC
(In reply to Zamundaaa from comment #6)
> I can't reproduce the bug, can someone here verify that this works?

After applying that patch, KWin no longer crashes when turning off/on the external monitor.
Comment 9 Matej Mrenica 2022-01-18 13:40:45 UTC
(In reply to hexchain from comment #8)
> (In reply to Zamundaaa from comment #6)
> > I can't reproduce the bug, can someone here verify that this works?
> 
> After applying that patch, KWin no longer crashes when turning off/on the
> external monitor.

How did we get different results? Are we not having the same issue?
Comment 10 hexchain 2022-01-18 13:50:19 UTC
(In reply to Matej Mrenica from comment #9)
> (In reply to hexchain from comment #8)
> > (In reply to Zamundaaa from comment #6)
> > > I can't reproduce the bug, can someone here verify that this works?
> > 
> > After applying that patch, KWin no longer crashes when turning off/on the
> > external monitor.
> 
> How did we get different results? Are we not having the same issue?

Looking at our backtraces, I'd guess we have the same issue (#2 and #3), but what you just encountered (#7) may be a new one.
Comment 11 Nate Graham 2022-01-18 21:36:28 UTC
Git commit e0a8fa778e3dce508695155c7d5a9a1e0bddd9f8 by Nate Graham, on behalf of Xaver Hugl.
Committed on 18/01/2022 at 21:36.
Pushed by ngraham into branch 'master'.

backends/drm: fix output changes with the placeholder output

As the placeholder output gets added or removed in response to other outputs
getting enabled or disabled, the output list may change while iterating over
them and applying changes.
Related: bug 448474, bug 448697
FIXED-IN: 5.24

M  +7    -3    src/backends/drm/drm_backend.cpp

https://invent.kde.org/plasma/kwin/commit/e0a8fa778e3dce508695155c7d5a9a1e0bddd9f8
Comment 12 Nate Graham 2022-01-18 21:38:05 UTC
Git commit 24dc3abe918cde9007e19c4351e5778480f5bb75 by Nate Graham, on behalf of Xaver Hugl.
Committed on 18/01/2022 at 21:37.
Pushed by ngraham into branch 'Plasma/5.24'.

backends/drm: fix output changes with the placeholder output

As the placeholder output gets added or removed in response to other outputs
getting enabled or disabled, the output list may change while iterating over
them and applying changes.
Related: bug 448474, bug 448697
FIXED-IN: 5.24


(cherry picked from commit e0a8fa778e3dce508695155c7d5a9a1e0bddd9f8)

M  +7    -3    src/backends/drm/drm_backend.cpp

https://invent.kde.org/plasma/kwin/commit/24dc3abe918cde9007e19c4351e5778480f5bb75
Comment 13 Zamundaaa 2022-01-18 22:27:59 UTC
(In reply to Matej Mrenica from comment #7)
> I built kwin from git (Plasma 5.24 + your patch) and if I unplug my monitor
> I get a crash https://pastebin.com/aSQWaQuM

It's not the same crash but related. https://invent.kde.org/plasma/kwin/-/merge_requests/1908 should fix it
Comment 14 Matej Mrenica 2022-01-19 14:06:04 UTC
(In reply to Zamundaaa from comment #13)
> (In reply to Matej Mrenica from comment #7)
> > I built kwin from git (Plasma 5.24 + your patch) and if I unplug my monitor
> > I get a crash https://pastebin.com/aSQWaQuM
> 
> It's not the same crash but related.
> https://invent.kde.org/plasma/kwin/-/merge_requests/1908 should fix it

It doesn't fix it for me, see: https://pastebin.com/zsrabTxv
Comment 15 Zamundaaa 2022-01-19 14:32:58 UTC
Added two more fixes to the MR that could cause the bug. If it still occurs with that, please open a new bug report and put me in CC so that we can gather more information