From 3dedd7ce2d5048edde7b6a8016d206d5e377a3b3 Mon Sep 17 00:00:00 2001 From: jeka Date: Sat, 7 Mar 2026 12:21:01 +0300 Subject: [PATCH] race condition fix (win tun) --- lib/ll_queue.c | 16 ++++++++++++---- lib/ll_queue.h | 2 +- lib/u_async.c | 13 +++++++++++++ lib/u_async.h | 3 +++ src/etcp_api.c | 2 +- src/tun_windows.c | 3 ++- 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/ll_queue.c b/lib/ll_queue.c index 94f522c37..6f2281909 100644 --- a/lib/ll_queue.c +++ b/lib/ll_queue.c @@ -270,10 +270,12 @@ static void check_waiters(struct ll_queue* q) { int queue_data_put(struct ll_queue* q, struct ll_entry* entry, uint32_t id) { if (!q || !entry) return -1; +#ifdef QUEUE_THREAD_CHECK queue_check_thread(q); +#endif #ifdef QUEUE_DEBUG - queue_check_consistency(q);// !!!! for debug - BEFORE callback +// queue_check_consistency(q);// !!!! for debug - BEFORE callback #endif entry->id = id; @@ -327,8 +329,10 @@ int queue_data_put(struct ll_queue* q, struct ll_entry* entry, uint32_t id) { int queue_data_put_first(struct ll_queue* q, struct ll_entry* entry, uint32_t id) { if (!q || !entry) return -1; +#ifdef QUEUE_THREAD_CHECK queue_check_thread(q); - +#endif + entry->id = id; // Проверить лимит размера @@ -381,7 +385,9 @@ int queue_data_put_first(struct ll_queue* q, struct ll_entry* entry, uint32_t id struct ll_entry* queue_data_get(struct ll_queue* q) { if (!q || !q->head) return NULL; +#ifdef QUEUE_THREAD_CHECK queue_check_thread(q); +#endif struct ll_entry* entry = q->head; @@ -437,8 +443,8 @@ int queue_check_consistency(struct ll_queue* q) { if (current->next) { if (current->next->prev != current) { // Несоответствие в связях prev/next - DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "Queue '%s': prev/next error at entry %p", - q->name ? q->name : "unknown", (void*)current); + DEBUG_ERROR(DEBUG_CATEGORY_LL_QUEUE, "Queue '%s': prev/next error at entry %p != %p entries: %d!=%d bytes: %zu!=%zu", + q->name ? q->name : "unknown", (void*)current, (void*)current->next->prev, actual_count, q->count, actual_bytes, q->total_bytes); return -1; } } @@ -512,7 +518,9 @@ struct ll_entry* queue_find_data_by_id(struct ll_queue* q, uint32_t id) { int queue_remove_data(struct ll_queue* q, struct ll_entry* entry) { if (!q || !entry) return -1; +#ifdef QUEUE_THREAD_CHECK queue_check_thread(q); +#endif // Удалить из двусвязного списка if (entry->prev) { diff --git a/lib/ll_queue.h b/lib/ll_queue.h index 8539101c7..c8cc5a30e 100644 --- a/lib/ll_queue.h +++ b/lib/ll_queue.h @@ -12,7 +12,7 @@ #endif #define QUEUE_DEBUG 1 -#define QUEUE_THREAD_CHECK 1 // 0 to disable +//#define QUEUE_THREAD_CHECK 1 // 0 to disable /** * @file ll_queue.h diff --git a/lib/u_async.c b/lib/u_async.c index ab3d20ff0..3b8107768 100644 --- a/lib/u_async.c +++ b/lib/u_async.c @@ -1426,6 +1426,19 @@ void uasync_get_stats(struct UASYNC* ua, size_t* timer_alloc, size_t* timer_u_fr if (socket_u_free) *socket_u_free = ua->socket_free_count; } +void uasync_memsync(struct UASYNC* ua) { +#ifdef _WIN32 + EnterCriticalSection(&ua->posted_lock); +#else + pthread_mutex_lock(&ua->posted_lock); +#endif +#ifdef _WIN32 + LeaveCriticalSection(&ua->posted_lock); +#else + pthread_mutex_unlock(&ua->posted_lock); +#endif +} + void uasync_post(struct UASYNC* ua, uasync_post_callback_t callback, void* arg) { if (!ua || !callback) return; diff --git a/lib/u_async.h b/lib/u_async.h index 004fac976..77527fd60 100644 --- a/lib/u_async.h +++ b/lib/u_async.h @@ -108,4 +108,7 @@ int uasync_get_wakeup_fd(struct UASYNC* ua); // returns write fd for wakeup pipe // сообщить async (из другого thread) чтобы он вызвал callback с аргументом void uasync_post(struct UASYNC* ua, uasync_post_callback_t callback, void* user_arg); +// синхронизация памяти (для доступности из других потоков) +void uasync_memsync(struct UASYNC* ua); + #endif // UASYNC_H diff --git a/src/etcp_api.c b/src/etcp_api.c index 8fad74e5b..1d6d4040d 100644 --- a/src/etcp_api.c +++ b/src/etcp_api.c @@ -88,7 +88,7 @@ int etcp_send(struct ETCP_CONN* conn, struct ll_entry* entry) { // Помещаем entry в очередь input normalizer // queue_data_put забирает ownership entry - DEBUG_TRACE(DEBUG_CATEGORY_NORMALIZER, "Before put to input"); +// DEBUG_TRACE(DEBUG_CATEGORY_NORMALIZER, "Before put to input"); int result = queue_data_put(pn->input, entry, 0); DEBUG_TRACE(DEBUG_CATEGORY_NORMALIZER, "After put to input"); diff --git a/src/tun_windows.c b/src/tun_windows.c index a7681548f..714ad0b44 100644 --- a/src/tun_windows.c +++ b/src/tun_windows.c @@ -273,7 +273,8 @@ DWORD WINAPI tun_read_thread_proc(LPVOID arg) memcpy(data + 1, wintun_pkt, size); WintunReleaseReceivePacket(session, wintun_pkt); /* сразу отдаём кольцо */ - struct ll_entry* pkt = queue_entry_new_from_pool(tun->pool); +// struct ll_entry* pkt = queue_entry_new_from_pool(tun->pool);// from pool нельзя - нет thred safe (memory consistency) + struct ll_entry* pkt = queue_entry_new(0); if (!pkt) { u_free(data); tun->read_errors++;