Skip to content

Conversation

@bmuddha
Copy link
Contributor

@bmuddha bmuddha commented Nov 3, 2025

Summary by CodeRabbit

  • Refactor

    • Enhanced internal debugging across encoder and filter types for improved diagnosability.
    • Centralized subscription lifecycle management to ensure consistent registration and cleanup, reducing duplicate/unmatched cleanup and improving connection stability.
  • Chores

    • Adjusted subscription-related exports and visibility to streamline internal wiring and enable the new lifecycle handling.

@bmuddha bmuddha requested a review from GabrielePicco November 3, 2025 11:20
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 3, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Adds Debug bounds/derives for Encoder-related types and refactors WebSocket subscription lifecycle handling by centralizing cleanup registration into WsDispatcher::register_unsub. Adjusts subscription APIs and visibility in the subscriptions module to support the new flow.

Changes

Cohort / File(s) Summary
Debug trait additions
magicblock-aperture/src/encoder.rs, magicblock-aperture/src/utils.rs
Adds std::fmt::Debug bound to Encoder and derives Debug for ProgramAccountEncoder, TransactionResultEncoder, TransactionLogsEncoder, SlotEncoder, ProgramFilter, and ProgramFilters.
Subscription lifecycle refactor (subscribers)
magicblock-aperture/src/requests/websocket/account_subscribe.rs, magicblock-aperture/src/requests/websocket/log_subscribe.rs, magicblock-aperture/src/requests/websocket/program_subscribe.rs, magicblock-aperture/src/requests/websocket/slot_subscribe.rs
Replaces direct inserts into self.unsubs with calls to self.register_unsub(handle) and returns prepared SubResult::SubId(handle.id) after registration.
Dispatcher: centralized registration
magicblock-aperture/src/server/websocket/dispatch.rs
Adds pub(crate) fn register_unsub(&mut self, handle: SubscriptionHandle) and re-exports SubscriptionHandle; new method stores the handle's cleanup in the connection unsubs map, dropping any previous cleanup for the same id.
Subscriptions module updates
magicblock-aperture/src/state/subscriptions.rs
Changes add_subscriber return type from u64 to SubscriptionID; makes CleanUp field pub(crate) so callers can pass SubscriptionHandle cleanup futures to the dispatcher.

Sequence Diagram(s)

sequenceDiagram
    participant Sub as Subscribe Handler
    participant WD as WsDispatcher
    participant Unsubs as per-connection unsubs Map

    rect rgb(240,248,255)
    Note over Sub,Unsubs: Old flow (before)
    Sub->>Unsubs: insert(handle.id -> handle.cleanup)
    Sub-->>Sub: return SubResult::SubId(handle.id)
    end

    rect rgb(245,255,240)
    Note over Sub,WD: New flow (after)
    Sub->>WD: register_unsub(handle)
    WD->>Unsubs: insert(handle.id -> handle.cleanup) (drop prev)
    WD-->>Sub: ack
    Sub-->>Sub: return SubResult::SubId(handle.id)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Verify all subscribe modules use register_unsub consistently and no remaining direct unsubs inserts exist.
  • Confirm register_unsub correctly replaces/drops previous cleanup callbacks and preserves expected semantics across connections.
  • Check trait bound additions compile across crates that implement Encoder.

Possibly related PRs

  • Rpc/execution related metrics #584: Modifies subscription lifecycle handling and CleanUp visibility in the same subscriptions area; likely overlaps in design and API changes.

Suggested reviewers

  • thlorenz

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'fix: don't increment subscription id for dups' directly describes the core change across multiple files in this PR. The changes consistently replace direct insertion into unsubs with a call to register_unsub, which manages duplicate subscription IDs by storing the handle's cleanup callback under handle.id (replacing any existing callback). This is precisely what the title conveys—fixing the behavior to not increment subscription IDs for duplicates. The title is concise, specific, and clearly summarizes the main objective.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b4016b2 and 1e6a48a.

📒 Files selected for processing (1)
  • magicblock-aperture/src/server/websocket/dispatch.rs (2 hunks)

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.

@github-actions
Copy link

github-actions bot commented Nov 3, 2025

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.

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: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1faae18 and b4016b2.

📒 Files selected for processing (8)
  • magicblock-aperture/src/encoder.rs (6 hunks)
  • magicblock-aperture/src/requests/websocket/account_subscribe.rs (1 hunks)
  • magicblock-aperture/src/requests/websocket/log_subscribe.rs (1 hunks)
  • magicblock-aperture/src/requests/websocket/program_subscribe.rs (1 hunks)
  • magicblock-aperture/src/requests/websocket/slot_subscribe.rs (1 hunks)
  • magicblock-aperture/src/server/websocket/dispatch.rs (2 hunks)
  • magicblock-aperture/src/state/subscriptions.rs (2 hunks)
  • magicblock-aperture/src/utils.rs (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: bmuddha
Repo: magicblock-labs/magicblock-validator PR: 578
File: magicblock-aperture/src/requests/websocket/account_subscribe.rs:18-27
Timestamp: 2025-10-21T14:00:54.642Z
Learning: In magicblock-aperture account_subscribe handler (src/requests/websocket/account_subscribe.rs), the RpcAccountInfoConfig fields data_slice, commitment, and min_context_slot are currently ignored—only encoding is applied. This is tracked as technical debt in issue #579: https://github.com/magicblock-labs/magicblock-validator/issues/579
📚 Learning: 2025-10-21T14:00:54.642Z
Learnt from: bmuddha
Repo: magicblock-labs/magicblock-validator PR: 578
File: magicblock-aperture/src/requests/websocket/account_subscribe.rs:18-27
Timestamp: 2025-10-21T14:00:54.642Z
Learning: In magicblock-aperture account_subscribe handler (src/requests/websocket/account_subscribe.rs), the RpcAccountInfoConfig fields data_slice, commitment, and min_context_slot are currently ignored—only encoding is applied. This is tracked as technical debt in issue #579: https://github.com/magicblock-labs/magicblock-validator/issues/579

Applied to files:

  • magicblock-aperture/src/requests/websocket/log_subscribe.rs
  • magicblock-aperture/src/requests/websocket/account_subscribe.rs
  • magicblock-aperture/src/requests/websocket/slot_subscribe.rs
  • magicblock-aperture/src/state/subscriptions.rs
📚 Learning: 2025-10-21T11:00:18.396Z
Learnt from: bmuddha
Repo: magicblock-labs/magicblock-validator PR: 578
File: magicblock-aperture/src/encoder.rs:176-187
Timestamp: 2025-10-21T11:00:18.396Z
Learning: In the magicblock validator, the current slot is always the root slot. The SlotEncoder in magicblock-aperture/src/encoder.rs correctly sets `root: slot` because there is no lag between current and root slots in this architecture.

Applied to files:

  • magicblock-aperture/src/encoder.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: run_make_ci_lint
  • GitHub Check: Build Project
  • GitHub Check: run_make_ci_test
🔇 Additional comments (11)
magicblock-aperture/src/requests/websocket/program_subscribe.rs (1)

46-50: LGTM: Centralized lifecycle management.

The refactored subscription lifecycle using register_unsub correctly implements the duplicate subscription ID fix. When the same connection subscribes to the same program with the same encoder, subscribe_to_program will return a handle with the existing subscription ID, and register_unsub will properly replace any old cleanup callback.

magicblock-aperture/src/requests/websocket/account_subscribe.rs (1)

35-39: LGTM: Consistent lifecycle management pattern.

The subscription lifecycle refactoring correctly prevents duplicate subscription ID allocation when the same connection subscribes multiple times to the same account with the same encoder.

magicblock-aperture/src/requests/websocket/log_subscribe.rs (1)

38-42: LGTM: Lifecycle management correctly applied.

The refactoring correctly uses register_unsub to manage subscription cleanup, aligning with the duplicate ID prevention mechanism.

magicblock-aperture/src/requests/websocket/slot_subscribe.rs (1)

10-14: LGTM: Consistent refactoring.

The subscription lifecycle management is correctly implemented using the centralized register_unsub method.

magicblock-aperture/src/state/subscriptions.rs (2)

288-308: LGTM: Core deduplication logic.

The add_subscriber method correctly implements the duplicate subscription fix. When a subscriber with the same encoder already exists (line 295), it reuses the existing subscriber.id (line 298) rather than generating a new one. This is the key mechanism that prevents subscription ID increment for duplicates.


387-387: LGTM: Necessary visibility change.

Making CleanUp pub(crate) is required for the new register_unsub method in dispatch.rs to access handle.cleanup.

magicblock-aperture/src/utils.rs (1)

52-59: LGTM: Improved debuggability.

Adding Debug derives to ProgramFilter and ProgramFilters enhances diagnostics without changing functionality. This aligns with the broader Debug trait additions across encoder types.

magicblock-aperture/src/server/websocket/dispatch.rs (1)

15-17: LGTM: Necessary import addition.

Adding SubscriptionHandle to the imports enables the new register_unsub method.

magicblock-aperture/src/encoder.rs (3)

1-1: LGTM: Required import for Debug trait bound.

The std::fmt::Debug import supports the new Debug trait bound on the Encoder trait.


25-25: LGTM: Debug trait bound improves observability.

Adding Debug as a required bound on the Encoder trait enables better diagnostics and debugging of subscription state, which is valuable for a WebSocket subscription system.


36-177: LGTM: Consistent Debug derives.

All encoder types correctly derive Debug to satisfy the trait bound. This enables debugging of subscription state across all encoder variants.

Copy link
Collaborator

@GabrielePicco GabrielePicco left a comment

Choose a reason for hiding this comment

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

LGTM!

@bmuddha bmuddha merged commit 2617901 into master Nov 4, 2025
6 of 7 checks passed
@bmuddha bmuddha deleted the bmuddha/fix/ws-sub-ids branch November 4, 2025 05:57
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.

3 participants