[vlc-devel] [PATCH] input: stats: fix data-race

Thomas Guillem thomas at gllm.fr
Tue Oct 1 15:51:58 CEST 2019


Locks were used for reading but not for writting.

WARNING: ThreadSanitizer: data race (pid=1969)
  Write of size 8 at 0x7b3c000005c8 by thread T6:
    #0 input_rate_Add ../../src/input/stats.c:122 (libvlccore.so.9+0x93036)
    #1 AStreamReadBlock ../../src/input/access.c:239 (libvlccore.so.9+0x55591)
    #2 vlc_stream_ReadRaw ../../src/input/stream.c:439 (libvlccore.so.9+0x9333d)
    #3 vlc_stream_ReadPartial ../../src/input/stream.c:462 (libvlccore.so.9+0x938e2)
    #4 ThreadRead ../../modules/stream_filter/prefetch.c:89 (libprefetch_plugin.so+0x2f8d)
    #5 Thread ../../modules/stream_filter/prefetch.c:253 (libprefetch_plugin.so+0x336f)

  Previous read of size 8 at 0x7b3c000005c8 by thread T4 (mutexes: write M121):
    #0 input_stats_Compute ../../src/input/stats.c:84 (libvlccore.so.9+0x92e4f)
    #1 MainLoopStatistics ../../src/input/input.c:638 (libvlccore.so.9+0x75225)
    #2 MainLoop ../../src/input/input.c:724 (libvlccore.so.9+0x7d8ac)
    #3 Run ../../src/input/input.c:465 (libvlccore.so.9+0x7db97)
---
 src/input/stats.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/input/stats.c b/src/input/stats.c
index fae7f14244..4a79ddc840 100644
--- a/src/input/stats.c
+++ b/src/input/stats.c
@@ -119,6 +119,7 @@ void input_stats_Compute(struct input_stats *stats, input_stats_t *st)
  */
 void input_rate_Add(input_rate_t *counter, uintmax_t val)
 {
+    vlc_mutex_lock(&counter->lock);
     counter->updates++;
     counter->value += val;
 
@@ -126,11 +127,15 @@ void input_rate_Add(input_rate_t *counter, uintmax_t val)
     vlc_tick_t now = vlc_tick_now();
     if (counter->samples[0].date != VLC_TICK_INVALID
      && (now - counter->samples[0].date) < VLC_TICK_FROM_SEC(1))
+    {
+        vlc_mutex_unlock(&counter->lock);
         return;
+    }
 
     memcpy(counter->samples + 1, counter->samples,
            sizeof (counter->samples[0]));
 
     counter->samples[0].value = counter->value;
     counter->samples[0].date = now;
+    vlc_mutex_unlock(&counter->lock);
 }
-- 
2.20.1



More information about the vlc-devel mailing list