Compare commits

..

2 commits

Author SHA1 Message Date
Muhammad Usman
4e109896c3
Merge b8d21ede7c into 32e581636f 2025-07-24 10:57:52 +05:00
Gaurav-Aggarwal-AWS
32e581636f
Delete thread key on process exit (#1297)
Previously, the shared thread key was deleted in xPortStartScheduler
after scheduler was ended. This created a race condition where
prvThreadKeyDestructor (responsible for freeing thread-specific heap
memory) would not be called if xPortStartScheduler deleted the key
before the last task deletion, as destructors are not invoked after key
deletion (see https://github.com/walac/glibc/blob/master/nptl/pthread_create.c#L145-L150).

Move thread key deletion to process exit to ensure all thread-specific
memory is properly freed.

Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
2025-07-24 11:07:27 +05:30

View file

@ -140,6 +140,8 @@ static void prvThreadKeyDestructor( void * pvData )
static void prvInitThreadKey( void ) static void prvInitThreadKey( void )
{ {
pthread_key_create( &xThreadKey, prvThreadKeyDestructor ); pthread_key_create( &xThreadKey, prvThreadKeyDestructor );
/* Destroy xThreadKey when the process exits. */
atexit( prvDestroyThreadKey );
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/
@ -315,8 +317,6 @@ BaseType_t xPortStartScheduler( void )
/* Restore original signal mask. */ /* Restore original signal mask. */
( void ) pthread_sigmask( SIG_SETMASK, &xSchedulerOriginalSignalMask, NULL ); ( void ) pthread_sigmask( SIG_SETMASK, &xSchedulerOriginalSignalMask, NULL );
prvDestroyThreadKey();
return 0; return 0;
} }
/*-----------------------------------------------------------*/ /*-----------------------------------------------------------*/