From d337c496d5fc6791beec0adf8ec75cb26d20df0e Mon Sep 17 00:00:00 2001 From: Patrick Avery Date: Wed, 13 May 2026 16:31:37 -0500 Subject: [PATCH 1/2] Fix numba warning on Linux for using TBB This fixes the following numba warning on Linux: ``` Numba: Attempted to fork from a non-main thread, the TBB library may be in an invalid state in the child process. ``` The main idea is that forked subprocesses (which we indeed do, in order to take advantage of copy-on-write mechanics and let subprocesses share data), which only happen on Linux (Windows and Mac do "spawn"), would always print this numba warning, even though we were never actually using TBB in an invalid way. There's no way to suppress this warning, so we switch to OpenMP on Linux, which is just as performant. In fact, it has one better feature too: it will crash if we try to run numba in parallel mode on a forked subprocess. TBB was just undefined, but the OpenMP implementation will actually crash, which prevents developers from ever doing that. Crossref: https://numba.discourse.group/t/fork-from-non-main-thread-warning/1753 Signed-off-by: Patrick Avery --- hexrdgui/main.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/hexrdgui/main.py b/hexrdgui/main.py index 9b9fb707e..0dcfd66d4 100644 --- a/hexrdgui/main.py +++ b/hexrdgui/main.py @@ -14,6 +14,19 @@ # Prevent crashing when using OpenBLAS os.environ['OPENBLAS_NUM_THREADS'] = '1' +if sys.platform == 'linux': + # Use OpenMP instead of TBB as the numba threading layer on Linux. + # TBB prints an unavoidable C-level warning to stderr on every + # non-main-thread fork ("Attempted to fork from a non-main thread"), + # which is harmless but noisy in our Qt background thread workflow. + # OpenMP is equally thread-safe and performant, emits no fork + # warning, and has a built-in safety net: it terminates any forked + # child that accidentally calls a numba parallel=True function, + # rather than silently running with undefined behavior. + # workqueue is not viable (crashes on concurrent thread access). + # macOS/Windows use spawn, so TBB's warning never appears there. + os.environ.setdefault('NUMBA_THREADING_LAYER', 'omp') + from PySide6.QtCore import QCoreApplication, Qt from PySide6.QtGui import QIcon, QPixmap from PySide6.QtWidgets import QApplication From a8eba67c092f6ace14985cac17868cbca5f8fe47 Mon Sep 17 00:00:00 2001 From: Patrick Avery Date: Wed, 20 May 2026 14:20:21 -0500 Subject: [PATCH 2/2] Also use OpenMP on macos Signed-off-by: Patrick Avery --- hexrdgui/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hexrdgui/main.py b/hexrdgui/main.py index 0dcfd66d4..2f6ee0cde 100644 --- a/hexrdgui/main.py +++ b/hexrdgui/main.py @@ -14,8 +14,8 @@ # Prevent crashing when using OpenBLAS os.environ['OPENBLAS_NUM_THREADS'] = '1' -if sys.platform == 'linux': - # Use OpenMP instead of TBB as the numba threading layer on Linux. +if sys.platform in ('linux', 'darwin'): + # Use OpenMP instead of TBB as the numba threading layer. # TBB prints an unavoidable C-level warning to stderr on every # non-main-thread fork ("Attempted to fork from a non-main thread"), # which is harmless but noisy in our Qt background thread workflow. @@ -24,7 +24,6 @@ # child that accidentally calls a numba parallel=True function, # rather than silently running with undefined behavior. # workqueue is not viable (crashes on concurrent thread access). - # macOS/Windows use spawn, so TBB's warning never appears there. os.environ.setdefault('NUMBA_THREADING_LAYER', 'omp') from PySide6.QtCore import QCoreApplication, Qt