From 68b1e63202aad0a6b5a74c04d0211312c4e1dc8a Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Tue, 17 May 2022 14:37:03 +0200 Subject: [PATCH] Tsan complained about strftime(), it manulipates locale and/or tz info. So guard if needed. --- pdns/recursordist/rec-main.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index b00a0af1fc..be46da934b 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -827,8 +827,14 @@ static std::string s_timestampFormat = "%s"; static const char* toTimestampStringMilli(const struct timeval& tv, char* buf, size_t sz) { - struct tm tm; - size_t len = strftime(buf, sz, s_timestampFormat.c_str(), localtime_r(&tv.tv_sec, &tm)); + size_t len = 0; + if (s_timestampFormat != "%s") { + // strftime is not thread safe, it can access locale information + static std::mutex m; + auto lock = std::lock_guard(m); + struct tm tm; + len = strftime(buf, sz, s_timestampFormat.c_str(), localtime_r(&tv.tv_sec, &tm)); + } if (len == 0) { len = snprintf(buf, sz, "%lld", static_cast(tv.tv_sec)); } -- 2.47.3