Skip to content

Conversation

@rustatian
Copy link
Member

@rustatian rustatian commented May 1, 2025

Reason for This PR

closes: roadrunner-server/roadrunner#2172

Description of Changes

  • Metrics collector returned to the metrics plugin when MetricsCollector method was implemented. But, in our case, we returned not initialized collector when there're no Redis driver.

License Acceptance

By submitting this pull request, I confirm that my contribution is made under
the terms of the MIT license.

PR Checklist

[Author TODO: Meet these criteria.]
[Reviewer TODO: Verify that these criteria are met. Request changes if not]

  • All commits in this PR are signed (git commit -s).
  • The reason for this PR is clearly provided (issue no. or explanation).
  • The description of changes is clear and encompassing.
  • Any required documentation changes (code and docs) are included in this PR.
  • Any user-facing changes are mentioned in CHANGELOG.md.
  • All added/changed functionality is tested.

Summary by CodeRabbit

  • Bug Fixes
    • Improved thread safety for metrics collection, reducing the risk of race conditions and ensuring more reliable performance in concurrent environments.

Signed-off-by: Valery Piashchynski <piashchynski.valery@gmail.com>
@rustatian rustatian added the enhancement New feature or request label May 1, 2025
@rustatian rustatian requested a review from Copilot May 1, 2025 13:51
@rustatian rustatian self-assigned this May 1, 2025
@coderabbitai
Copy link

coderabbitai bot commented May 1, 2025

"""

Walkthrough

The changes refactor the Plugin struct in plugin.go by renaming the embedded sync.RWMutex to a named field rwm and introducing explicit locking for thread-safe access to the metricsCollector field. The KvFromConfig method now uses a write lock when assigning to metricsCollector, and the MetricsCollector method uses a read lock when accessing it. Additionally, a nil check was added to prevent returning a nil collector. These changes address concurrency safety and prevent potential nil pointer dereferences.

Changes

File(s) Change Summary
plugin.go Renamed embedded sync.RWMutex to named field rwm. Added explicit read/write locking and nil check for metricsCollector in KvFromConfig and MetricsCollector methods.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Plugin

    Caller->>Plugin: KvFromConfig()
    Plugin->>Plugin: rwm.Lock()
    Plugin->>Plugin: Set metricsCollector
    Plugin->>Plugin: rwm.Unlock()
    Caller->>Plugin: MetricsCollector()
    Plugin->>Plugin: rwm.RLock()
    Plugin->>Plugin: Check metricsCollector != nil
    Plugin->>Plugin: rwm.RUnlock()
    Plugin-->>Caller: Return metricsCollector or nil
Loading

Assessment against linked issues

Objective Addressed Explanation
Prevent nil pointer dereference and runtime panic when accessing metricsCollector (#2172)
Ensure thread-safe access to metricsCollector (#2172)

Poem

A mutex renamed, a lock in place,
No more panics, no frantic race!
Metrics now guarded, safe and sound,
No nils to trip or errors found.
With every hop, the code feels right—
A rabbit’s work, from day to night!
🐇✨
"""

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a823294 and 9fd3ee6.

📒 Files selected for processing (1)
  • plugin.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • plugin.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Redis plugin (Go stable, PHP 8.4, OS ubuntu-latest)
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses an issue where an uninitialized metrics collector was returned when the Redis driver is absent. Key changes include renaming the mutex field for clarity, adding an extra lock-check in KvFromConfig, and modifying the MetricsCollector method to guard against accessing an uninitialized collector.

Files not reviewed (1)
  • go.work.sum: Language not supported

Signed-off-by: Valery Piashchynski <piashchynski.valery@gmail.com>
Copy link

@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

🧹 Nitpick comments (1)
plugin.go (1)

73-76: Locking is fine, but the comment is misleading

The write-lock is in fact required to avoid a data race when multiple goroutines may call KvFromConfig concurrently.
Consider revising the comment (// should not be needed, but to double check) to reflect the real necessity, otherwise future refactors might be tempted to remove what looks like defensive locking only.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b9c9a33 and a823294.

⛔ Files ignored due to path filters (1)
  • go.work.sum is excluded by !**/*.sum
📒 Files selected for processing (1)
  • plugin.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Redis plugin (Go stable, PHP 8.4, OS ubuntu-latest)
🔇 Additional comments (1)
plugin.go (1)

34-34:

✅ Verification successful

Renaming the anonymous sync.RWMutex can break external callers that locked the struct directly

Exposing the mutex as an anonymous field previously allowed call-sites to do:

p.RLock()

After renaming the field to rwm, such code will no longer compile.
Inside this package you updated the references, but please verify that no other package takes a dependency on the anonymous lock.


🏁 Script executed:

#!/usr/bin/env bash
# Search for direct calls to Plugin.RLock / Plugin.RUnlock outside redis/plugin.go
rg --glob '!redis/plugin.go' -nE '\.RUnlock?\('

Length of output: 446


Let’s rerun the search with the proper pattern to catch both RLock and RUnlock calls outside of redis/plugin.go.


🏁 Script executed:

#!/usr/bin/env bash
# Search for direct calls to RLock/RUnlock outside redis/plugin.go
rg -n --glob '!redis/plugin.go' -e '\.RLock\(' -e '\.RUnlock\(' .

Length of output: 363


🏁 Script executed:

#!/usr/bin/env bash
# Locate plugin.go files and inspect the package declaration
fd plugin.go
sed -n '1,20p' plugin.go

Length of output: 607


Let’s exclude plugin.go itself and rerun the search:


🏁 Script executed:

#!/usr/bin/env bash
# Search for direct calls to RLock/RUnlock outside plugin.go
rg -n --glob '!plugin.go' -e '\.RLock\(' -e '\.RUnlock\(' .

Length of output: 327


No external direct calls to Plugin.RLock() or Plugin.RUnlock() found

Our search across the repository (excluding plugin.go) did not locate any call‐sites using the old anonymous sync.RWMutex on Plugin. The only RLock/RUnlock invocations live in tests/mock/observer.go against a different mu field, so renaming the embedded mutex to rwm won’t break external code.

No further action required.

@rustatian rustatian merged commit bd7aeed into master May 1, 2025
7 checks passed
@rustatian rustatian deleted the fix/incorrect-init-order branch May 1, 2025 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 BUG]: panic: runtime error: invalid memory address or nil pointer dereference in 2025.1.0

2 participants