Skip to content

Commit c5abbea

Browse files
committed
feat: add crash handler with cache cleanup
Added signal handlers for common crash signals (SIGSEGV, SIGILL, SIGABRT, SIGFPE) to automatically clean up QML cache directory when crashes occur in release builds. This prevents corrupted cache from causing repeated startup failures. The handler first removes the qmlcache directory, then re-raises the signal to generate core dump for debugging. Influence: 1. Test application startup after simulating crashes to verify cache cleanup works 2. Verify normal operation is unaffected in debug builds (QT_DEBUG defined) 3. Check that core dumps are still generated for crash analysis 4. Test QML loading performance after cache cleanup to ensure proper regeneration feat: 添加崩溃处理及缓存清理功能 为常见崩溃信号(SIGSEGV、SIGILL、SIGABRT、SIGFPE)添加信号处理程序,在 发布版本中发生崩溃时自动清理QML缓存目录。这可以防止损坏的缓存导致重复启 动失败。处理程序会先删除qmlcache目录,然后重新触发信号以生成核心转储用于 调试。 Influence: 1. 模拟崩溃后测试应用启动,验证缓存清理功能正常工作 2. 验证调试版本(定义了QT_DEBUG)的正常操作不受影响 3. 检查崩溃分析所需的核心转储是否仍然生成 4. 测试缓存清理后的QML加载性能,确保正确重新生成 PMS: BUG-313293
1 parent 2ce629d commit c5abbea

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/dde-control-center/main.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <QCommandLineOption>
1212
#include <QCommandLineParser>
1313
#include <QDBusConnection>
14+
#include <QDir>
1415
#include <QGuiApplication>
1516
#include <QIcon>
1617
#include <QQmlApplicationEngine>
@@ -19,10 +20,26 @@
1920
#include <QStandardPaths>
2021
#include <QStringList>
2122
#include <QWindow>
22-
23+
#ifndef QT_DEBUG
24+
# include <signal.h>
25+
#endif
2326
DGUI_USE_NAMESPACE
24-
DCORE_USE_NAMESPACE
2527

28+
DCORE_USE_NAMESPACE
29+
#ifndef QT_DEBUG
30+
void sig_crash(int signum)
31+
{
32+
// 崩溃了,尝试清下缓存,防止下次还起不来
33+
QString qmlCache = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/deepin/dde-control-center/qmlcache";
34+
QDir dir(qmlCache);
35+
if (dir.exists()) {
36+
dir.removeRecursively();
37+
}
38+
// 重新触发信号,产生coredump
39+
signal(signum, SIG_DFL);
40+
raise(signum);
41+
}
42+
#endif
2643
QString loggingRules(const QString &loggingModule)
2744
{
2845
if (loggingModule.isEmpty())
@@ -53,6 +70,19 @@ QStringList defaultpath()
5370

5471
int main(int argc, char *argv[])
5572
{
73+
#ifndef QT_DEBUG
74+
// 设置信号处理函数
75+
struct sigaction sa;
76+
sa.sa_handler = sig_crash;
77+
sigemptyset(&sa.sa_mask);
78+
// 在处理完信号后恢复默认信号处理
79+
sa.sa_flags = SA_RESETHAND;
80+
81+
sigaction(SIGSEGV, &sa, nullptr);
82+
sigaction(SIGILL, &sa, nullptr);
83+
sigaction(SIGABRT, &sa, nullptr);
84+
sigaction(SIGFPE, &sa, nullptr);
85+
#endif
5686
QGuiApplication *app = new QGuiApplication(argc, argv);
5787
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
5888
app.setAttribute(Qt::AA_UseHighDpiPixmaps);

0 commit comments

Comments
 (0)