[v1] libcamera: base: thread: Use `pthread_self()` when setting name
diff mbox series

Message ID 20251111084133.2292217-1-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • [v1] libcamera: base: thread: Use `pthread_self()` when setting name
Related show

Commit Message

Barnabás Pőcze Nov. 11, 2025, 8:41 a.m. UTC
There is a data race between `Thread::start()` writing `Thread::thread_`
and `Thread::startThread()` reading it. Avoid it by using `pthread_self()`
to get the id of the current thread instead of using the `thread_` member.

This is at least the second time this issue occurs:
https://lists.libcamera.org/pipermail/libcamera-devel/2025-January/047954.html

Fixes: 559128b1f1b3cf ("Thread: Add name parameter")
Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 src/libcamera/base/thread.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Dan Scally Nov. 11, 2025, 11:11 a.m. UTC | #1
Hi Barnabás

On 11/11/2025 08:41, Barnabás Pőcze wrote:
> There is a data race between `Thread::start()` writing `Thread::thread_`
> and `Thread::startThread()` reading it. Avoid it by using `pthread_self()`
> to get the id of the current thread instead of using the `thread_` member.
> 
> This is at least the second time this issue occurs:
> https://lists.libcamera.org/pipermail/libcamera-devel/2025-January/047954.html
> 
> Fixes: 559128b1f1b3cf ("Thread: Add name parameter")
> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
> ---
Good spot:

Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>

>   src/libcamera/base/thread.cpp | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp
> index 39209ec8ad..b94a3d7490 100644
> --- a/src/libcamera/base/thread.cpp
> +++ b/src/libcamera/base/thread.cpp
> @@ -292,8 +292,7 @@ void Thread::startThread()
>   	currentThreadData = data_.get();
>   
>   	if (!name_.empty())
> -		pthread_setname_np(thread_.native_handle(),
> -				   name_.substr(0, 15).c_str());
> +		pthread_setname_np(pthread_self(), name_.substr(0, 15).c_str());
>   
>   	run();
>   }
Laurent Pinchart Nov. 11, 2025, 11:26 a.m. UTC | #2
On Tue, Nov 11, 2025 at 09:41:33AM +0100, Barnabás Pőcze wrote:
> There is a data race between `Thread::start()` writing `Thread::thread_`
> and `Thread::startThread()` reading it. Avoid it by using `pthread_self()`
> to get the id of the current thread instead of using the `thread_` member.
> 
> This is at least the second time this issue occurs:
> https://lists.libcamera.org/pipermail/libcamera-devel/2025-January/047954.html
> 
> Fixes: 559128b1f1b3cf ("Thread: Add name parameter")
> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>

I didn't see any other potential race condition related to the usage of
thread_, so

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  src/libcamera/base/thread.cpp | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp
> index 39209ec8ad..b94a3d7490 100644
> --- a/src/libcamera/base/thread.cpp
> +++ b/src/libcamera/base/thread.cpp
> @@ -292,8 +292,7 @@ void Thread::startThread()
>  	currentThreadData = data_.get();
>  
>  	if (!name_.empty())
> -		pthread_setname_np(thread_.native_handle(),
> -				   name_.substr(0, 15).c_str());
> +		pthread_setname_np(pthread_self(), name_.substr(0, 15).c_str());
>  
>  	run();
>  }

Patch
diff mbox series

diff --git a/src/libcamera/base/thread.cpp b/src/libcamera/base/thread.cpp
index 39209ec8ad..b94a3d7490 100644
--- a/src/libcamera/base/thread.cpp
+++ b/src/libcamera/base/thread.cpp
@@ -292,8 +292,7 @@  void Thread::startThread()
 	currentThreadData = data_.get();
 
 	if (!name_.empty())
-		pthread_setname_np(thread_.native_handle(),
-				   name_.substr(0, 15).c_str());
+		pthread_setname_np(pthread_self(), name_.substr(0, 15).c_str());
 
 	run();
 }