Threadsafe errno

Daniel81
Posts: 2
Joined: Fri Sep 17, 2021 5:18 pm

Threadsafe errno

Postby Daniel81 » Fri Sep 17, 2021 5:52 pm

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

ESP_Mahavir
Posts: 190
Joined: Wed Jan 24, 2018 6:51 am

Re: Threadsafe errno

Postby ESP_Mahavir » Wed Sep 22, 2021 6:49 am

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?

Who is online

Users browsing this forum: Google [Bot] and 108 guests