Skip to content

Commit

Permalink
Fix destruction and tear down of the embedding thread. (#2328)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
manyoso committed May 15, 2024
1 parent 1427ef7 commit 61cefcf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions gpt4all-chat/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ Database::~Database()
{
m_dbThread.quit();
m_dbThread.wait();
delete m_embLLM;
}

void Database::scheduleNext(int folder_id, size_t countForFolder)
Expand Down
8 changes: 8 additions & 0 deletions gpt4all-chat/embllm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ EmbeddingLLMWorker::EmbeddingLLMWorker()
: QObject(nullptr)
, m_networkManager(new QNetworkAccessManager(this))
, m_model(nullptr)
, m_stopGenerating(false)
{
moveToThread(&m_workerThread);
connect(this, &EmbeddingLLMWorker::finished, &m_workerThread, &QThread::quit, Qt::DirectConnection);
Expand All @@ -14,6 +15,10 @@ EmbeddingLLMWorker::EmbeddingLLMWorker()

EmbeddingLLMWorker::~EmbeddingLLMWorker()
{
m_stopGenerating = true;
m_workerThread.quit();
m_workerThread.wait();

if (m_model) {
delete m_model;
m_model = nullptr;
Expand Down Expand Up @@ -148,6 +153,9 @@ void EmbeddingLLMWorker::requestSyncEmbedding(const QString &text)
// this function is always called for storage into the database
void EmbeddingLLMWorker::requestAsyncEmbedding(const QVector<EmbeddingChunk> &chunks)
{
if (m_stopGenerating)
return;

if (!hasModel() && !loadModel()) {
qWarning() << "WARNING: Could not load model for embeddings";
return;
Expand Down
1 change: 1 addition & 0 deletions gpt4all-chat/embllm.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private Q_SLOTS:
QNetworkAccessManager *m_networkManager;
std::vector<float> m_lastResponse;
LLModel *m_model = nullptr;
std::atomic<bool> m_stopGenerating;
QThread m_workerThread;
};

Expand Down

0 comments on commit 61cefcf

Please sign in to comment.