Don't block on exit when not connected.

This commit is contained in:
Adam Treat 2023-07-11 12:37:21 -04:00
parent 88bbe30952
commit 34a3b9c857
5 changed files with 34 additions and 3 deletions

View File

@ -4,6 +4,7 @@
#include <vector>
#include <iostream>
#include <QCoreApplication>
#include <QThread>
#include <QEventLoop>
#include <QJsonDocument>
@ -160,6 +161,7 @@ void ChatGPTWorker::request(const QString &apiKey,
request.setRawHeader("Authorization", authorization.toUtf8());
m_networkManager = new QNetworkAccessManager(this);
QNetworkReply *reply = m_networkManager->post(request, array);
connect(qApp, &QCoreApplication::aboutToQuit, reply, &QNetworkReply::abort);
connect(reply, &QNetworkReply::finished, this, &ChatGPTWorker::handleFinished);
connect(reply, &QNetworkReply::readyRead, this, &ChatGPTWorker::handleReadyRead);
connect(reply, &QNetworkReply::errorOccurred, this, &ChatGPTWorker::handleErrorOccurred);

View File

@ -427,6 +427,9 @@ bool ChatLLM::handleResponse(int32_t token, const std::string &response)
bool ChatLLM::handleRecalculate(bool isRecalc)
{
#if defined(DEBUG)
qDebug() << "recalculate" << m_llmThread.objectName() << isRecalc;
#endif
if (m_isRecalc != isRecalc) {
m_isRecalc = isRecalc;
emit recalcChanged();
@ -597,6 +600,9 @@ void ChatLLM::handleChatIdChanged(const QString &id)
bool ChatLLM::handleNamePrompt(int32_t token)
{
#if defined(DEBUG)
qDebug() << "name prompt" << m_llmThread.objectName() << token;
#endif
Q_UNUSED(token);
qt_noop();
return !m_stopGenerating;
@ -604,6 +610,9 @@ bool ChatLLM::handleNamePrompt(int32_t token)
bool ChatLLM::handleNameResponse(int32_t token, const std::string &response)
{
#if defined(DEBUG)
qDebug() << "name response" << m_llmThread.objectName() << token << response;
#endif
Q_UNUSED(token);
m_nameResponse.append(response);
@ -615,28 +624,40 @@ bool ChatLLM::handleNameResponse(int32_t token, const std::string &response)
bool ChatLLM::handleNameRecalculate(bool isRecalc)
{
#if defined(DEBUG)
qDebug() << "name recalc" << m_llmThread.objectName() << isRecalc;
#endif
Q_UNUSED(isRecalc);
Q_UNREACHABLE();
return !m_stopGenerating;
return false;
}
bool ChatLLM::handleSystemPrompt(int32_t token)
{
#if defined(DEBUG)
qDebug() << "system prompt" << m_llmThread.objectName() << token << m_stopGenerating;
#endif
Q_UNUSED(token);
return !m_stopGenerating;
}
bool ChatLLM::handleSystemResponse(int32_t token, const std::string &response)
{
#if defined(DEBUG)
qDebug() << "system response" << m_llmThread.objectName() << token << response << m_stopGenerating;
#endif
Q_UNUSED(token);
Q_UNUSED(response);
return !m_stopGenerating;
return false;
}
bool ChatLLM::handleSystemRecalculate(bool isRecalc)
{
#if defined(DEBUG)
qDebug() << "system recalc" << m_llmThread.objectName() << isRecalc;
#endif
Q_UNUSED(isRecalc);
return !m_stopGenerating;
return false;
}
bool ChatLLM::serialize(QDataStream &stream, int version)

View File

@ -94,6 +94,7 @@ void Download::updateReleaseNotes()
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
request.setSslConfiguration(conf);
QNetworkReply *jsonReply = m_networkManager.get(request);
connect(qApp, &QCoreApplication::aboutToQuit, jsonReply, &QNetworkReply::abort);
connect(jsonReply, &QNetworkReply::finished, this, &Download::handleReleaseJsonDownloadFinished);
}
@ -137,6 +138,7 @@ void Download::downloadModel(const QString &modelFile)
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
request.setSslConfiguration(conf);
QNetworkReply *modelReply = m_networkManager.get(request);
connect(qApp, &QCoreApplication::aboutToQuit, modelReply, &QNetworkReply::abort);
connect(modelReply, &QNetworkReply::downloadProgress, this, &Download::handleDownloadProgress);
connect(modelReply, &QNetworkReply::finished, this, &Download::handleModelDownloadFinished);
connect(modelReply, &QNetworkReply::readyRead, this, &Download::handleReadyRead);

View File

@ -868,6 +868,7 @@ void ModelList::updateModelsFromJson()
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
request.setSslConfiguration(conf);
QNetworkReply *jsonReply = m_networkManager.get(request);
connect(qApp, &QCoreApplication::aboutToQuit, jsonReply, &QNetworkReply::abort);
QEventLoop loop;
connect(jsonReply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
QTimer::singleShot(1500, &loop, &QEventLoop::quit);
@ -908,6 +909,7 @@ void ModelList::updateModelsFromJsonAsync()
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
request.setSslConfiguration(conf);
QNetworkReply *jsonReply = m_networkManager.get(request);
connect(qApp, &QCoreApplication::aboutToQuit, jsonReply, &QNetworkReply::abort);
connect(jsonReply, &QNetworkReply::finished, this, &ModelList::handleModelsJsonDownloadFinished);
}

View File

@ -115,6 +115,7 @@ bool Network::packageAndSendJson(const QString &ingestId, const QString &json)
QByteArray body(newDoc.toJson(QJsonDocument::Compact));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkReply *jsonReply = m_networkManager.post(request, body);
connect(qApp, &QCoreApplication::aboutToQuit, jsonReply, &QNetworkReply::abort);
connect(jsonReply, &QNetworkReply::finished, this, &Network::handleJsonUploadFinished);
m_activeUploads.append(jsonReply);
return true;
@ -434,6 +435,7 @@ void Network::sendIpify()
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
request.setSslConfiguration(conf);
QNetworkReply *reply = m_networkManager.get(request);
connect(qApp, &QCoreApplication::aboutToQuit, reply, &QNetworkReply::abort);
connect(reply, &QNetworkReply::finished, this, &Network::handleIpifyFinished);
}
@ -449,6 +451,7 @@ void Network::sendMixpanel(const QByteArray &json, bool isOptOut)
request.setSslConfiguration(conf);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkReply *trackReply = m_networkManager.post(request, json);
connect(qApp, &QCoreApplication::aboutToQuit, trackReply, &QNetworkReply::abort);
connect(trackReply, &QNetworkReply::finished, this, &Network::handleMixpanelFinished);
}
@ -512,6 +515,7 @@ void Network::sendHealth()
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
request.setSslConfiguration(conf);
QNetworkReply *healthReply = m_networkManager.get(request);
connect(qApp, &QCoreApplication::aboutToQuit, healthReply, &QNetworkReply::abort);
connect(healthReply, &QNetworkReply::finished, this, &Network::handleHealthFinished);
}