Skip to content

Commit

Permalink
fix: [codegeex] crash when generate file index
Browse files Browse the repository at this point in the history
QProcess thread issue

Log: as title
  • Loading branch information
LiHua000 authored and deepin-mozart committed Oct 28, 2024
1 parent e2acd78 commit 50da92a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
34 changes: 17 additions & 17 deletions src/plugins/codegeex/codegeexmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,7 @@ void CodeGeeXManager::showIndexingWidget()
ProjectService *prjServ = dpfGetService(ProjectService);
auto currentProject = prjServ->getActiveProjectInfo().workspaceFolder();
connect(confirmBtn, &QPushButton::clicked, widget, [=]() {
QtConcurrent::run([=]() { generateRag(currentProject); });

generateRag(currentProject);
layout->addWidget(new QLabel(tr("It may take servel minutes"), widget));
layout->addWidget(spinner);
spinner->show();
Expand Down Expand Up @@ -536,36 +535,37 @@ void CodeGeeXManager::generateRag(const QString &projectPath)
{
if (indexingProject.contains(projectPath))
return;
bool failed = false;
indexingProject.append(projectPath);
QProcess process;
QObject::connect(QApplication::instance(), &QApplication::aboutToQuit, this, [&process]() { process.terminate(); });
QProcess *process = new QProcess;
QObject::connect(QApplication::instance(), &QApplication::aboutToQuit, process, [process]() {
process->kill();
});

QObject::connect(&process, &QProcess::readyReadStandardError, &process, [&, projectPath]() {
if (!failed) // only notify once
emit notify(2, tr("The error occurred when performing rag on project %1.").arg(projectPath));
failed = true;
qInfo() << "Error:" << process.readAllStandardError() << "\n";
QObject::connect(process, &QProcess::readyReadStandardError, process, [process]() {
qInfo() << "Error:" << process->readAllStandardError() << "\n";
});

QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, [&](int exitCode, QProcess::ExitStatus exitStatus) {
QObject::connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
process, [=](int exitCode, QProcess::ExitStatus exitStatus) {
qInfo() << "Python script finished with exit code" << exitCode << "Exit!!!";
indexingProject.removeOne(projectPath);
auto success = process->readAllStandardError().isEmpty();
emit generateDone(projectPath, !success);
if (!success)
emit notify(2, tr("The error occurred when performing rag on project %1.").arg(projectPath));
process->deleteLater();
});

qInfo() << "start rag project:" << projectPath;

QString ragPath = CustomPaths::CustomPaths::global(CustomPaths::Scripts) + "/rag";
process.setWorkingDirectory(ragPath);
process->setWorkingDirectory(ragPath);
auto generatePyPath = ragPath + "/generate.py";
auto pythonPath = condaRootPath() + "/miniforge/envs/deepin_unioncode_env/bin/python";
auto modelPath = CustomPaths::CustomPaths::global(CustomPaths::Models);
if (!QFileInfo(pythonPath).exists())
return;
process.start(pythonPath, QStringList() << generatePyPath << modelPath << projectPath);
process.waitForFinished(-1);
indexingProject.removeOne(projectPath);
emit generateDone(projectPath, failed);
process->start(pythonPath, QStringList() << generatePyPath << modelPath << projectPath);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codegeex/codegeexmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class CodeGeeXManager : public QObject
QString condaRootPath() const;
void showIndexingWidget();
Q_INVOKABLE void installConda();
void generateRag(const QString &projectPath);
Q_INVOKABLE void generateRag(const QString &projectPath);
/*
JsonObject:
Query: str
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codegeex/eventreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ 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);
QMetaObject::invokeMethod(CodeGeeXManager::instance(), "generateRag", Qt::QueuedConnection, Q_ARG(const QString &, projectPath));
}

void CodeGeeXReceiver::processSwitchToWidget(const dpf::Event &event)
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/rag/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,4 @@ def embeddingDirectory(dir: str, provider: ONNXEmbeddingsProvider, db: sqlite3.C
if (len(sys.argv) > 2):
embeddingDirectory(sys.argv[2], provider, db)

db.db.close()
db.db.close()

0 comments on commit 50da92a

Please sign in to comment.