From ec782e3d230241f2c5bbc01fe9f2d3c0b81be4ef Mon Sep 17 00:00:00 2001 From: evgeny Date: Tue, 27 Oct 2026 19:16:30 +0100 Subject: [PATCH] Keep worker callbacks atomic and verify failed shutdown preserves owners --- .../libutun_lite/instance_lite.c | 40 +++++++++---------- tools/chatgui/CMakeLists.txt | 1 + .../tests/test_android_audio_lifecycle.c | 7 ++++ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/tools/chatgui-android/libutun_lite/instance_lite.c b/tools/chatgui-android/libutun_lite/instance_lite.c index 04c855bff..d8991a9b0 100644 --- a/tools/chatgui-android/libutun_lite/instance_lite.c +++ b/tools/chatgui-android/libutun_lite/instance_lite.c @@ -68,6 +68,7 @@ static struct UASYNC* g_ua = NULL; /* C-write, Kotlin-read via __atomic */ static struct UTUN_INSTANCE* g_inst = NULL; /* C-write, Kotlin-read via __atomic */ static pthread_t g_thread; +static __thread int g_is_worker; /* Проверка controller API до захвата блокировки start/stop. */ static pthread_mutex_t join_port_lock = PTHREAD_MUTEX_INITIALIZER; static struct chat_join_port* join_port; static void bind_join_port(struct UTUN_INSTANCE* inst) { @@ -191,13 +192,14 @@ static int ensure_keys(struct utun_config* config) { static void chat_event_forward(struct UTUN_INSTANCE* inst, int type, const uint8_t* data, int len) { (void)inst; - if (g_event_handler) { g_event_handler(type, data, len); return; } - IL_LOGE("[EVT_DIAG] chat_event_forward: g_event_handler is NULL! type=%d len=%d", type, len); + instance_lite_event_fn handler = __atomic_load_n(&g_event_handler, __ATOMIC_ACQUIRE); + if (handler) handler(type, data, len); } static void nodeinfo_event_cb(struct TOPO_GROUP* group, struct TOPO_GROUP_NODE* nq, void* arg) { (void)group; (void)arg; - if (!nq || !g_event_handler) return; + instance_lite_event_fn handler = __atomic_load_n(&g_event_handler, __ATOMIC_ACQUIRE); + if (!nq || !handler) return; uint16_t best_rtt = topo_get_chain_rtt(nq); uint8_t hc = 0; topo_node_best_hop_list(nq, &hc, NULL); @@ -209,21 +211,23 @@ static void nodeinfo_event_cb(struct TOPO_GROUP* group, struct TOPO_GROUP_NODE* memcpy(data + 10, &best_rtt, 2); memcpy(data + 12, &best_rtt, 2); memcpy(data + 14, &bgp_nodes, 2); - g_event_handler(CHAT_EVT_NODEINFO_UPDATED, data, sizeof(data)); + handler(CHAT_EVT_NODEINFO_UPDATED, data, sizeof(data)); } static void fire_local_sockets_event(void) { - if (!g_event_handler) return; + instance_lite_event_fn handler = __atomic_load_n(&g_event_handler, __ATOMIC_ACQUIRE); + if (!handler) return; char* json = utun_bridge_get_local_sockets_json(); if (!json) return; - g_event_handler(CHAT_EVT_LOCAL_SOCKETS, (const uint8_t*)json, (int)strlen(json)); + handler(CHAT_EVT_LOCAL_SOCKETS, (const uint8_t*)json, (int)strlen(json)); u_free(json); } static void on_link_status_changed(struct ETCP_CONN* conn, struct ETCP_LINK* link, int old_state, int old_status, void* arg) { (void)old_state; (void)old_status; (void)arg; - if (!g_event_handler || !conn || conn->peer_node_id == 0) return; + instance_lite_event_fn handler = __atomic_load_n(&g_event_handler, __ATOMIC_ACQUIRE); + if (!handler || !conn || conn->peer_node_id == 0) return; uint8_t data[13]; memcpy(data, &conn->peer_node_id, 8); data[8] = link->local_link_id; @@ -231,7 +235,7 @@ static void on_link_status_changed(struct ETCP_CONN* conn, struct ETCP_LINK* lin data[10] = link->link_status; data[11] = conn->links_up; data[12] = conn->initialized ? 1 : 0; - g_event_handler(CHAT_EVT_LINK_STATUS_CHANGED, data, sizeof(data)); + handler(CHAT_EVT_LINK_STATUS_CHANGED, data, sizeof(data)); fire_local_sockets_event(); } @@ -305,6 +309,7 @@ static void detach_media(struct UTUN_INSTANCE* inst) { } static void* instance_thread(void* arg) { + g_is_worker = 1; char* config_text = (char*)arg; struct UASYNC* my_ua = NULL; struct UTUN_INSTANCE* my_inst = NULL; @@ -419,8 +424,6 @@ poll_current: detach_media(old_inst); utun_instance_destroy(old_inst); my_inst = NULL; - uasync_print_resources(g_ua, "AFTER_DESTROY"); - u_report_unfreed_blocks(); standby_deinit(); udp_log_detach(g_ua); pthread_mutex_lock(&g_stop_mutex); @@ -494,7 +497,6 @@ cleanup: } struct restart_request* stale = __atomic_exchange_n(&g_restart_config, NULL, __ATOMIC_ACQUIRE); if (stale) { u_free(stale->config_text); u_free(stale); } - u_report_unfreed_blocks(); pthread_mutex_lock(&g_stop_mutex); g_thread_exited = 1; DEBUG_INFO(DEBUG_CATEGORY_SYS, "Android worker finished: all native resources released"); @@ -550,6 +552,7 @@ static int start_locked(const char* config_text) { } int instance_lite_start(const char* config_text) { + if (g_is_worker) { IL_LOGE("start refused: must run outside the native worker"); return -1; } pthread_mutex_lock(&g_control_mutex); int result = start_locked(config_text); pthread_mutex_unlock(&g_control_mutex); @@ -557,13 +560,9 @@ int instance_lite_start(const char* config_text) { } int instance_lite_stop(void) { + if (g_is_worker) { IL_LOGE("stop refused: must run outside the native worker"); return -1; } pthread_mutex_lock(&g_control_mutex); if (!g_thread_joinable) { pthread_mutex_unlock(&g_control_mutex); return 0; } - if (pthread_equal(pthread_self(), g_thread)) { - IL_LOGE("stop refused: must run outside the native worker"); - pthread_mutex_unlock(&g_control_mutex); - return -1; - } bind_join_port(NULL); pthread_mutex_lock(&g_stop_mutex); __atomic_store_n(&g_stop, 1, __ATOMIC_RELEASE); @@ -639,6 +638,7 @@ static void log_config_servers(const char* cfg, const char* label) { } static int request_restart(const char* new_config_text, int regenerate_keys) { + if (g_is_worker) { IL_LOGE("restart refused: must run outside the native worker"); return -1; } if (!new_config_text) { IL_LOGE("restart refused: config is NULL"); return -1; } pthread_mutex_lock(&g_control_mutex); pthread_mutex_lock(&g_stop_mutex); @@ -782,10 +782,6 @@ int instance_lite_regenerate_keys(const char* config_text) { } void instance_lite_set_event_handler(instance_lite_event_fn handler) { - IL_LOGI("[EVT_DIAG] instance_lite_set_event_handler handler=%p g_running=%d", (void*)handler, g_running); - g_event_handler = handler; - if (__atomic_load_n(&g_running, __ATOMIC_ACQUIRE)) { - struct UTUN_INSTANCE* inst = (struct UTUN_INSTANCE*)__atomic_load_n(&g_inst, __ATOMIC_ACQUIRE); - if (inst) chat_event_set_handler(inst, chat_event_forward); - } + __atomic_store_n(&g_event_handler, handler, __ATOMIC_RELEASE); + DEBUG_DEBUG(DEBUG_CATEGORY_SYS, "Android event handler enabled=%d", handler != NULL); } diff --git a/tools/chatgui/CMakeLists.txt b/tools/chatgui/CMakeLists.txt index 957af03bf..5cec3f0ed 100644 --- a/tools/chatgui/CMakeLists.txt +++ b/tools/chatgui/CMakeLists.txt @@ -813,6 +813,7 @@ if(UNIX AND NOT APPLE) endforeach() add_executable(test_android_audio_lifecycle tests/test_android_audio_lifecycle.c ../chatgui-android/libutun_lite/instance_lite.c ../chatgui-android/libutun_lite/standby.c + ../chatgui-android/libutun_lite/utun_config_api.c ../chatgui-android/libutun_lite/voice_recorder.c ../chatgui-android/libutun_lite/stream_bridge.c ../chatgui-android/jni_bridge/android_jni_bridge.c ../chatgui-android/jni_bridge/android_udp_log.c) target_include_directories(test_android_audio_lifecycle PRIVATE ${UTUN_INCLUDE_DIRS}) diff --git a/tools/chatgui/tests/test_android_audio_lifecycle.c b/tools/chatgui/tests/test_android_audio_lifecycle.c index a59c17a3e..19dd4f713 100644 --- a/tools/chatgui/tests/test_android_audio_lifecycle.c +++ b/tools/chatgui/tests/test_android_audio_lifecycle.c @@ -9,6 +9,7 @@ #include "u_async.h" #include "../../chatgui-android/libutun_lite/instance_lite.h" #include "../../chatgui-android/jni_bridge/android_jni_bridge.h" +#include "../../chatgui-android/libutun_lite/utun_config_api.h" #include "../../../tests/test_utils.h" #include #include @@ -24,6 +25,7 @@ static unsigned identity_failures; static uint64_t first_node, current_node; static struct UTUN_INSTANCE* last_detached; static int stream_failure, voice_failure; +static const char* lifecycle_string(const char* key, const char* fallback) { return !strcmp(key, "lifecycle") ? "owned" : fallback; } static int pause_loop, pause_instance, pause_attach; static unsigned paused, release_worker, stop_requested, stop_entered, stop_done, joins, self_detaches, finished; @@ -178,6 +180,9 @@ static void inspect(void* arg) { (void)arg; struct UTUN_INSTANCE* inst = instance_lite_get_instance(); observe(inst && inst->call_audio_ops && inst->call_audio_ops->on_media, "ready core without audio provider"); + observe(!strcmp(cfg_get_string("lifecycle", "missing"), "owned"), "config provider unavailable before callback stop"); + observe(utun_bridge_destroy() == -1 && instance_lite_is_running(), "bridge destroyed resources from core callback"); + observe(!strcmp(cfg_get_string("lifecycle", "missing"), "owned"), "failed stop cleared config callbacks"); uint8_t public_key[SC_PUBKEY_SIZE], ed_public[SC_PUBKEY_SIZE], seed[SHA512_DIGEST_LENGTH]; observe(sc_compute_public_key_from_private(inst->my_keys.private_key, public_key) == SC_OK && !memcmp(public_key, inst->my_keys.public_key, sizeof(public_key)) && @@ -214,6 +219,8 @@ int main(int argc, char** argv) { debug_config_init(); debug_set_level(DEBUG_LEVEL_ERROR); debug_set_category_level(DEBUG_CATEGORY_CALL, DEBUG_LEVEL_INFO); utun_instance_set_tun_init_enabled(0); size_t allocated = u_get_allocated_count(); + utun_config_provider_t provider = { .get_string = lifecycle_string }; + utun_config_set_provider(&provider); CHECK_ANDROID(utun_bridge_call_audio_start(42) == -1); char directory[512], config[1200], public_key[65], private_key[65]; const char* temporary = getenv("TMPDIR");