Skip to content

[maint] Reworked C++03 solution for thread-local error#3315

Open
ethouris wants to merge 1 commit into
Haivision:masterfrom
ethouris:dev-rework-error-object2
Open

[maint] Reworked C++03 solution for thread-local error#3315
ethouris wants to merge 1 commit into
Haivision:masterfrom
ethouris:dev-rework-error-object2

Conversation

@ethouris
Copy link
Copy Markdown
Collaborator

@ethouris ethouris commented May 7, 2026

Fixes #3311

The change:

  1. Completely removed thread-local error facilities split between STDCXX_SYNC and POSIX_SYNC versions.
  2. Added C++11 version and C++03/POSIX version for thread-local facility. The latter is in force only if compiling with enforced C++98 compatibility.
  3. C++11 version: keep the thread-local error object as static thread_local inside the AccessThreadLocalError function. This function then guarantees always possible access to the object for setting and obtaining.
  4. C++98 version: Use the CThreadLocal wrapper to ensure unique and per-thread object initialization:
    • The key is created during initialization of CThreadLocal-wrapped object declared as local static (initialized at the first call of AccessThreadLocalError) by pthread_key_create
    • Every call to AccessThreadLocalError uses CThreadLocal::get to get pointer to the object. The object is lazy-initialized. There exists a possibility that the allocation will fail and the pointer remains NULL. The successfully allocated object is saved by pthread_setspecific under the globally-accessible key so that further calls obtain it by pthread_getspecific.
    • The call to SetThreadLocalError gets first access to the object by get; if this fails, setting is silently ignored.
    • The call to GetThreadLocalError gets first access to the object by get; if this fails, the fallback error object (this time as simply global static object) is returned, preconfigured as memory allocation error
    • The call to ClearThreadLocalError sets the error as success, again, only if get returns the valid object and it's ignored otherwise
    • The destructor of CThreadLocal deletes also the object that is assigned in the current thread; it is believed that this will only happen in the same thread that will exit as the last one, so it's a safety precaution to not access it too late.

NOTE: A small change in the API was also necessary: the CUDT::getLastError now returns the error as constant reference. This shouldn't matter provided that this is an unofficial C++ API only and for the official C API this doesn't change anything. This is a safety precaution, which although the allocation error possible in the POSIX version is highly unlikely, the safety precaution in case when the fallback memory allocation resident error is returned as a fallback, it is not allowed to be modified.

@ethouris ethouris added this to the Patch milestone May 7, 2026
@ethouris ethouris added Type: Maintenance Work required to maintain or clean up the code [core] Area: Changes in SRT library core [API] Area: Changes in SRT library API labels May 7, 2026
@ethouris ethouris modified the milestones: Patch, v1.5.6 May 19, 2026
@ethouris
Copy link
Copy Markdown
Collaborator Author

The failed "ABI checks" is:

Type of return value became const (has been changed from ERRORINFO& to ERRORINFO const&).

This was intended, it's backward-compatible (a possibility to alter this value was never intended) and it fixes an old design flaw. The only risk for ABI change is when anyone tried to use the old compat UDT API and tried to alter this value, which we believe no one does.

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

Labels

[API] Area: Changes in SRT library API [core] Area: Changes in SRT library core Type: Maintenance Work required to maintain or clean up the code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[core] Rework thErr object to prevent usage of global initialization

1 participant