fix: Resolve thread-related QNetworkAccessManager issue (#140)

Fixes "QObject: Cannot create children for a parent that is in a different thread" error by creating QNetworkAccessManager in the same thread where it's used, ensuring proper thread affinity for network operations.
This commit is contained in:
Petr Mironychev 2025-03-16 09:47:04 +01:00 committed by GitHub
parent 91a6a88130
commit 8419577ae5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 3 deletions

View File

@ -27,7 +27,6 @@ namespace QodeAssist::LLMCore {
RequestHandler::RequestHandler(QObject *parent)
: RequestHandlerBase(parent)
, m_manager(new QNetworkAccessManager(this))
{}
void RequestHandler::sendLLMRequest(const LLMConfig &config, const QJsonObject &request)
@ -38,11 +37,12 @@ void RequestHandler::sendLLMRequest(const LLMConfig &config, const QJsonObject &
QString::fromUtf8(
QJsonDocument(config.providerRequest).toJson(QJsonDocument::Indented))));
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkRequest networkRequest(config.url);
config.provider->prepareNetworkRequest(networkRequest);
QNetworkReply *reply
= m_manager->post(networkRequest, QJsonDocument(config.providerRequest).toJson());
= manager->post(networkRequest, QJsonDocument(config.providerRequest).toJson());
if (!reply) {
LOG_MESSAGE("Error: Failed to create network reply");
return;
@ -54,6 +54,7 @@ void RequestHandler::sendLLMRequest(const LLMConfig &config, const QJsonObject &
connect(reply, &QNetworkReply::readyRead, this, [this, reply, request, config]() {
handleLLMResponse(reply, request, config);
});
connect(reply, &QNetworkReply::finished, this, [manager]() { manager->deleteLater(); });
connect(reply, &QNetworkReply::finished, this, [this, reply, requestId]() {
reply->deleteLater();

View File

@ -40,7 +40,6 @@ public:
void handleLLMResponse(QNetworkReply *reply, const QJsonObject &request, const LLMConfig &config);
private:
QNetworkAccessManager *m_manager;
QMap<QString, QNetworkReply *> m_activeRequests;
QMap<QNetworkReply *, QString> m_accumulatedResponses;