From 5c547a9cc6e072d764beec22e5ce9c1ff1eef983 Mon Sep 17 00:00:00 2001 From: ThetaSinner Date: Tue, 28 May 2024 16:05:38 +0100 Subject: [PATCH 1/2] Make close on drop clone safe --- src/app_websocket_inner.rs | 10 ++++--- tests/app.rs | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/app_websocket_inner.rs b/src/app_websocket_inner.rs index deba92f..af8b8d3 100644 --- a/src/app_websocket_inner.rs +++ b/src/app_websocket_inner.rs @@ -10,12 +10,14 @@ use std::{net::ToSocketAddrs, sync::Arc}; use tokio::sync::Mutex; use tokio::task::AbortHandle; +struct AbortOnDropHandle(AbortHandle); + /// The core functionality for an app websocket. #[derive(Clone)] pub(crate) struct AppWebsocketInner { tx: WebsocketSender, event_emitter: Arc>, - abort_handle: Arc, + _abort_handle: Arc, } impl AppWebsocketInner { @@ -51,7 +53,7 @@ impl AppWebsocketInner { Ok(Self { tx, event_emitter: mutex, - abort_handle: Arc::new(poll_handle.abort_handle()), + _abort_handle: Arc::new(AbortOnDropHandle(poll_handle.abort_handle())), }) } @@ -96,8 +98,8 @@ impl AppWebsocketInner { } } -impl Drop for AppWebsocketInner { +impl Drop for AbortOnDropHandle { fn drop(&mut self) { - self.abort_handle.abort(); + self.0.abort(); } } diff --git a/tests/app.rs b/tests/app.rs index bab7709..002ba83 100644 --- a/tests/app.rs +++ b/tests/app.rs @@ -186,3 +186,57 @@ async fn handle_signal() { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct TestString(pub String); + +#[tokio::test(flavor = "multi_thread")] +async fn close_on_drop_is_clone_safe() { + let conductor = SweetConductor::from_standard_config().await; + + // Connect admin client + let admin_port = conductor.get_arbitrary_admin_websocket_port().unwrap(); + let admin_ws = AdminWebsocket::connect((Ipv4Addr::LOCALHOST, admin_port)) + .await + .unwrap(); + + // Set up the test app + let app_id: InstalledAppId = "test-app".into(); + let agent_key = admin_ws.generate_agent_pub_key().await.unwrap(); + let app_info = admin_ws + .install_app(InstallAppPayload { + agent_key: agent_key.clone(), + installed_app_id: Some(app_id.clone()), + membrane_proofs: HashMap::new(), + network_seed: None, + source: AppBundleSource::Path(PathBuf::from("./fixture/test.happ")), + }) + .await + .unwrap(); + admin_ws.enable_app(app_id.clone()).await.unwrap(); + + // Connect app client + let app_ws_port = admin_ws + .attach_app_interface(0, AllowedOrigins::Any, None) + .await + .unwrap(); + let token_issued = admin_ws + .issue_app_auth_token(app_id.clone().into()) + .await + .unwrap(); + let signer = ClientAgentSigner::default().into(); + let app_ws = AppWebsocket::connect( + (Ipv4Addr::LOCALHOST, app_ws_port), + token_issued.token, + signer, + ) + .await + .unwrap(); + + { + let app_ws_2 = app_ws.clone(); + let app_info_2 = app_ws_2.app_info().await.unwrap().unwrap(); + assert_eq!(app_info.installed_app_id, app_info_2.installed_app_id); + } + + // Should still work after dropping the first app_ws + let app_info_3 = app_ws.app_info().await.unwrap().unwrap(); + assert_eq!(app_info.installed_app_id, app_info_3.installed_app_id); +} From bd284e8b4f179cb1bec6a2d7826bc255d35efcd0 Mon Sep 17 00:00:00 2001 From: ThetaSinner Date: Tue, 28 May 2024 16:08:05 +0100 Subject: [PATCH 2/2] Bump version --- CHANGELOG.md | 4 ++++ Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27f9d8f..cae5d97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Fixed ### Removed +## 2024-05-28: v0.5.0-rc.4 +### Fixed +- The app websocket was closing connections when cloned versions of itself were dropped. + ## 2024-05-28: v0.5.0-rc.3 ### Fixed - Dropping admin or app connections will now close the connection. diff --git a/Cargo.lock b/Cargo.lock index 6643157..ad33750 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2508,7 +2508,7 @@ dependencies = [ [[package]] name = "holochain_client" -version = "0.5.0-rc.3" +version = "0.5.0-rc.4" dependencies = [ "again", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 5d9483a..59bdde5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ name = "holochain_client" readme = "README.md" repository = "https://github.com/holochain/holochain-client-rust" resolver = "2" -version = "0.5.0-rc.3" +version = "0.5.0-rc.4" [workspace] members = ["fixture/zomes/foo"]