gh-769: Replace celix_tss usage with libuv key #818
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR replaces the custom
celix_tssimplementation withlibuv'suv_key_tAPI.Current Status
This is a draft because the
libuvTLS implementation lacks a destructor callback, which was previously used to clean upcelix_err_tstructures. Specifically,pthread_key_createallows a destructor registration, butuv_key_createdoes not (likely due to limitations in the underlying Windows TLS implementation).The Problem
Without a destructor,
celix_err_tstructures allocated viamallocincelix_err_getTssErr()are leaked when a thread terminates.Proposed Paths Forward
I have identified four potential strategies to address the leak:
Global Registry: Store created
celix_errstructs in a mutex-protected list. This list is cleaned up in a__attribute__((destructor))function.Thread Monitoring: Store thread IDs in the
celix_errstructure and provide a periodic cleanup function that usespthread_kill(tid, 0)to check if threads are still alive.Consume-to-Cleanup + Global Registry: Free the TLS entry whenever the last error message is consumed (popped). Additionally, include Option 1 to ensure any unconsumed errors are freed at program exit.
malloc/freecalls during error-heavy operations and theoretically does not solve the growing memory issues (if users are not consuming celix_err messages).Platform-Specific Hybrid + (future) Global Registry: Use
libuvkeys for data access but register a "shadow"pthread_keyon Linux/macOS solely to trigger a destructor on thread exit (using a#ifndef _WIN32). For Windows, this would fall back to Option 1 behavior.Current Preference: Option 3.