Skip to content

fix: race condition in resetLogLevel#7

Merged
bborbe merged 3 commits intobborbe:masterfrom
openclaw-for-ben:fix/resetloglevel-race-condition
Feb 12, 2026
Merged

fix: race condition in resetLogLevel#7
bborbe merged 3 commits intobborbe:masterfrom
openclaw-for-ben:fix/resetloglevel-race-condition

Conversation

@openclaw-for-ben
Copy link
Copy Markdown
Contributor

Problem

Two issues in logLevelSetter:

1. Data race in resetLogLevel()

func (l *logLevelSetter) Set(...) {
    l.mux.Lock()
    l.lastSetTime = time.Now()  // writes with lock
    l.mux.Unlock()
    go func() {
        <-ctx.Done()
        l.resetLogLevel()
    }()
}

func (l *logLevelSetter) resetLogLevel() {
    if time.Since(l.lastSetTime) <= l.autoResetDuration {  // reads WITHOUT lock!
        ...
    }
}

2. Context cancellation triggers early reset

The reset goroutine inherited the caller's context, so if an HTTP request finished early, the log level would reset prematurely.

Solution

  1. Add l.mux.Lock() to resetLogLevel()
  2. Use context.Background() for the reset timer
  3. Added concurrent access test (run with -race flag)

- Add mutex lock to resetLogLevel() to prevent data race when reading lastSetTime
- Use context.Background() for reset goroutine to ensure reset happens after
  the full duration, regardless of caller's context cancellation
- Add concurrent access test (run with -race flag)
- Fix typo: 'to short' -> 'too short'
@bborbe bborbe merged commit b9d9718 into bborbe:master Feb 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants