Fix race in POSIX port vPortEndScheduler

The `vPortEndScheduler` checks whether it's a FreeRTOS thread after signalling the scheduler thread to stop. This creates a race between the check and the destruction of the thread key. By moving the signal to the scheduler thread after the check, the race is prevented.
This commit is contained in:
arctic-alpaca 2025-04-02 12:42:05 +02:00
parent 0030d609a4
commit 9dda4781a4
No known key found for this signature in database
GPG key ID: F1634FB43C0FEF7D

View file

@ -329,12 +329,17 @@ void vPortEndScheduler( void )
xTimerTickThreadShouldRun = false; xTimerTickThreadShouldRun = false;
pthread_join( hTimerTickThread, NULL ); pthread_join( hTimerTickThread, NULL );
/* Check whether the current thread is a FreeRTOS thread.
* This has to happen before the scheduler is signaled to exit
* its loop to prevent data races on the thread key. */
BaseType_t is_freertos_thread = prvIsFreeRTOSThread();
/* Signal the scheduler to exit its loop. */ /* Signal the scheduler to exit its loop. */
xSchedulerEnd = pdTRUE; xSchedulerEnd = pdTRUE;
( void ) pthread_kill( hMainThread, SIG_RESUME ); ( void ) pthread_kill( hMainThread, SIG_RESUME );
/* Waiting to be deleted here. */ /* Waiting to be deleted here. */
if( prvIsFreeRTOSThread() == pdTRUE ) if( is_freertos_thread == pdTRUE )
{ {
pxCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() ); pxCurrentThread = prvGetThreadFromTask( xTaskGetCurrentTaskHandle() );
event_wait( pxCurrentThread->ev ); event_wait( pxCurrentThread->ev );