build: fix data race in RotatingLogWriter during startup#10886
Conversation
Summary of ChangesHello, 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
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 AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
|
PR Severity: MEDIUM Automated classification | 1 file | 24 lines changed Medium (1 file):
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. |
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
2b6feed to
5987785
Compare
5987785 to
a6c6872
Compare
|
@Euler-B, remember to re-request review from reviewers when ready |
The
RotatingLogWriter.Write()method calledrotator.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. Async.RWMutexis 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 viasync.WaitGroup, then closes the underlying rotator to ensure a clean shutdown.Fixes #10867
Steps to Test
Start btcd in simnet mode (minimal config)
Start lnd with race detector in simnet mode
Verify no data race warnings appear during startup
Run unit tests with race detector:
go test -race ./build/...Pull Request Checklist
Testing
Code Style and Documentation
[skip ci]in the commit message for small changes.📝 Please see our Contribution Guidelines for further guidance.