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

fix(rpc module): close subscription task when a subscription is unsubscribed via the unsubscribe call #743

Merged
merged 16 commits into from
Apr 29, 2022

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Apr 28, 2022

The reason for this is that the stream passed to pipe_from_stream might not produce new items and that can lead to that calls that are unsubscribed is not terminated until an item for the underlying stream is produced.

In worst these tasks will not be terminated until the actual connection is closed which this fixes.

After this PR the subscriptions is terminated once the unsubscribe call is received.

@niklasad1 niklasad1 requested a review from a team as a code owner April 28, 2022 12:13
core/src/server/rpc_module.rs Outdated Show resolved Hide resolved
}
};

let sub_closed = match self.unsubscribe.take() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah I see, so this oneshot unsubscribe channel is removed here, which means we can only use pipe_from_try_stream once.

Is it possible to take a mutable ref to it instead to use below (which may need pinning)? Maybe then this method would still be reusable?

Copy link
Member Author

@niklasad1 niklasad1 Apr 29, 2022

Choose a reason for hiding this comment

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

it won't work because send takes a &self as well.

I changed it to tokio::sync::watch for ease of use.

The receiver is clonable and it's possible to check whether the sender is still alive
core/src/server/rpc_module.rs Outdated Show resolved Hide resolved
tests/tests/helpers.rs Outdated Show resolved Hide resolved
tests/tests/helpers.rs Outdated Show resolved Hide resolved
tests/tests/helpers.rs Outdated Show resolved Hide resolved
niklasad1 and others added 3 commits April 29, 2022 20:48
Co-authored-by: David <dvdplm@gmail.com>
Co-authored-by: David <dvdplm@gmail.com>
@@ -108,6 +100,48 @@ pub async fn websocket_server_with_subscription() -> (SocketAddr, WsServerHandle
})
.unwrap();

module
.register_subscription("subscribe_5_ints", "n", "unsubscribe_5_ints", |_, pending, _| {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now we can call pipe_from_stream more than once, should we also have a test to make sure that we can?

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 great to me!

@niklasad1 niklasad1 changed the title [rpc module]: close subscription task when a subscription is unsubscribed via the unsubscribe call fix(rpc module): close subscription task when a subscription is unsubscribed via the unsubscribe call Apr 29, 2022
@niklasad1 niklasad1 merged commit 8e945de into master Apr 29, 2022
@niklasad1 niklasad1 deleted the na-close-tasks-when-unsubscribed branch April 29, 2022 20:49
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