-
Notifications
You must be signed in to change notification settings - Fork 173
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
WsClient::on_disconnect is not cancel-safe/footgun #996
Comments
EDIT: The documentation is wrong the method is not cancel-safe because after So for now you need use futures::future::select and handle the future which isn't resolved and poll it again to workaround that when using If you do this instead it should work on the latest release: #[tokio::main]
async fn main() -> anyhow::Result<()> {
let filter = tracing_subscriber::EnvFilter::try_from_default_env()?
.add_directive("jsonrpsee[method_call{name = \"say_hello\"}]=trace".parse()?);
tracing_subscriber::FmtSubscriber::builder().with_env_filter(filter).finish().try_init()?;
let interval = interval(Duration::from_millis(200));
let stream = IntervalStream::new(interval).map(move |_| 1);
let addr = run_server().await?;
let url = format!("ws://{}", addr);
let client = WsClientBuilder::default().build(&url).await?;
let disconnect = client.on_disconnect();
tokio::pin!(disconnect, stream);
let mut next_event = stream.next();
loop {
match futures::future::select(disconnect, next_event).await {
Either::Left((_, _)) => {
// drop the stream here because the client is disconnected.
break;
}
Either::Right((item, disconnect_fut)) => {
println!("stream item: {:?}", item);
next_event = stream.next();
disconnect = disconnect_fut;
}
}
}
Ok(())
}
async fn run_server() -> anyhow::Result<SocketAddr> {
let server = ServerBuilder::default().build("127.0.0.1:0").await?;
let mut module = RpcModule::new(());
module.register_method("say_hello", |_, _| Ok("lo"))?;
let addr = server.local_addr()?;
let handle = server.start(module)?;
// In this example we don't care about doing shutdown so let's it run forever.
// You may use the `ServerHandle` to shut it down or manage it yourself.
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(20)).await;
println!("server stopped");
handle.stop().unwrap();
});
Ok(addr)
} |
Closed by #999 |
jsonrpsee/core/src/client/async_client/mod.rs
Lines 260 to 272 in a330dae
on_disconnect
can only be called once which makes this code incorrect https://github.com/AcalaNetwork/subway/blob/1026a5110837f96564732922ada96c9800dd495d/src/client/mod.rs#L114We need to either improve the docs & API design to ensure the repeated call either works as expected or fail with some error indicate repeated call is not supported.
The text was updated successfully, but these errors were encountered: