Skip to content

chore: add re-entry guard to StreamManager::optimize#1052

Open
thlorenz wants to merge 6 commits intomasterfrom
thlorenz/grpc-optimize-reentrancy
Open

chore: add re-entry guard to StreamManager::optimize#1052
thlorenz wants to merge 6 commits intomasterfrom
thlorenz/grpc-optimize-reentrancy

Conversation

@thlorenz
Copy link
Collaborator

@thlorenz thlorenz commented Mar 16, 2026

Summary

Add a defensive re-entry guard to StreamManager::optimize to protect against concurrent calls
and future refactoring risks.

Details

The optimize() method in StreamManager operates on mutable state and is intended to be
called only once at a time within the single tokio::select! loop. While re-entry should currently
be impossible due to the &mut self receiver, this change adds an optimizing boolean flag as a
defensive measure.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Improved stream optimization stability by implementing concurrency safeguards that prevent simultaneous optimization operations, reducing conflicts and enhancing system reliability during optimization cycles.

@github-actions
Copy link

github-actions bot commented Mar 16, 2026

Manual Deploy Available

You can trigger a manual deploy of this PR branch to testnet:

Deploy to Testnet 🚀

Alternative: Comment /deploy on this PR to trigger deployment directly.

⚠️ Note: Manual deploy requires authorization. Only authorized users can trigger deployments.

Comment updated automatically when the PR is synchronized.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 16, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9be5c994-4a15-4c67-a46a-06554b9fda3b

📥 Commits

Reviewing files that changed from the base of the PR and between 1603ca7 and 4d3ac84.

📒 Files selected for processing (1)
  • magicblock-chainlink/src/remote_account_provider/chain_laser_actor/stream_manager.rs

📝 Walkthrough

Walkthrough

The changes add re-entrancy protection to the StreamManager.optimize method in stream_manager.rs. A new boolean field optimizing is introduced to guard against concurrent optimization calls. The optimize method is refactored into a wrapper that manages the flag state and a new optimize_inner helper containing the original optimization logic. When optimize is invoked while already optimizing, it returns early with a trace log. The flag is reset to false both after optimization completes and when account subscriptions are cleared.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch thlorenz/grpc-optimize-reentrancy
📝 Coding Plan
  • Generate coding plan for human review comments

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can customize the tone of the review comments and chat replies.

Configure the tone_instructions setting to customize the tone of the review comments and chat replies. For example, you can set the tone to Act like a strict teacher, Act like a pirate and more.

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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@magicblock-chainlink/src/remote_account_provider/chain_laser_actor/stream_manager.rs`:
- Around line 409-410: The optimize() method sets self.optimizing = true but can
return early on errors (e.g., when awaiting subscribe(...).await? or other
failure paths) leaving optimizing stuck true; update optimize() to ensure
self.optimizing is reset to false on every exit path (both success and error) —
use a scoped guard or finally-style cleanup (e.g., a Drop guard or a defer-like
closure) to set self.optimizing = false, and apply the same pattern to the
surrounding async flows that set optimizing (references: optimize(), the
subscribe(...).await? call site, and any places that assign self.optimizing =
true) so no early-return path leaves optimizing true.
🪄 Autofix (Beta)

✅ Autofix completed


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 22fffb2a-0d0a-43af-83c0-255d94afb203

📥 Commits

Reviewing files that changed from the base of the PR and between 82111ad and 964cef8.

📒 Files selected for processing (1)
  • magicblock-chainlink/src/remote_account_provider/chain_laser_actor/stream_manager.rs

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 16, 2026

Note

Autofix is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it.

Fixes Applied Successfully

Fixed 2 file(s) based on 1 unresolved review comment.

Files modified:

  • magicblock-chainlink/src/remote_account_provider/chain_laser_actor/mock.rs
  • magicblock-chainlink/src/remote_account_provider/chain_laser_actor/stream_manager.rs

Commit: 1603ca75a0bf4cea49d942a44157f93555a970b8

The changes have been pushed to the thlorenz/grpc-optimize-reentrancy branch.

Time taken: 8m 28s

Fixed 2 file(s) based on 1 unresolved review comment.

Co-authored-by: CodeRabbit <noreply@coderabbit.ai>
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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@magicblock-chainlink/src/remote_account_provider/chain_laser_actor/mock.rs`:
- Around line 137-143: The code returns a non-existent error variant
RemoteAccountProviderError::Other causing a compile error; replace that with an
existing variant such as RemoteAccountProviderError::AccountSubscriptionsFailed
(or another appropriate existing variant) in the mock subscribe failure branch
so the function returns a valid enum variant; update the Err(...) in the
should_fail block to construct
RemoteAccountProviderError::AccountSubscriptionsFailed with a descriptive
message (e.g., "mock subscribe failure") so the mock compiles and preserves the
original error text.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: f6cd5928-ef79-44f3-98c6-49d53c8f7099

📥 Commits

Reviewing files that changed from the base of the PR and between 4dde7ed and 1603ca7.

📒 Files selected for processing (2)
  • magicblock-chainlink/src/remote_account_provider/chain_laser_actor/mock.rs
  • magicblock-chainlink/src/remote_account_provider/chain_laser_actor/stream_manager.rs

@GabrielePicco GabrielePicco marked this pull request as draft March 16, 2026 07:35
@thlorenz thlorenz marked this pull request as ready for review March 16, 2026 14:39
@GabrielePicco GabrielePicco removed their assignment Mar 17, 2026
@GabrielePicco GabrielePicco self-requested a review March 17, 2026 08:15
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