From d40332de6b3bdc363338ca9ffc12462fc0584837 Mon Sep 17 00:00:00 2001 From: colinChen <709729345@qq.com> Date: Fri, 31 Oct 2025 11:22:01 +0800 Subject: [PATCH] Fix lock handling in GetValue for recursive calls Ensures that the read lock is properly released before making recursive GetValue calls when BlockMode is enabled, preventing potential deadlocks. --- conf.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/conf.go b/conf.go index 657b465..c96cadc 100644 --- a/conf.go +++ b/conf.go @@ -163,9 +163,14 @@ func (c *ConfigFile) DeleteKey(section, key string) bool { // It returns an error and empty string value if the section does not exist, // or key does not exist in DEFAULT and current sections. func (c *ConfigFile) GetValue(section, key string) (string, error) { + unLocked := false if c.BlockMode { c.lock.RLock() - defer c.lock.RUnlock() + defer func() { + if !unLocked { + c.lock.RUnlock() + } + }() } // Blank section name represents DEFAULT section. @@ -185,6 +190,10 @@ func (c *ConfigFile) GetValue(section, key string) (string, error) { if !ok { // Check if it is a sub-section. if i := strings.LastIndex(section, "."); i > -1 { + if c.BlockMode { + unLocked = true + c.lock.RUnlock() + } return c.GetValue(section[:i], key) } @@ -205,6 +214,10 @@ func (c *ConfigFile) GetValue(section, key string) (string, error) { noption = strings.TrimRight(noption, ")s") // Search variable in default section. + if c.BlockMode { + unLocked = true + c.lock.RUnlock() + } nvalue, err := c.GetValue(DEFAULT_SECTION, noption) if err != nil && section != DEFAULT_SECTION { // Search in the same section.