Skip to content

Subscription requests now take priority over notifications#7031

Merged
steveluscher merged 1 commit intoanza-xyz:masterfrom
steveluscher:receive-before-send-pubsub
Jul 18, 2025
Merged

Subscription requests now take priority over notifications#7031
steveluscher merged 1 commit intoanza-xyz:masterfrom
steveluscher:receive-before-send-pubsub

Conversation

@steveluscher
Copy link
Copy Markdown

@steveluscher steveluscher commented Jul 17, 2025

Problem

When there are a ton of notifications ready to send out over the RPC subscriptions server, they can end up starving the request processor. This means that time-sensitive requests like PING and new subscription requests don't get through.

Summary of Changes

Always process waiting requests before sending out ready notifications.

Test plan

Add a sleep to let a ton of notifications pile up in the broadcast sender.

  select! {
      result = &mut receive_future => match result {
          Ok(_) => {
              break;
          },
          Err(soketto::connection::Error::Closed) => return Ok(()),
          Err(err) => return Err(err.into()),
      },
      result = broadcast_receiver.recv() => {
          // In both possible error cases (closed or lagged) we disconnect the client.
          if let Some(json) = broadcast_handler.handle(result?)? {
              sender.send_text(&*json).await?;
          }
+         sleep(Duration::from_secs(6)).await;
      },
      _ = &mut tripwire => {
          warn!("disconnecting websocket client: shutting down");
          return Ok(())
      },
  }

Send pings from the client.

import ws from "ws";

const w = new ws("ws://127.0.0.1:8900");

w.on("message", function (message) {
  console.log("<<<", new TextDecoder().decode(message));
});

// When connected, send a PING
w.on("open", function open() {
  console.log("Connected to WebSocket server.");
  w.send('{"id":1,"jsonrpc":"2.0","method":"blockSubscribe","params":["all"]}');
  console.log("Subscribed to blocks");
  setInterval(() => {
    w.ping();
    console.log("Sent PING.");
  }, 5000);
});

// Optional: Listen for PONG responses from the server
w.on("pong", function () {
  console.log("PONG received.");
});

Observe that they always get processed before the next notification gets sent.

Fixes #7022

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.2%. Comparing base (55f4f8d) to head (eb1a15f).
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7031   +/-   ##
=======================================
  Coverage    83.2%    83.2%           
=======================================
  Files         853      853           
  Lines      374866   374866           
=======================================
+ Hits       312054   312083   +29     
+ Misses      62812    62783   -29     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

@lijunwangs lijunwangs left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for addressing this problem!

@steveluscher steveluscher merged commit 8adcd5a into anza-xyz:master Jul 18, 2025
41 checks passed
@steveluscher steveluscher deleted the receive-before-send-pubsub branch July 18, 2025 18:55
@lijunwangs
Copy link
Copy Markdown

@steveluscher @joncinque , The issue is important for Chainlink's customers, can we consider back port this to the next release branch?

@mergify
Copy link
Copy Markdown

mergify Bot commented Jul 22, 2025

Backports to the beta branch are to be avoided unless absolutely necessary for fixing bugs, security issues, and perf regressions. Changes intended for backport should be structured such that a minimum effective diff can be committed separately from any refactoring, plumbing, cleanup, etc that are not strictly necessary to achieve the goal. Any of the latter should go only into master and ride the normal stabilization schedule. Exceptions include CI/metrics changes, CLI improvements and documentation updates on a case by case basis.

mergify Bot pushed a commit that referenced this pull request Jul 22, 2025
(cherry picked from commit 8adcd5a)

# Conflicts:
#	CHANGELOG.md
steveluscher added a commit that referenced this pull request Jul 22, 2025
puhtaytow pushed a commit to puhtaytow/agave that referenced this pull request Jul 24, 2025
steveluscher added a commit that referenced this pull request Jul 30, 2025
mergify Bot added a commit that referenced this pull request Aug 26, 2025
…kport of #7031) (#7092)

* Subscription requests now take priority over notifications (#7031)

(cherry picked from commit 8adcd5a)

* Derp. This CHANGELOG already has an RPC section

---------

Co-authored-by: Steven Luscher <steveluscher@users.noreply.github.com>
Co-authored-by: Steven Luscher <steven.luscher@anza.xyz>
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.

websocket disconnection while actively using websocket

3 participants