From e6bbfbdcf0fae26b8599ca3b104fd68809549583 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 28 May 2025 21:17:51 +0200 Subject: [PATCH] Fix locks. --- profiler/src/profiler/TracyLlm.cpp | 12 +++++++----- profiler/src/profiler/TracyLlm.hpp | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/profiler/src/profiler/TracyLlm.cpp b/profiler/src/profiler/TracyLlm.cpp index e05657ec..6510fbe1 100644 --- a/profiler/src/profiler/TracyLlm.cpp +++ b/profiler/src/profiler/TracyLlm.cpp @@ -722,7 +722,7 @@ void TracyLlm::Worker() m_busy = false; break; case Task::SendMessage: - SendMessage(); + SendMessage( lock ); break; default: assert( false ); @@ -799,7 +799,7 @@ void TracyLlm::AddMessage( std::string&& str, const char* role ) m_chat.emplace_back( std::move( msg ) ); } -void TracyLlm::SendMessage() +void TracyLlm::SendMessage( std::unique_lock& lock ) { const auto& models = m_api->GetModels(); const auto ctxSize = models[m_modelIdx].contextSize; @@ -833,10 +833,11 @@ void TracyLlm::SendMessage() AddMessage( "", "assistant" ); - m_lock.unlock(); bool res; try { + lock.unlock(); + nlohmann::json req; req["model"] = m_api->GetModels()[m_modelIdx].name; req["messages"] = m_chat; @@ -844,15 +845,16 @@ void TracyLlm::SendMessage() if( m_setTemperature ) req["temperature"] = m_temperature; res = m_api->ChatCompletion( req, [this]( const nlohmann::json& response ) -> bool { return OnResponse( response ); }, m_modelIdx ); + + lock.lock(); } catch( std::exception& e ) { - m_lock.lock(); + lock.lock(); if( !m_chat.empty() && m_chat.back()["role"].get_ref() == "assistant" ) m_chat.pop_back(); AddMessage( e.what(), "error" ); m_responding = false; m_stop = false; - return; } } diff --git a/profiler/src/profiler/TracyLlm.hpp b/profiler/src/profiler/TracyLlm.hpp index 66bf5175..729702fe 100644 --- a/profiler/src/profiler/TracyLlm.hpp +++ b/profiler/src/profiler/TracyLlm.hpp @@ -64,7 +64,7 @@ private: void ResetChat(); void AddMessage( std::string&& str, const char* role ); - void SendMessage(); + void SendMessage( std::unique_lock& lock ); bool OnResponse( const nlohmann::json& json ); void UpdateCache( ChatCache& cache, const std::string& str );