Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LightClient: Unsubscribe from subscriptions #1408

Merged
merged 6 commits into from
Jan 30, 2024
Merged

Conversation

lexnv
Copy link
Collaborator

@lexnv lexnv commented Jan 29, 2024

Previous to the PR, subscribing to a method would never call unsubscribe from that method, even when the user dropped the subscription.

This PR ensures that the appropriate unsubscribe method is being called to avoid consuming unnecessary memory, overloading the smoldot instance by never-ending subscriptions, and unwanted side-effects as imposing a maximum limit of subscriptions after which they are rejected.

To achieve this:

  • the background <-> foreground protocol of subxt is extended to capture the unsubscribe method
  • the unsubscribe method is passed down from the RPC layer to the background task for tracking
  • the unsubscribe call is made when the user dropped the foreground subscription and smoldot produced a response for the said subscription

Running the subxt test suite for the light-client produced multiple warnings;

 WARN subxt-light-client-background: Subscription response id=1 chain=ChainId(0) is not tracked

With this PR, the smoldot acknowledges the unsubscribe method and stops the subscription:

DEBUG json-rpc-polkadot: json-rpc-request-queued; request={"jsonrpc":"2.0","id":"11", "method":"chain_unsubscribeNewHeads","params":["1"]}

Closes: #1402

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
@lexnv lexnv requested a review from a team as a code owner January 29, 2024 12:39
Copy link
Contributor

@tadeohepperle tadeohepperle left a comment

Choose a reason for hiding this comment

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

LGTM, nice work! 👍

}
} else {
// User dropped the receiver, unsubscribe from the method and remove internal tracking.
let Some(subscription_state) = self.subscriptions.remove(&(id, chain_id)) else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd have to check; is the only kind of error that can be returned from the channel above one that indicates it was closed by the receiver?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, taken from https://docs.rs/tokio/latest/tokio/sync/mpsc/struct.UnboundedSender.html#method.send:

If the receive half of the channel is closed, either due to close being called or the UnboundedReceiver having been dropped, this function returns an error. The error includes the value passed to send.

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

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

Looks good to me; good job spotting and fixing this!

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
@lexnv lexnv merged commit 7762da8 into master Jan 30, 2024
11 checks passed
@lexnv lexnv deleted the lexnv/smoldot-unsub branch January 30, 2024 11:47
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.

LightClient: Unsubscribe from subscriptions to avoid warnings
4 participants