Skip to content

🐛 bug: Add Read Lock around app.Server() #3426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

🐛 bug: Add Read Lock around app.Server() #3426

wants to merge 3 commits into from

Conversation

gaby
Copy link
Member

@gaby gaby commented Apr 25, 2025

Description

  • Add Read Lock around app.Server()

Fixes #3362

@gaby gaby added this to the v3 milestone Apr 25, 2025
@gaby gaby requested a review from a team as a code owner April 25, 2025 12:48
@gaby gaby requested review from sixcolors and ReneWerner87 April 25, 2025 12:48
@gaby gaby added this to v3 Apr 25, 2025
@gaby gaby requested a review from efectn April 25, 2025 12:48
Copy link
Contributor

coderabbitai bot commented Apr 25, 2025

Walkthrough

The App struct's mutex field was changed from a sync.Mutex to a sync.RWMutex to enable concurrent read access. Multiple getter methods (NewCtxFunc, GetRoute, GetRoutes, Config, Handler, Stack, HandlersCount, Server, Hooks, and State) were updated to acquire a read lock (RLock()) at the start and defer unlocking (RUnlock()) before returning. The SetTLSHandler method retains a write lock (Lock()). These changes ensure thread-safe concurrent reads of internal fields without altering method logic or control flow.

Changes

File(s) Change Summary
app.go Changed App.mutex from sync.Mutex to sync.RWMutex. Added RLock()/RUnlock() to multiple getter methods to allow concurrent reads. SetTLSHandler retains Lock() for writes.

Assessment against linked issues

Objective Addressed Explanation
Protect all accesses to app.server with appropriate locking to avoid data races (#3362)

Suggested reviewers

  • ReneWerner87
  • efectn

Poem

🐇 With locks in my paws and code shining bright,
I guard all the fields both by day and by night.
Readers can gather, no race will ensue,
Thanks to RWMutex, concurrency’s true!
A hop and a skip, the app runs just right,
Safe and sound in the fiber’s light! ✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 85cce3c and 973a81d.

📒 Files selected for processing (1)
  • app.go (1 hunks)
🧰 Additional context used
🪛 GitHub Check: unit (1.24.x, macos-13)
app.go

[failure] 965-965:
app.mutex.RUnlock undefined (type "sync".Mutex has no field or method RUnlock)


[failure] 964-964:
app.mutex.RLock undefined (type "sync".Mutex has no field or method RLock)

🪛 GitHub Check: unit (1.23.x, ubuntu-latest)
app.go

[failure] 965-965:
app.mutex.RUnlock undefined (type sync.Mutex has no field or method RUnlock)


[failure] 964-964:
app.mutex.RLock undefined (type sync.Mutex has no field or method RLock)


[failure] 965-965:
app.mutex.RUnlock undefined (type sync.Mutex has no field or method RUnlock)


[failure] 964-964:
app.mutex.RLock undefined (type sync.Mutex has no field or method RLock)

🪛 GitHub Check: govulncheck-check
app.go

[failure] 965-965:
app.mutex.RUnlock undefined (type "sync".Mutex has no field or method RUnlock)


[failure] 964-964:
app.mutex.RLock undefined (type "sync".Mutex has no field or method RLock)

🪛 GitHub Check: unit (1.24.x, ubuntu-latest)
app.go

[failure] 965-965:
app.mutex.RUnlock undefined (type "sync".Mutex has no field or method RUnlock)


[failure] 964-964:
app.mutex.RLock undefined (type "sync".Mutex has no field or method RLock)

🪛 GitHub Check: lint
app.go

[failure] 965-965:
app.mutex.RUnlock undefined (type sync.Mutex has no field or method RUnlock) (typecheck)


[failure] 964-964:
app.mutex.RLock undefined (type sync.Mutex has no field or method RLock)

🪛 GitHub Actions: Run govulncheck
app.go

[error] 964-964: app.mutex.RLock undefined (type "sync".Mutex has no field or method RLock)

🪛 GitHub Actions: golangci-lint
app.go

[error] 964-964: golangci-lint: app.mutex.RLock undefined (type sync.Mutex has no field or method RLock)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: repeated
  • GitHub Check: Compare
  • GitHub Check: Analyse

@gaby gaby moved this to In Progress in v3 Apr 25, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 973a81d and 9f2facc.

📒 Files selected for processing (1)
  • app.go (7 hunks)
🧰 Additional context used
🪛 GitHub Check: lint
app.go

[failure] 903-903:
File is not properly formatted (gofmt)

🪛 GitHub Actions: golangci-lint
app.go

[error] 903-903: File is not properly formatted (gofmt)

⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: unit (1.24.x, macos-13)
  • GitHub Check: unit (1.24.x, ubuntu-latest)
  • GitHub Check: unit (1.24.x, macos-latest)
  • GitHub Check: unit (1.23.x, ubuntu-latest)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: unit (1.23.x, macos-13)
  • GitHub Check: unit (1.23.x, macos-latest)
  • GitHub Check: repeated
  • GitHub Check: Analyse
  • GitHub Check: Compare
🔇 Additional comments (11)
app.go (11)

127-127: Good change: Using RWMutex for concurrent read access.

Changing from sync.Mutex to sync.RWMutex is the correct approach since you want to allow multiple readers to access the app's state concurrently while ensuring that writers still have exclusive access.


635-637: Proper read locking for NewCtxFunc method.

Adding read lock around this getter method is appropriate. It allows concurrent reads while maintaining thread safety.


692-694: Proper read locking for GetRoute method.

Adding read lock around this getter method is appropriate and consistent with the read lock pattern.


708-710: Proper read locking for GetRoutes method.

Adding read lock around this getter method is appropriate and consistent with the read lock pattern.


894-896: Proper read locking for Config method.

Adding read lock around this getter method is appropriate and consistent with the read lock pattern.


915-917: Proper read locking for Stack method.

Adding read lock around this getter method is appropriate and consistent with the read lock pattern.


922-924: Proper read locking for HandlersCount method.

Adding read lock around this getter method is appropriate and consistent with the read lock pattern.


982-984: Proper read locking for Server method - main fix.

This is the primary fix described in the PR objectives. Adding a read lock around app.Server() is correct to address issue #3362 and prevent potential race conditions when reading server state.


989-991: Proper read locking for Hooks method.

Adding read lock around this getter method is appropriate and consistent with the read lock pattern.


996-998: Proper read locking for State method.

Adding read lock around this getter method is appropriate and consistent with the read lock pattern.


656-662: Consistent use of write lock for mutations.

This method correctly continues to use a write lock (Lock/Unlock) since it modifies internal state, while read-only methods use read locks. This shows a good understanding of the read-write mutex pattern.

@gaby
Copy link
Member Author

gaby commented Apr 25, 2025

Closing this for now, tests will never finish with the RWLock

@gaby gaby closed this Apr 25, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Done in v3 Apr 25, 2025
@gaby gaby deleted the server-lock branch April 25, 2025 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: server: protect field access with lock to avoid possible data race
1 participant