Skip to content

build: fix data race in RotatingLogWriter during startup#10886

Open
Euler-B wants to merge 2 commits into
lightningnetwork:masterfrom
Euler-B:fix/10867-data-race-in-log-rotator
Open

build: fix data race in RotatingLogWriter during startup#10886
Euler-B wants to merge 2 commits into
lightningnetwork:masterfrom
Euler-B:fix/10867-data-race-in-log-rotator

Conversation

@Euler-B

@Euler-B Euler-B commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

The RotatingLogWriter.Write() method called rotator.Write() directly while rotator.Run() executed concurrently in a background goroutine. Both accessed the rotator's internal size field without synchronization, triggering a data race detected by -race.

The fix writes to the io.PipeWriter instead of calling rotator.Write() directly. The pipe is designed for concurrent use and feeds data to Run() through its reader end. A sync.RWMutex is introduced to protect the pipe field from concurrent access between Write, InitLogRotator, and Close. Additionally, Close() now closes the pipe first (setting it to nil for idempotency), waits for the Run goroutine to finish via sync.WaitGroup, then closes the underlying rotator to ensure a clean shutdown.

Fixes #10867

Steps to Test

  1. Build lnd with race detector:
go build -race -o lnd-race ./cmd/lnd
  1. Start btcd in simnet mode (minimal config)

  2. Start lnd with race detector in simnet mode

  3. Verify no data race warnings appear during startup

  4. Run unit tests with race detector:

go test -race ./build/...
  1. Run linter:
make lint

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a data race condition identified during the startup of the log rotator in lnd. By introducing an io.Pipe to handle log data, the writer is now decoupled from the rotator's internal state, ensuring thread-safe operations. Additionally, the shutdown sequence has been improved to ensure all background processes complete before the rotator is closed.

Highlights

  • Data Race Resolution: Replaced direct calls to the rotator's Write method with an io.PipeWriter to decouple the writer from the background rotator goroutine, eliminating the concurrent access to the internal size field.
  • Graceful Shutdown: Updated the Close method to properly signal the pipe to close, wait for the rotator's background goroutine to finish using a sync.WaitGroup, and then safely close the underlying rotator.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the severity-medium Focused review required label Jun 8, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the log rotator to use a sync.WaitGroup to ensure the rotator's background goroutine completes before the rotator is closed, and redirects writes to the pipe instead of the rotator directly. The review feedback highlights critical data races on the pipe field when accessed concurrently across InitLogRotator, Write, and Close. It is recommended to introduce a sync.RWMutex to synchronize access to the pipe field, protecting it from concurrent reads and writes and ensuring safe, idempotent closure.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread build/logrotator.go
Comment thread build/logrotator.go
Comment thread build/logrotator.go Outdated
Comment thread build/logrotator.go Outdated
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

PR Severity: MEDIUM

Automated classification | 1 file | 24 lines changed

Medium (1 file):

  • build/logrotator.go - Build infrastructure / log rotation utility; falls under Other Go files not categorized above

Analysis:

This PR modifies a single file in the build/ package (build/logrotator.go), which is build infrastructure and log rotation support code. This path does not fall into any CRITICAL or HIGH severity category, nor is it documentation, scripts, or test-only code. It is therefore classified as MEDIUM.

With only 1 non-test, non-generated file changed and 24 total lines modified, no severity bump triggers apply.

To override, add a severity-override-{critical,high,medium,low} label.
<!-- pr-severity-bot -->

The RotatingLogWriter.Write() method called rotator.Write() directly
while rotator.Run() executed concurrently in a background goroutine.
Both accessed the rotator's internal state (size field) without
synchronization, triggering a data race detected by -race.

Fix by writing to the io.Pipe writer instead of calling
rotator.Write() directly. The pipe is designed for concurrent use
and feeds data to Run() through its reader end. Close() now closes
the pipe first, waits for the Run goroutine to finish via a
sync.WaitGroup, and then closes the underlying rotator.

Fixes lightningnetwork#10867
@Euler-B Euler-B force-pushed the fix/10867-data-race-in-log-rotator branch 2 times, most recently from 2b6feed to 5987785 Compare June 11, 2026 14:07
@Euler-B Euler-B force-pushed the fix/10867-data-race-in-log-rotator branch from 5987785 to a6c6872 Compare June 12, 2026 13:16
@lightninglabs-deploy

Copy link
Copy Markdown
Collaborator

@Euler-B, remember to re-request review from reviewers when ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

severity-medium Focused review required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: Data Race in Log Rotator During Startup

2 participants