From f3d59556efd7e4930eb2633277ebe9f969d5cd0a Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Mon, 17 Sep 2018 14:28:58 -0400 Subject: [PATCH] web_backend: protect jwt cache with a mutex --- src/web_service/web_backend.cpp | 2 ++ src/web_service/web_backend.h | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index e935d8b158..3c5d278e86 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -24,6 +24,7 @@ Client::JWTCache Client::jwt_cache{}; Client::Client(const std::string& host, const std::string& username, const std::string& token) : host(host), username(username), token(token) { + std::lock_guard lock(jwt_cache.mutex); if (username == jwt_cache.username && token == jwt_cache.token) { jwt = jwt_cache.jwt; } @@ -116,6 +117,7 @@ void Client::UpdateJWT() { if (result.result_code != Common::WebResult::Code::Success) { LOG_ERROR(WebService, "UpdateJWT failed"); } else { + std::lock_guard lock(jwt_cache.mutex); jwt_cache.username = username; jwt_cache.token = token; jwt_cache.jwt = jwt = result.returned_data; diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h index be115f96ea..955b91c7ab 100644 --- a/src/web_service/web_backend.h +++ b/src/web_service/web_backend.h @@ -5,7 +5,7 @@ #pragma once #include -#include +#include #include #include #include @@ -81,6 +81,7 @@ private: std::unique_ptr cli; struct JWTCache { + std::mutex mutex; std::string username; std::string token; std::string jwt;