-
Notifications
You must be signed in to change notification settings - Fork 41
fix: prevent crash when closing wayland popup surfaces #453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yixinshark
merged 1 commit into
linuxdeepin:master
from
yixinshark:fix-windowCloseCrash
Apr 24, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. | ||
| // SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
|
|
@@ -6,12 +6,62 @@ | |
| #include "pluginsurface_p.h" | ||
| #include "pluginmanagerintegration_p.h" | ||
|
|
||
| #include "qwayland-plugin-manager-v1.h" | ||
|
|
||
| #include <QTimer> | ||
| #include <QApplication> | ||
| #include <QWidget> | ||
| #include <QEvent> | ||
| #include <QtWaylandClient/private/qwaylandwindow_p.h> | ||
|
|
||
| namespace Plugin { | ||
|
|
||
| class NullHandleGuard : public QObject { | ||
| public: | ||
| static void install() { | ||
| static bool installed = false; | ||
| if (!installed && qApp) { | ||
| qApp->installEventFilter(new NullHandleGuard(qApp)); | ||
| installed = true; | ||
| } | ||
| } | ||
|
|
||
| NullHandleGuard(QObject *parent = nullptr) : QObject(parent) {} | ||
|
Check warning on line 29 in src/tray-wayland-integration/pluginsurface.cpp
|
||
|
|
||
| protected: | ||
| bool eventFilter(QObject *watched, QEvent *event) override { | ||
| if (event->type() == QEvent::UpdateRequest) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 每个widget的每一帧都要判断么? |
||
| QWindow *win = nullptr; | ||
| QWidget *w = qobject_cast<QWidget *>(watched); | ||
| QWindow *wnd = qobject_cast<QWindow *>(watched); | ||
|
|
||
| if (w) { | ||
| win = w->windowHandle(); | ||
| if (!win && w->window()) { | ||
| win = w->window()->windowHandle(); | ||
| } | ||
| } else if (wnd) { | ||
| win = wnd; | ||
| } | ||
|
|
||
| // Drop UpdateRequest to prevent QWaylandShmBackingStore from crashing | ||
| // in beginPaint/decoration() when trying to paint a destroyed window. | ||
| if (win) { | ||
| // Case 1: The QWindow exists, but its underlying Wayland surface is destroyed. | ||
| if (!win->handle()) { | ||
| qDebug() << "NullHandleGuard Dropped UpdateRequest for" << watched << "(null Wayland handle)"; | ||
| return true; | ||
| } | ||
| } else if (w) { | ||
| // Case 2: The QWidget has lost its QWindow entirely. | ||
| // A widget without a QWindow cannot be painted to the screen. | ||
| qDebug() << "NullHandleGuard Dropped UpdateRequest for" << watched << "(missing QWindow)"; | ||
| return true; | ||
| } | ||
| } | ||
| return QObject::eventFilter(watched, event); | ||
| } | ||
| }; | ||
| PluginSurface::PluginSurface(PluginManagerIntegration *manager, QtWaylandClient::QWaylandWindow *window) | ||
| : QtWaylandClient::QWaylandShellSurface(window) | ||
| , QtWayland::plugin() | ||
|
|
@@ -130,13 +180,43 @@ | |
| destroy(); | ||
| } | ||
|
|
||
| void PluginPopupSurface::plugin_popup_close() | ||
| { | ||
| // it would be delete this object directly. | ||
| m_window->close(); | ||
| // Install the global safeguard just in case | ||
| NullHandleGuard::install(); | ||
|
|
||
| // Use QPointer to ensure m_window is still valid when the queued lambda executes | ||
| QPointer<QWindow> safeWindow(m_window); | ||
|
|
||
| // DEFER the destruction! | ||
| // Why: QWaylandShmBackingStore::beginPaint() can spin the Wayland event loop | ||
| // (e.g., waiting for buffers) which synchronously dispatches this Wayland event. | ||
| // If we destroy m_window here, beginPaint() resumes with a dangling pointer | ||
| // and crashes immediately. Deferring to the next event loop iteration ensures | ||
| // that the current paint frame completes before we destroy the Wayland surface. | ||
| QMetaObject::invokeMethod(qApp, [safeWindow]() { | ||
| if (!safeWindow) { | ||
| return; | ||
| } | ||
|
|
||
| QWidget *popupWidget = nullptr; | ||
| for (QWidget *w : QApplication::topLevelWidgets()) { | ||
| if (w && w->windowHandle() == safeWindow.data()) { | ||
| popupWidget = w; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (popupWidget) { | ||
| popupWidget->hide(); | ||
| } | ||
|
|
||
| // Safely close the QWindow. This destroys the Wayland surface. | ||
| safeWindow->close(); | ||
| }, Qt::QueuedConnection); | ||
| } | ||
|
|
||
| void PluginPopupSurface::plugin_popup_geometry(int32_t x, int32_t y, int32_t width, int32_t height) | ||
| { | ||
| auto plugin = PluginPopup::get(m_window); | ||
| auto rect = QRect(x, y, width, height); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.