Skip to content

gh-769: Replace celix_tss usage with libuv key#818

Draft
pnoltes wants to merge 1 commit intomasterfrom
feature/769-replace-tss-with-uv-key
Draft

gh-769: Replace celix_tss usage with libuv key#818
pnoltes wants to merge 1 commit intomasterfrom
feature/769-replace-tss-with-uv-key

Conversation

@pnoltes
Copy link
Contributor

@pnoltes pnoltes commented Feb 6, 2026

This PR replaces the custom celix_tss implementation with libuv's uv_key_t API.

Current Status

This is a draft because the libuv TLS implementation lacks a destructor callback, which was previously used to clean up celix_err_t structures. Specifically, pthread_key_create allows a destructor registration, but uv_key_create does not (likely due to limitations in the underlying Windows TLS implementation).

The Problem

Without a destructor, celix_err_t structures allocated via malloc in celix_err_getTssErr() are leaked when a thread terminates.

Proposed Paths Forward

I have identified four potential strategies to address the leak:

  1. Global Registry: Store created celix_err structs in a mutex-protected list. This list is cleaned up in a __attribute__((destructor)) function.

    • Cons: Memory only gets reclaimed at program exit, not thread exit. Memory usage grows linearly with the number of unique threads that have ever reported an error.
  2. Thread Monitoring: Store thread IDs in the celix_err structure and provide a periodic cleanup function that uses pthread_kill(tid, 0) to check if threads are still alive.

    • Cons: High complexity and overhead; requires the Celix framework to maintain a background cleanup cycle. Also depends on pthread api.
  3. 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.

    • Cons: This introduces frequent malloc/free calls during error-heavy operations and theoretically does not solve the growing memory issues (if users are not consuming celix_err messages).
  4. Platform-Specific Hybrid + (future) Global Registry: Use libuv keys for data access but register a "shadow" pthread_key on 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.

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 83.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.52%. Comparing base (effe33a) to head (39c86b7).

Files with missing lines Patch % Lines
libs/utils/src/celix_err.c 83.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #818      +/-   ##
==========================================
+ Coverage   91.50%   91.52%   +0.02%     
==========================================
  Files         235      235              
  Lines       28686    28670      -16     
==========================================
- Hits        26248    26241       -7     
+ Misses       2438     2429       -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants