Skip to content

fix: add write mutex for log writing to file#943

Closed
ukpratik wants to merge 3 commits intosipeed:mainfrom
ukpratik:fix/logging-issue
Closed

fix: add write mutex for log writing to file#943
ukpratik wants to merge 3 commits intosipeed:mainfrom
ukpratik:fix/logging-issue

Conversation

@ukpratik
Copy link

@ukpratik ukpratik commented Mar 1, 2026

📝 Description

Add per-logger writeMu mutex to ensure safe concurrent file writes

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written (Human lead, AI assisted or none)

Note: Test was fully written by AI and then that test was verified by Human

🔗 Related Issue

Resubmitting -> #191 : fix: minor fix in logger file writing
Fix indirectly relates to Roadmap Item #346

📚 Technical Context (Skip for Docs)

  • Reference URL:
  • Reasoning:

🧪 Test Environment

  • Hardware: Apple Silicon (Apple M3 Pro), 18 GB RAM
  • OS: macOS 14.4 (23E214), arch arm64
  • Model/Provider:
  • Channels:

📸 Evidence (Optional)

Click to view Logs/Screenshots $ go test -race ./pkg/logger -run TestConcurrentFileLogging -count=1 image

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly.

@sipeed-bot sipeed-bot bot added the type: bug Something isn't working label Mar 3, 2026
Copy link

@nikolasdehor nikolasdehor left a comment

Choose a reason for hiding this comment

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

The intent is correct -- adding a mutex to prevent interleaved log writes, especially on Windows. However, there are some issues with the implementation:

  1. Lock ordering concern: The code acquires mu.RLock() (global read lock) then logger.writeMu.Lock() (per-logger write lock). This nested locking is safe here since mu is always acquired first, but the mu.RLock/RUnlock now wraps only the file-writing section and not the console output. Before this change, there was no locking around the file write at all. The mu.RLock/RUnlock placement seems intentional (to prevent the file from being closed/swapped while writing), which is a good defensive improvement.

  2. WriteString allocation: Changed from file.Write(append(jsonData, newline)) to file.WriteString(string(jsonData) + newline). This creates an extra string allocation via string concatenation. The original Write(append(jsonData, newline)) was more efficient. Consider instead:
    logger.writeMu.Lock()
    _, err = fileptr.Write(append(jsonData, newlineByte))
    logger.writeMu.Unlock()

  3. The test is good -- 100 concurrent goroutines writing and verifying all lines appear. Running with -race is the right validation.

  4. Minor: the comment says 'Although POSIX systems typically guarantee atomicity for single write() syscalls' -- this is approximately correct for writes under PIPE_BUF (4096 bytes), but log lines can be larger. The mutex is the right call regardless.

Overall the fix is sound, just the WriteString allocation could be avoided. Not a blocker.

@ukpratik
Copy link
Author

ukpratik commented Mar 4, 2026

WriteString allocation
thanks @nikolasdehor for noticing, I have it fixed now

@CLAassistant
Copy link

CLAassistant commented Mar 5, 2026

CLA assistant check
All committers have signed the CLA.

@yinwm
Copy link
Collaborator

yinwm commented Mar 11, 2026

Closing this PR as it may no longer be needed after #1239 (refactor logger to zerolog) was merged. The zerolog-based file logging implementation should handle concurrent writes properly. If there are still concurrency issues with the new implementation, please open a new PR with the fix based on the current main branch.

@yinwm yinwm closed this Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants