Skip to content

Commit

Permalink
Merge pull request #1010 from tonlabs/SDK-4757-memory-leak
Browse files Browse the repository at this point in the history
FIX: WebsocketLink stopped the handler loop on unsubscribe.
  • Loading branch information
melsomino authored Jun 24, 2023
2 parents 91c0f09 + 5028b12 commit 7ea1cf3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
43 changes: 33 additions & 10 deletions ton_client/src/client/tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::client::ResultOfGetApiReference;
use crate::crypto::default_mnemonic_word_count;
use crate::json_interface::modules::ClientModule;
use crate::json_interface::runtime::Runtime;
use crate::net::{subscribe_collection, unsubscribe, ParamsOfSubscribeCollection};
use crate::tests::TestClient;
use crate::{ClientConfig, create_context, destroy_context};
use crate::{create_context, destroy_context, ClientConfig};
use api_info::ApiModule;
use serde_json::Value;
use std::time::Duration;

#[test]
fn test_config_fields() {
Expand Down Expand Up @@ -79,9 +82,9 @@ fn test_invalid_params_error_secret_stripped() {
assert!(!error.message.contains(secret));
}

#[test]
fn test_memory_leak() {
for _ in 0..100 {
#[tokio::test]
async fn test_memory_leak() {
for _ in 0..1 {
let ctx = create_context(
r#"
{
Expand All @@ -90,12 +93,32 @@ fn test_memory_leak() {
}"#.to_string());
let context = serde_json::from_str::<Value>(&ctx).unwrap()["result"].as_i64().unwrap() as u32;
{
// let context = Runtime::required_context(context).unwrap();
// query_collection(context, ParamsOfQueryCollection {
// collection: "blocks".to_string(),
// result: "id".to_string(),
// ..Default::default()
// }).await.unwrap();
let context = Runtime::required_context(context).unwrap();
let subscription = subscribe_collection(
context.clone(),
ParamsOfSubscribeCollection {
collection: "blocks".to_string(),
result: "id".to_string(),
filter: None,
},
|_| async {},
)
.await
.unwrap();
unsubscribe(context.clone(), subscription).await.unwrap();
tokio::time::sleep(Duration::from_millis(1000)).await;
let subscription = subscribe_collection(
context.clone(),
ParamsOfSubscribeCollection {
collection: "blocks".to_string(),
result: "id".to_string(),
filter: None,
},
|_| async {},
)
.await
.unwrap();
unsubscribe(context.clone(), subscription).await.unwrap();
}
destroy_context(context);
}
Expand Down
4 changes: 2 additions & 2 deletions ton_client/src/net/server_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl NetworkState {
pub(crate) struct ServerLink {
config: NetworkConfig,
pub(crate) client_env: Arc<ClientEnv>,
websocket_link: WebsocketLink,
websocket_link: Arc<WebsocketLink>,
state: Arc<NetworkState>,
}

Expand Down Expand Up @@ -417,7 +417,7 @@ impl ServerLink {
config: config.clone(),
client_env: client_env.clone(),
state: state.clone(),
websocket_link: WebsocketLink::new(client_env, state, config),
websocket_link: Arc::new(WebsocketLink::new(client_env, state, config)),
})
}

Expand Down
1 change: 0 additions & 1 deletion ton_client/src/net/websocket_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ impl HandlerAction {

//================================================================================== WebsocketLink

#[derive(Clone)]
pub(crate) struct WebsocketLink {
client_env: Arc<ClientEnv>,
handler_action_sender: Sender<HandlerAction>,
Expand Down

0 comments on commit 7ea1cf3

Please sign in to comment.