From 009647d99b01aa5feaa1377d7943aabe4b405a11 Mon Sep 17 00:00:00 2001 From: Zhang TingAn Date: Thu, 31 Oct 2024 15:37:00 +0800 Subject: [PATCH] fix: [codegeex] thread issue of generate file index Log: as title --- src/plugins/codegeex/codegeexmanager.cpp | 12 ++++++++++-- src/plugins/codegeex/codegeexmanager.h | 2 ++ src/plugins/codegeex/eventreceiver.cpp | 10 ++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/plugins/codegeex/codegeexmanager.cpp b/src/plugins/codegeex/codegeexmanager.cpp index 7c317dbfc..507f88107 100644 --- a/src/plugins/codegeex/codegeexmanager.cpp +++ b/src/plugins/codegeex/codegeexmanager.cpp @@ -526,13 +526,17 @@ void CodeGeeXManager::installConda() void CodeGeeXManager::generateRag(const QString &projectPath) { - if (indexingProject.contains(projectPath)) + mutex.lock(); + if (indexingProject.contains(projectPath)) { + mutex.unlock(); return; + } indexingProject.append(projectPath); + mutex.unlock(); QProcess *process = new QProcess; QObject::connect(QApplication::instance(), &QApplication::aboutToQuit, process, [process]() { process->kill(); - }); + }, Qt::DirectConnection); QObject::connect(process, &QProcess::readyReadStandardError, process, [process]() { qInfo() << "Error:" << process->readAllStandardError() << "\n"; @@ -541,7 +545,9 @@ void CodeGeeXManager::generateRag(const QString &projectPath) QObject::connect(process, QOverload::of(&QProcess::finished), process, [=](int exitCode, QProcess::ExitStatus exitStatus) { qInfo() << "Python script finished with exit code" << exitCode << "Exit!!!"; + mutex.lock(); indexingProject.removeOne(projectPath); + mutex.unlock(); auto success = process->readAllStandardError().isEmpty(); emit generateDone(projectPath, !success); if (!success) @@ -559,6 +565,8 @@ void CodeGeeXManager::generateRag(const QString &projectPath) if (!QFileInfo(pythonPath).exists()) return; process->start(pythonPath, QStringList() << generatePyPath << modelPath << projectPath); + if (QThread::currentThread() != qApp->thread()) // incase thread exit before process done. cause slot function can`t work + process->waitForFinished(); } /* diff --git a/src/plugins/codegeex/codegeexmanager.h b/src/plugins/codegeex/codegeexmanager.h index 55ae1e2f5..42594739b 100644 --- a/src/plugins/codegeex/codegeexmanager.h +++ b/src/plugins/codegeex/codegeexmanager.h @@ -11,6 +11,7 @@ #include #include #include +#include struct RecordData { @@ -153,6 +154,7 @@ public Q_SLOTS: bool isRunning { false }; bool condaInstalled { false }; QTimer installCondaTimer; + QMutex mutex; QStringList indexingProject {}; }; diff --git a/src/plugins/codegeex/eventreceiver.cpp b/src/plugins/codegeex/eventreceiver.cpp index 67c888a28..9395a0f0f 100644 --- a/src/plugins/codegeex/eventreceiver.cpp +++ b/src/plugins/codegeex/eventreceiver.cpp @@ -76,10 +76,12 @@ void CodeGeeXReceiver::processActionInvokedEvent(const dpf::Event &event) void CodeGeeXReceiver::processOpenProjectEvent(const dpf::Event &event) { - auto projectPath = event.property("workspace").toString(); - QJsonObject results = CodeGeeXManager::instance()->query(projectPath, "", 1); - if (results["Chunks"].toArray().size() != 0) // project has generated, update it - CodeGeeXManager::instance()->generateRag(projectPath); + QtConcurrent::run([=](){ + auto projectPath = event.property("workspace").toString(); + QJsonObject results = CodeGeeXManager::instance()->query(projectPath, "", 1); + if (results["Chunks"].toArray().size() != 0) // project has generated, update it + CodeGeeXManager::instance()->generateRag(projectPath); + }); } void CodeGeeXReceiver::processSwitchToWidget(const dpf::Event &event)