Skip to content

logger: avoid mutex contention#20454

Merged
AskAlexSharov merged 11 commits intomainfrom
alex/slog_34
Apr 10, 2026
Merged

logger: avoid mutex contention#20454
AskAlexSharov merged 11 commits intomainfrom
alex/slog_34

Conversation

@AskAlexSharov
Copy link
Copy Markdown
Collaborator

@AskAlexSharov AskAlexSharov commented Apr 10, 2026

an alternative for: #20148

Move mutex from log handler to file writer. Seems we used mutex in handler - to reduce allocs (by using shared buf in the past) - but now we don't use any shared buffer there.

TestStreamHandlerNoContention 5K goroutines: 5s -> 7ms

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to reduce logger mutex contention by removing the global synchronization wrapper from StreamHandler and instead adding locking only where it’s required (rotating file writes), with accompanying tests/bench-oriented checks to catch contention regressions.

Changes:

  • Remove SyncHandler wrapping from StreamHandler to avoid global serialization during logging.
  • Add an internal mutex to rotatingWriter to make rotation (stat/truncate/write/sync) safe under concurrency.
  • Add new tests in bench_test.go to detect mutex contention and measure allocation/concurrency overhead.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
common/log/v3/handler.go Removes handler-level mutex from StreamHandler; adds mutex inside rotatingWriter.Write for safe multi-step rotation.
common/log/v3/bench_test.go Adds contention-focused and allocation/concurrency tests around StreamHandler.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread common/log/v3/handler.go
Comment thread common/log/v3/handler.go Outdated
Comment thread common/log/v3/handler.go
Comment thread common/log/v3/bench_test.go Outdated
Comment thread common/log/v3/bench_test.go
Comment thread common/log/v3/bench_test.go Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread common/log/v3/bench_test.go Outdated
Comment thread common/log/v3/bench_test.go Outdated
Comment thread common/log/v3/bench_test.go Outdated
@AskAlexSharov AskAlexSharov requested a review from awskii April 10, 2026 05:44
Copy link
Copy Markdown
Member

@awskii awskii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving this as better alternative to more complicated solution which also can silently drop logs in case of bursts. Sometimes we do burst logging, especially happens with Debug logging level. Usage allows blocking logging calls which is additional contention, i think this version would be easier to support

@AskAlexSharov AskAlexSharov changed the title [wip] logger mutex contention logger: avoid mutex contention Apr 10, 2026
@AskAlexSharov AskAlexSharov added this pull request to the merge queue Apr 10, 2026
Merged via the queue into main with commit 02e843b Apr 10, 2026
35 checks passed
@AskAlexSharov AskAlexSharov deleted the alex/slog_34 branch April 10, 2026 16:10
AskAlexSharov added a commit that referenced this pull request Apr 11, 2026
AskAlexSharov added a commit that referenced this pull request Apr 11, 2026
AskAlexSharov added a commit that referenced this pull request Apr 18, 2026
AskAlexSharov added a commit that referenced this pull request Apr 18, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Apr 18, 2026
Reverts #20454

reason: gnosis has regression
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.

3 participants