Page 1 of 1

Threadsafe errno

Posted: Fri Sep 17, 2021 5:52 pm
by Daniel81
Hello

I am using ESP-IDF 4.3 and need to evaluate the error reason for some library functions (such as for example "read()") by checking the value of "errno".

However, it seems that the errno variable is not thread safe and could report a wrong value set by another task.

I thought about putting the function call and evaluation of errno into a critical section (vTaskSuspendAll() / xTaskResumeAll()), but that did not work, as it is not allowed to use blocking functions inside a critical section.

Another idea would be to store the errno in each tasks local storage, but I have no idea, how to achieve this; especially because some libraries (for example newlib) directly write to the errno variable without using seterrno() routine.
There seems to be some preparation already done in the TCB in freertos/tasks.c

Code: Select all

	#if( configUSE_POSIX_ERRNO == 1 )
		int iTaskErrno;
	#endif
but that member is not referenced anywhere else.

Any idea how to make the errno threadsafe?

I am wondering if I am the only one facing this issue...

Thanks & best regards
Daniel

Re: Threadsafe errno

Posted: Wed Sep 22, 2021 6:49 am
by ESP_Mahavir
However, it seems that the errno variable is not thread safe and could report a wrong value set by another task.
In ESP-IDF, `errno` is thread-local with every task having its own copy. Please refer to https://docs.espressif.com/projects/esp ... r-messages. Any specific reason why you thought that its not thread safe?