fix: protect Logger.config with sync.RWMutex to prevent data race (#4795) - #4811
Open
waterWang wants to merge 4 commits into
Open
fix: protect Logger.config with sync.RWMutex to prevent data race (#4795)#4811waterWang wants to merge 4 commits into
waterWang wants to merge 4 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #4795 —
os/glog: Logger.SetConfig与并发日志输出(Info/Print 等)存在未加锁数据竞争(-race 确认)Root Cause
Logger结构体的config Config字段是裸字段,没有sync.Mutex/sync.RWMutex/atomic保护。SetConfig直接l.config = config无锁整体赋值,而包内几乎所有对外 API(Info/Print/checkLevel/SetLevel/SetPrefix等)都不经过任何同步直接读写这个字段。Fix
在
Logger结构体中添加sync.RWMutex,保护所有对config字段的并发访问:SetConfig,SetConfigWithMap,SetLevel,SetLevelStr,SetLevelPrefix,SetLevelPrefixes,SetDebug,SetAsync,SetFlags,SetStack,SetStackSkip,SetStackFilter,SetCtxKeys,AppendCtxKeys,SetWriter,SetPath,SetFile,SetTimeFormat,SetStdoutPrint,SetHeaderPrint,SetLevelPrint,SetPrefix,SetHandlers,SetWriterColorEnable,SetStdoutColorDisabled):使用Lock/UnlockGetConfig,GetFlags,GetCtxKeys,GetWriter,GetPath,GetLevel,GetLevelPrefix,getLevelPrefixWithBrackets,checkLevel,GetStack):使用RLock/RUnlockprint,doFinalPrint,printErr):使用RLock/RUnlock,确保日志输出与配置变更并发安全Changes
os/glog/glog_logger.gomu sync.RWMutex到 Logger struct;Clone()、print()、doFinalPrint()、printErr()、GetStack()加锁os/glog/glog_logger_config.goos/glog/glog_logger_level.goSetLevel/GetLevel/SetLevelStr/SetLevelPrefix/SetLevelPrefixes/GetLevelPrefix/getLevelPrefixWithBrackets加锁os/glog/glog_logger_api.gocheckLevel()加 RLockVerification
Bug reporter 提供的最小复现程序(
go run -race)不应再触发 DATA RACE 警告。所有现有测试应继续通过。