From c46cfb991845003822d9d40616a6efabb499b662 Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Thu, 3 Jan 2013 12:09:21 -0800 Subject: [PATCH] stats: Another attempt at fixing the race condition in flushing. It appears that sometimes multiple threads are flushing the same dict at the same time. Yikes! This should fix that without double counting. --- r2/r2/lib/stats.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/r2/r2/lib/stats.py b/r2/r2/lib/stats.py index 7f5aaef40..aaadb10e5 100644 --- a/r2/r2/lib/stats.py +++ b/r2/r2/lib/stats.py @@ -53,8 +53,12 @@ class TimingStatBuffer: def flush(self): """Yields accumulated timing and counter data and resets the buffer.""" data, self.data = self.data, collections.defaultdict(complex) - while data: - k, v = data.popitem() + while True: + try: + k, v = data.popitem() + except KeyError: + break + total_time, count = v.real, v.imag yield k, str(int(count)) + '|c' divisor = count or 1