Skip to content

Commit

Permalink
Merge pull request #80 from holochain/0.3-make-close-on-drop-clone-safe
Browse files Browse the repository at this point in the history
0.3 make close on drop clone safe
  • Loading branch information
ThetaSinner authored May 28, 2024
2 parents c61df5d + bd284e8 commit 4a97eea
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 6 additions & 4 deletions src/app_websocket_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<EventEmitter>>,
abort_handle: Arc<AbortHandle>,
_abort_handle: Arc<AbortOnDropHandle>,
}

impl AppWebsocketInner {
Expand Down Expand Up @@ -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())),
})
}

Expand Down Expand Up @@ -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();
}
}
54 changes: 54 additions & 0 deletions tests/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 4a97eea

Please sign in to comment.