Skip to content

src: fix thread-safety issues @ ContinuousProfiler#309

Closed
santigimeno wants to merge 1 commit intonode-v22.x-nsolid-v5.xfrom
santi/continuous_profiling_thread_safety
Closed

src: fix thread-safety issues @ ContinuousProfiler#309
santigimeno wants to merge 1 commit intonode-v22.x-nsolid-v5.xfrom
santi/continuous_profiling_thread_safety

Conversation

@santigimeno
Copy link
Copy Markdown
Member

@santigimeno santigimeno commented May 7, 2025

Summary by CodeRabbit

  • Refactor
    • Simplified continuous profiling control flow and enhanced thread safety for more reliable performance.
  • Bug Fixes
    • Improved thread handling during profiling for increased stability and robustness.

@santigimeno santigimeno self-assigned this May 7, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 7, 2025

Walkthrough

The changes refactor the ContinuousProfiler class to streamline and centralize the logic for starting and stopping CPU profiling. Helper methods for conditional profiling control are removed, replaced by a new should_start() method. Thread safety is enhanced with improved mutex usage, and the profiling interval is now managed atomically.

Changes

File(s) Change Summary
src/nsolid/continuous_profiler.cc Removed start_if_needed() and stop_if_needed() methods; replaced their usage with should_start() checks; updated Enable(), Disable(), IsEnabled(), register_hook_impl(), UnregisterHook(), Initialize(), on_prepare(), on_thread_removed(), and start_cpu_profiling_for_thread() for new control flow and improved thread safety with mutex protection around pending_threads_. Profiling stop logic simplified by removing explicit stop calls.
src/nsolid/continuous_profiler.h Removed private methods start_if_needed() and stop_if_needed(); added private inline method should_start() returning a boolean; changed interval_ member from uint64_t to std::atomic<uint64_t> for thread-safe interval management.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ContinuousProfiler
    participant Thread

    User->>ContinuousProfiler: Enable(interval_ms)
    ContinuousProfiler->>ContinuousProfiler: Acquire lock, set enabled_
    ContinuousProfiler->>ContinuousProfiler: Check should_start()
    alt should_start() true
        ContinuousProfiler->>ContinuousProfiler: start_cpu_profiling()
    end

    User->>ContinuousProfiler: register_hook_impl(callback)
    ContinuousProfiler->>ContinuousProfiler: Acquire lock, add callback
    ContinuousProfiler->>ContinuousProfiler: Check should_start()
    alt should_start() true
        ContinuousProfiler->>ContinuousProfiler: start_cpu_profiling()
    end

    User->>ContinuousProfiler: Disable()
    ContinuousProfiler->>ContinuousProfiler: Acquire lock, set enabled_ = false

    User->>ContinuousProfiler: UnregisterHook(hook_id)
    ContinuousProfiler->>ContinuousProfiler: Acquire lock, remove callback
Loading

Poem

In the warren, code hops anew,
Old helpers gone, a streamlined view.
With locks and checks, the threads align,
Profiling starts at just the right time.
Atomic intervals keep us safe and bright—
A rabbit’s code runs smooth and light!
🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 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 adce98e and e43a523.

📒 Files selected for processing (2)
  • src/nsolid/continuous_profiler.cc (7 hunks)
  • src/nsolid/continuous_profiler.h (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/nsolid/continuous_profiler.h
  • src/nsolid/continuous_profiler.cc
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: lint-js-and-md
  • GitHub Check: test-linux (ubuntu-24.04-arm)
  • GitHub Check: coverage-linux
  • GitHub Check: test-macOS
  • GitHub Check: coverage-linux-without-intl
  • GitHub Check: build-docs
  • GitHub Check: build-tarball
  • GitHub Check: test-linux (ubuntu-24.04)
  • GitHub Check: coverage-windows
✨ 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.

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
Copy Markdown

@windsurf-bot windsurf-bot Bot left a comment

Choose a reason for hiding this comment

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

Other comments: 0 Blocking, 0 Optional, 0 Nit, 2 FYI
  • FYI: src/nsolid/continuous_profiler.cc (71-71) FYI: The code uses `should_start()` method in several places (lines 61, 71, 90) but this method isn't defined in the visible code. Make sure this method is properly implemented.
  • FYI: src/nsolid/continuous_profiler.cc (171-171) FYI: The `pending_threads_.erase(thread_id)` call is already protected by the `thread_mutex_` lock at the beginning of the `on_thread_removed()` method. No need for additional locking here.

uv_loop_t* loop_ = nullptr;
uint64_t interval_ = 60000; // Default: 1 minute per configuration
std::atomic<uint64_t> interval_ = 60000; // 1 min
bool enabled_ = false; // Disabled by default per config
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The enabled_ member variable should be declared as std::atomic<bool> for thread safety, similar to how interval_ was changed to std::atomic<uint64_t>.

@santigimeno santigimeno force-pushed the santi/continuous_profiling_thread_safety branch from 7da3c8c to adce98e Compare May 7, 2025 15:01
@santigimeno santigimeno force-pushed the santi/continuous_profiling_thread_safety branch from adce98e to e43a523 Compare May 12, 2025 14:12
@santigimeno
Copy link
Copy Markdown
Member Author

santigimeno added a commit that referenced this pull request Jun 5, 2025
Fixes: nodesource/nsolid-roadmap#31
PR-URL: #309
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
@santigimeno
Copy link
Copy Markdown
Member Author

Landed in 63ce33e

@santigimeno santigimeno closed this Jun 5, 2025
@santigimeno santigimeno deleted the santi/continuous_profiling_thread_safety branch June 5, 2025 13:33
santigimeno added a commit that referenced this pull request Aug 25, 2025
Fixes: nodesource/nsolid-roadmap#31
PR-URL: #309
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
santigimeno added a commit that referenced this pull request Aug 26, 2025
Fixes: nodesource/nsolid-roadmap#31
PR-URL: #309
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
PR-URL: #359
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants