Skip to content

CBG-5405: fix race when InitLogging replaces memory loggers - #8852

Open
torcolvin wants to merge 3 commits into
mainfrom
CBG-5405
Open

torcolvin wants to merge 3 commits into
mainfrom
CBG-5405

Conversation

@torcolvin

@torcolvin torcolvin commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

InitLogging copied the memory loggers' buffers without a lock, which raced with any goroutine that was still logging to them. NewFileLogger now takes the logger it replaces. For a memory logger, it calls SetOutput to wait for in-flight writes and forward later writes to the new logger, then copies the buffer. Log writes take no new lock.

Tests that call InitializeMemoryLoggers and then InitLogging can run under the race detector again, so remove the !race build tag from audit_test.go and cluster_compat_audit_test.go. The audit RestTester now disables log collation for every logger, because FlushLogBuffers waits on a collation WaitGroup while background goroutines log.

Pre-review checklist

  • Logging sensitive data? Make sure it's tagged (e.g. base.UD(docID), base.MD(dbName))
  • Updated relevant information in the API specifications (such as endpoint descriptions, schemas, ...) in docs/api

Integration Tests

@torcolvin
torcolvin requested review from a team and a balanced review from Copilot October 5, 2026 19:02
@factory-droid

factory-droid Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Droid finished @torcolvin's task —— View job


Droid review (validator): Validated 0 inline comments (none proposed).

Overall, the logger handoff looks like it fixes the InitLogging memory-buffer race without adding hot-path locking; removing the !race build tags for the audit tests matches that intent.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

Concurrent logger replacement needs focused regression coverage and race-detector validation before approval.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

This PR fixes a race when Sync Gateway replaces startup memory loggers with configured loggers.

Changes:

  • Synchronizes buffer transfer and forwards subsequent writes from retained memory loggers.
  • Passes previous loggers through file and audit logger initialization.
  • Re-enables audit tests under the race detector.
File Description
rest/​cluster_compat_audit_test.go Removes the race-test exclusion.
rest/​audit_test.go Removes the race-test exclusion.
base/​logging_config.go Passes previous logger instances during initialization.
base/​logger_file.go Synchronizes memory-log handoff and forwards later writes.
base/​logger_audit.go Passes the previous audit logger into file-logger initialization.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread base/logger_file.go
InitLogging copied the memory loggers' buffers without a lock, which raced
with any goroutine that was still logging to them. NewFileLogger now takes
the logger it replaces. For a memory logger, it calls SetOutput to wait for
in-flight writes and forward later writes to the new logger, then copies
the buffer. Log writes take no new lock.

Tests that call InitializeMemoryLoggers and then InitLogging can run under
the race detector again, so remove the !race build tag from audit_test.go
and cluster_compat_audit_test.go. The audit RestTester now disables log
collation for every logger, because FlushLogBuffers waits on a collation
WaitGroup while background goroutines log.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@torcolvin
torcolvin requested a review from bbrks October 6, 2026 00:09

This branch has not been deployed

No deployments
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