From 1a6ca1caf4401aba81d54c84ddc3e59524ce125e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 20 Jul 2026 21:36:49 +0300 Subject: [PATCH] gh-154283: Make threading.get_native_id() unique across processes on DragonFly On DragonFly BSD lwp_gettid() is only unique within a process (the main thread is always LWP 1), so combine it with the process id, like Solaris. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Library/2026-07-20-21-36-40.gh-issue-154283.Xip7xE.rst | 2 ++ Python/thread_pthread.h | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-20-21-36-40.gh-issue-154283.Xip7xE.rst diff --git a/Misc/NEWS.d/next/Library/2026-07-20-21-36-40.gh-issue-154283.Xip7xE.rst b/Misc/NEWS.d/next/Library/2026-07-20-21-36-40.gh-issue-154283.Xip7xE.rst new file mode 100644 index 00000000000000..a278d67e1824d5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-20-21-36-40.gh-issue-154283.Xip7xE.rst @@ -0,0 +1,2 @@ +On DragonFly BSD, :func:`threading.get_native_id` now returns a value that is +unique across processes, matching the other platforms. diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index 8496f91db2eec2..de93178576e6ec 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -399,8 +399,8 @@ PyThread_get_thread_native_id(void) lwpid_t native_id; native_id = _lwp_self(); #elif defined(__DragonFly__) - lwpid_t native_id; - native_id = lwp_gettid(); + // lwp_gettid() is only unique within a process, so combine it with the pid. + unsigned long native_id = (unsigned long)getpid() << 32 | lwp_gettid(); #elif defined(__sun__) && SIZEOF_LONG >= 8 unsigned long native_id = (unsigned long)getpid() << 32 | thr_self(); #endif