From 1b20e538d32bfe64c0868ac6901a3d5c1ab93f10 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Sun, 6 Sep 2026 23:53:53 -0400 Subject: [PATCH 1/4] plutoSDR: Make thread running state atomic m_running is used to synchronize the PlutoSDR manager and runner threads. The manager-side startWork() and stopWork() functions control whether the runner in run() should execute. startWork() reads m_running before starting the QThread and then waits for run() to set it true. stopWork() sets m_running false to request that the runner exit, while run() continuously reads the flag and sets it false when it terminates. These accesses occur across the manager and runner threads without atomic synchronization. Make m_running an atomic_bool in the PlutoSDR input, output, MIMO input, and MIMO output threads to eliminate the data race identified by ThreadSanitizer. This can prevent inconsistent thread state during user-driven device start, stop, and reload operations, where the GUI can cause the manager thread to start or stop a PlutoSDR runner while that runner is processing. Signed-off-by: Robin Getz --- plugins/samplemimo/plutosdrmimo/plutosdrmithread.h | 3 ++- plugins/samplemimo/plutosdrmimo/plutosdrmothread.h | 3 ++- plugins/samplesink/plutosdroutput/plutosdroutputthread.h | 3 ++- plugins/samplesource/plutosdrinput/plutosdrinputthread.h | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/plugins/samplemimo/plutosdrmimo/plutosdrmithread.h b/plugins/samplemimo/plutosdrmimo/plutosdrmithread.h index 32bc8b8ae..38517cfb5 100644 --- a/plugins/samplemimo/plutosdrmimo/plutosdrmithread.h +++ b/plugins/samplemimo/plutosdrmimo/plutosdrmithread.h @@ -21,6 +21,7 @@ #ifndef _PLUTOSDR_PLUTOSDRMITHREAD_H_ #define _PLUTOSDR_PLUTOSDRMITHREAD_H_ +#include #include #include #include @@ -53,7 +54,7 @@ signals: private: QMutex m_startWaitMutex; QWaitCondition m_startWaiter; - bool m_running; + std::atomic_bool m_running; DevicePlutoSDRBox *m_plutoBox; qint16 *m_buf[2]; //!< one buffer per I/Q channel diff --git a/plugins/samplemimo/plutosdrmimo/plutosdrmothread.h b/plugins/samplemimo/plutosdrmimo/plutosdrmothread.h index d557d019f..6e5396bf7 100644 --- a/plugins/samplemimo/plutosdrmimo/plutosdrmothread.h +++ b/plugins/samplemimo/plutosdrmimo/plutosdrmothread.h @@ -21,6 +21,7 @@ #ifndef _PLUTOSDR_PLUTOSDRMOTHREAD_H_ #define _PLUTOSDR_PLUTOSDRMOTHREAD_H_ +#include #include #include #include @@ -52,7 +53,7 @@ signals: private: QMutex m_startWaitMutex; QWaitCondition m_startWaiter; - bool m_running; + std::atomic_bool m_running; DevicePlutoSDRBox *m_plutoBox; qint16 *m_buf[2]; //!< one buffer per I/Q channel diff --git a/plugins/samplesink/plutosdroutput/plutosdroutputthread.h b/plugins/samplesink/plutosdroutput/plutosdroutputthread.h index a1403fbcc..7cc8ed118 100644 --- a/plugins/samplesink/plutosdroutput/plutosdroutputthread.h +++ b/plugins/samplesink/plutosdroutput/plutosdroutputthread.h @@ -21,6 +21,7 @@ #ifndef PLUGINS_SAMPLESOURCE_PLUTOSDROUTPUT_PLUTOSDROUTPUTTHREAD_H_ #define PLUGINS_SAMPLESOURCE_PLUTOSDROUTPUT_PLUTOSDROUTPUTTHREAD_H_ +#include #include #include #include @@ -51,7 +52,7 @@ signals: private: QMutex m_startWaitMutex; QWaitCondition m_startWaiter; - bool m_running; + std::atomic_bool m_running; DevicePlutoSDRBox *m_plutoBox; int16_t *m_buf; //!< holds I+Q values of each sample from device diff --git a/plugins/samplesource/plutosdrinput/plutosdrinputthread.h b/plugins/samplesource/plutosdrinput/plutosdrinputthread.h index 57ea016dd..8aa78a8df 100644 --- a/plugins/samplesource/plutosdrinput/plutosdrinputthread.h +++ b/plugins/samplesource/plutosdrinput/plutosdrinputthread.h @@ -21,6 +21,7 @@ #ifndef PLUGINS_SAMPLESOURCE_PLUTOSDRINPUT_PLUTOSDRINPUTTHREAD_H_ #define PLUGINS_SAMPLESOURCE_PLUTOSDRINPUT_PLUTOSDRINPUTTHREAD_H_ +#include #include #include #include @@ -53,7 +54,7 @@ signals: private: QMutex m_startWaitMutex; QWaitCondition m_startWaiter; - bool m_running; + std::atomic_bool m_running; DevicePlutoSDRBox *m_plutoBox; int16_t *m_buf; //!< holds I+Q values of each sample from device From 324a3271a4ed44c9d3960f16107889a8ac2f94de Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Sun, 6 Sep 2026 23:57:34 -0400 Subject: [PATCH 2/4] adsbdemodsink: Protect magnitude statistics with mutex ADSBDemodSink::processOneSample() updates m_magsqSum, m_magsqPeak, and m_magsqCount from the sample processing path, while getMagSqLevels() reads and updates the same statistics when the GUI requests the current magnitude levels. Thus, two different threads can read and write the same variables concurrently. Protect both access paths with a new m_magsqMutex to prevent concurrent updates and reads of the magnitude statistics. This eliminates the data race identified by ThreadSanitizer and prevents the GUI from observing partially updated magnitude statistics while ADS-B samples are being processed. Signed-off-by: Robin Getz --- plugins/channelrx/demodadsb/adsbdemodsink.cpp | 12 ++++++++---- plugins/channelrx/demodadsb/adsbdemodsink.h | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/plugins/channelrx/demodadsb/adsbdemodsink.cpp b/plugins/channelrx/demodadsb/adsbdemodsink.cpp index fa599f1e0..024643769 100644 --- a/plugins/channelrx/demodadsb/adsbdemodsink.cpp +++ b/plugins/channelrx/demodadsb/adsbdemodsink.cpp @@ -121,10 +121,14 @@ void ADSBDemodSink::feed(const SampleVector::const_iterator& begin, const Sample void ADSBDemodSink::processOneSample(Real magsq) { - m_magsqSum += magsq; - if (magsq > m_magsqPeak) - m_magsqPeak = magsq; - m_magsqCount++; + { + QMutexLocker locker(&m_magsqMutex); + + m_magsqSum += magsq; + if (magsq > m_magsqPeak) + m_magsqPeak = magsq; + m_magsqCount++; + } m_sampleBuffer[m_writeBuffer][m_writeIdx] = magsq; m_writeIdx++; if (!m_bufferDateTimeValid[m_writeBuffer]) diff --git a/plugins/channelrx/demodadsb/adsbdemodsink.h b/plugins/channelrx/demodadsb/adsbdemodsink.h index c57e312d5..fc458bf25 100644 --- a/plugins/channelrx/demodadsb/adsbdemodsink.h +++ b/plugins/channelrx/demodadsb/adsbdemodsink.h @@ -35,6 +35,8 @@ public: void getMagSqLevels(double& avg, double& peak, int& nbSamples) { + QMutexLocker locker(&m_magsqMutex); + if (m_magsqCount > 0) { m_magsq = m_magsqSum / m_magsqCount; @@ -107,6 +109,8 @@ private: int m_magsqCount; MagSqLevelsStore m_magSqLevelStore; + QMutex m_magsqMutex; + MessageQueue *m_messageQueueToGUI; MessageQueue *m_messageQueueToWorker; From 121836d69afd5b29962e380817e32b3ba8ecd00b Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Mon, 7 Sep 2026 00:02:27 -0400 Subject: [PATCH 3/4] glspectrumview: Make display change flag atomic ThreadSanitizer identified a race on GLSpectrumView::m_displayChanged between the GUI thread and the spectrum processing thread. The spectrum processing path sets m_displayChanged when display configuration changes, while the GUI update path reads and clears the flag in GLSpectrumView::tick(). These accesses can occur concurrently when the display is being updated, allowing tick() to read or clear m_displayChanged while another thread is setting it. Make m_displayChanged an atomic_bool so the cross-thread notification between the spectrum processing and GUI threads is synchronized. This eliminates the data race identified by ThreadSanitizer and ensures display-change notifications are not subject to unsynchronized reads and writes. Signed-off-by: Robin Getz --- sdrgui/gui/glspectrumview.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdrgui/gui/glspectrumview.h b/sdrgui/gui/glspectrumview.h index c06107d50..66db9b67f 100644 --- a/sdrgui/gui/glspectrumview.h +++ b/sdrgui/gui/glspectrumview.h @@ -26,6 +26,7 @@ #ifndef INCLUDE_GLSPECTRUMVIEW_H #define INCLUDE_GLSPECTRUMVIEW_H +#include #include #include #include @@ -431,7 +432,7 @@ private: QMatrix4x4 m_glHistogramSpectrumMatrix; QMatrix4x4 m_glHistogramBoxMatrix; bool m_displayHistogram; - bool m_displayChanged; + std::atomic_bool m_displayChanged; bool m_displaySourceOrSink; int m_displayStreamIndex; float m_frequencyZoomFactor; From 5549dd2c5c6b67ed5919c55c9ddc1de6a788a616 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Mon, 7 Sep 2026 00:12:40 -0400 Subject: [PATCH 4/4] dspdevicesinkengine: Make DSPDeviceSinkEngine m_state atomic DSPDeviceSinkEngine::setState() updates m_state while state() and external users of the engine read the same variable. These accesses can occur from different threads without atomic synchronization. Make m_state an atomic State to eliminate the data race identified by ThreadSanitizer. This prevents callers from observing an unsynchronized DSP device sink engine state while the engine transitions between states. Signed-off-by: Robin Getz --- sdrbase/dsp/dspdevicesinkengine.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdrbase/dsp/dspdevicesinkengine.h b/sdrbase/dsp/dspdevicesinkengine.h index d8c100bff..fc84d2010 100644 --- a/sdrbase/dsp/dspdevicesinkengine.h +++ b/sdrbase/dsp/dspdevicesinkengine.h @@ -22,6 +22,7 @@ #ifndef SDRBASE_DSP_DSPDEVICESINKENGINE_H_ #define SDRBASE_DSP_DSPDEVICESINKENGINE_H_ +#include #include #include #include @@ -83,7 +84,7 @@ private: MessageQueue m_inputMessageQueue; // m_state; QString m_errorMessage; QString m_deviceDescription;