From 75cbf798de4f4d8616a3068d736ecfb9b929eb87 Mon Sep 17 00:00:00 2001
From: Abhishek Chanda <abhishek.becs@gmail.com>
Date: Wed, 29 Jul 2026 22:11:40 -0500
Subject: [PATCH] MINOR: Implement setting unique names for threads

This is especially useful for debugging. Normally each thread shows
up with the name haproxy, with this change those would show up as
haproxy-1, haproxy-2 and so on making those easier to observe.

Also updating docs
---
 doc/configuration.txt |  7 +++++++
 doc/management.txt    |  5 +++++
 src/haproxy.c         | 19 +++++++++++++++++++
 3 files changed, 31 insertions(+)

diff --git a/doc/configuration.txt b/doc/configuration.txt
index a8e5192b1..b8624fc19 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -3079,6 +3079,13 @@ nbthread <number>
   output of "haproxy -vv". Note that values set here or automatically detected
   are subject to the limit set by "thread-hard-limit" (if set).
 
+  On Linux and macOS, each thread is assigned an operating-system-visible name
+  of the form "haproxy-N", where N starts at 1 and matches the thread number
+  reported by the Runtime API "show threads" command. These names help identify
+  individual threads in system monitors, profilers, debuggers, and crash dumps.
+  They do not affect scheduling or CPU affinity. Other supported platforms may
+  continue to display the process name for every thread.
+
 numa-cpu-mapping
   When running on a NUMA-aware platform, this enables the "cpu-policy"
   directive to inspect the topology and figure the best set of CPUs to use and
diff --git a/doc/management.txt b/doc/management.txt
index 6cd2f4270..dc186dbfc 100644
--- a/doc/management.txt
+++ b/doc/management.txt
@@ -4319,6 +4319,11 @@ show threads
   alone, it's a different bug like a corrupted list. In all cases the process
   needs is not fully functional anymore and needs to be restarted.
 
+  On Linux and macOS, the operating-system-visible thread name is "haproxy-N",
+  where N is the thread number used by this output. This makes it possible to
+  correlate a thread shown by system monitors, profilers, debuggers, or crash
+  dumps with its corresponding block in this command's output.
+
   The output format is purposely not documented so that it can easily evolve as
   new needs are identified, without having to maintain any form of backwards
   compatibility, and just like with "show activity", the values are meaningless
diff --git a/src/haproxy.c b/src/haproxy.c
index b242be44b..58f342f02 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -3110,6 +3110,24 @@ void run_poll_loop()
 	_HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_IN_LOOP);
 }
 
+#if defined(USE_THREAD) && (defined(__linux__) || defined(__APPLE__))
+static void set_thread_name(void)
+{
+	char name[16];
+
+	snprintf(name, sizeof(name), "haproxy-%u", tid + 1);
+#if defined(__APPLE__)
+	pthread_setname_np(name);
+#else
+	pthread_setname_np(pthread_self(), name);
+#endif
+}
+#else
+static inline void set_thread_name(void)
+{
+}
+#endif
+
 void *run_thread_poll_loop(void *data)
 {
 	struct per_thread_alloc_fct  *ptaf;
@@ -3121,6 +3139,7 @@ void *run_thread_poll_loop(void *data)
 	__decl_thread(static pthread_cond_t  init_cond  = PTHREAD_COND_INITIALIZER);
 
 	ha_set_thread(data);
+	set_thread_name();
 	set_thread_cpu_affinity();
 	clock_set_local_source();
 	ha_random_seed_thread();
-- 
2.55.0

