From b1dbe714e884deb639ee91ef0925500014d17bd1 Mon Sep 17 00:00:00 2001 From: Jabber Developer Date: Mon, 11 May 2026 21:15:10 +0000 Subject: [PATCH] fix(ai): use atomic ai_provider_ref() in cmd_ai_switch src/command/cmd_funcs:11118 used a plain non-atomic ref_count++ on ai_provider, which is a data race against concurrent ai_provider_unref() calls using g_atomic_int_dec_and_test. - Expose ai_provider_ref() in ai_client.h (was static) - Replace non-atomic ref_count++ with ai_provider_ref() in cmd_ai_switch" --- src/ai/ai_client.c | 2 +- src/ai/ai_client.h | 7 +++++++ src/command/cmd_funcs.c | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ai/ai_client.c b/src/ai/ai_client.c index f49f1ef2..da891e8d 100644 --- a/src/ai/ai_client.c +++ b/src/ai/ai_client.c @@ -205,7 +205,7 @@ ai_provider_new(const gchar* name, const gchar* api_url, const gchar* org_id) return provider; } -static AIProvider* +AIProvider* ai_provider_ref(AIProvider* provider) { if (provider) { diff --git a/src/ai/ai_client.h b/src/ai/ai_client.h index 385b8510..eb41bcce 100644 --- a/src/ai/ai_client.h +++ b/src/ai/ai_client.h @@ -78,6 +78,13 @@ AIProvider* ai_add_provider(const gchar* name, const gchar* api_url, const gchar */ gboolean ai_remove_provider(const gchar* name); +/** + * Increment the reference count of a provider. + * @param provider The provider to reference + * @return The same provider pointer + */ +AIProvider* ai_provider_ref(AIProvider* provider); + /** * Decrement the reference count of a provider. * @param provider The provider to unreference diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index f2de429c..79ffc094 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -11115,7 +11115,7 @@ cmd_ai_switch(ProfWin* window, const char* const command, gchar** args) aiwin->session->provider_name = g_strdup(provider_name); ai_provider_unref(aiwin->session->provider); aiwin->session->provider = new_provider; - aiwin->session->provider->ref_count++; + ai_provider_ref(aiwin->session->provider); } g_free(aiwin->session->model); aiwin->session->model = g_strdup(model);