From 14fdfbce857b4dca85bb7a5c033f7bd782dae3ff Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Sun, 7 Sep 2025 05:31:33 -0700 Subject: [PATCH] Proper synchronization for access to destructor hash table --- src/refcount.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/refcount.c b/src/refcount.c index bac38f6..db604d6 100644 --- a/src/refcount.c +++ b/src/refcount.c @@ -765,11 +765,17 @@ bool refcount_context_add_destructor(const RefcountContext *ctx, void *obj, } entry->callback = callback; entry->user_data = user_data; - if (!ht_insert(ENTRY->destructors, key, entry)) { + if (!lock_entry_mtx(ENTRY)) { refcount_free(&ctx->alloc, entry); return false; } - return true; + bool success = true; + if (!ht_insert(ENTRY->destructors, key, entry)) { + refcount_free(&ctx->alloc, entry); + success = false; + } + unlock_entry_mtx(ENTRY); + return success; } /** @@ -782,5 +788,10 @@ bool refcount_context_add_destructor(const RefcountContext *ctx, void *obj, */ bool refcount_context_remove_destructor(const RefcountContext *ctx, void *obj, void *key) { - return ht_remove(ENTRY->destructors, key); + if (!lock_entry_mtx(ENTRY)) { + return false; + } + bool success = ht_remove(ENTRY->destructors, key); + unlock_entry_mtx(ENTRY); + return success; }