Fix destruction and tear down of the embedding thread. (#2328)

* Fix destruction and tear down of the embedding thread.

Signed-off-by: Adam Treat <treat.adam@gmail.com>

* Fix order of deletion to prevent use after free.

Signed-off-by: Adam Treat <treat.adam@gmail.com>

---------

Signed-off-by: Adam Treat <treat.adam@gmail.com>
This commit is contained in:
AT 2024-05-15 09:01:53 -05:00 committed by GitHub
parent 1427ef7195
commit 61cefcfd8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 0 deletions

View File

@ -552,6 +552,7 @@ Database::~Database()
{ {
m_dbThread.quit(); m_dbThread.quit();
m_dbThread.wait(); m_dbThread.wait();
delete m_embLLM;
} }
void Database::scheduleNext(int folder_id, size_t countForFolder) void Database::scheduleNext(int folder_id, size_t countForFolder)

View File

@ -5,6 +5,7 @@ EmbeddingLLMWorker::EmbeddingLLMWorker()
: QObject(nullptr) : QObject(nullptr)
, m_networkManager(new QNetworkAccessManager(this)) , m_networkManager(new QNetworkAccessManager(this))
, m_model(nullptr) , m_model(nullptr)
, m_stopGenerating(false)
{ {
moveToThread(&m_workerThread); moveToThread(&m_workerThread);
connect(this, &EmbeddingLLMWorker::finished, &m_workerThread, &QThread::quit, Qt::DirectConnection); connect(this, &EmbeddingLLMWorker::finished, &m_workerThread, &QThread::quit, Qt::DirectConnection);
@ -14,6 +15,10 @@ EmbeddingLLMWorker::EmbeddingLLMWorker()
EmbeddingLLMWorker::~EmbeddingLLMWorker() EmbeddingLLMWorker::~EmbeddingLLMWorker()
{ {
m_stopGenerating = true;
m_workerThread.quit();
m_workerThread.wait();
if (m_model) { if (m_model) {
delete m_model; delete m_model;
m_model = nullptr; m_model = nullptr;
@ -148,6 +153,9 @@ void EmbeddingLLMWorker::requestSyncEmbedding(const QString &text)
// this function is always called for storage into the database // this function is always called for storage into the database
void EmbeddingLLMWorker::requestAsyncEmbedding(const QVector<EmbeddingChunk> &chunks) void EmbeddingLLMWorker::requestAsyncEmbedding(const QVector<EmbeddingChunk> &chunks)
{ {
if (m_stopGenerating)
return;
if (!hasModel() && !loadModel()) { if (!hasModel() && !loadModel()) {
qWarning() << "WARNING: Could not load model for embeddings"; qWarning() << "WARNING: Could not load model for embeddings";
return; return;

View File

@ -58,6 +58,7 @@ private:
QNetworkAccessManager *m_networkManager; QNetworkAccessManager *m_networkManager;
std::vector<float> m_lastResponse; std::vector<float> m_lastResponse;
LLModel *m_model = nullptr; LLModel *m_model = nullptr;
std::atomic<bool> m_stopGenerating;
QThread m_workerThread; QThread m_workerThread;
}; };