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 session memory leak during liveliness cleanup #806

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/api/liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ _z_liveliness_token_t _z_liveliness_token_null(void) {
return s;
}

void _z_liveliness_token_clear(_z_liveliness_token_t *token) {
if (!_z_liveliness_token_check(token)) {
return;
z_result_t _z_liveliness_token_clear(_z_liveliness_token_t *token) {
z_result_t ret = _Z_RES_OK;
if (_Z_RC_IS_NULL(&token->_zn)) {
return ret;
}
// TODO(sashacmc): implement proper check
if (token->_zn._val != NULL) {
_z_session_rc_t sess_rc = _z_session_weak_upgrade_if_open(&token->_zn);
if (!_Z_RC_IS_NULL(&sess_rc)) {
_z_undeclare_liveliness_token(token);
_z_session_rc_drop(&sess_rc);
}
_z_session_weak_drop(&token->_zn);
_z_session_rc_t sess_rc = _z_session_weak_upgrade_if_open(&token->_zn);
if (!_Z_RC_IS_NULL(&sess_rc)) {
ret = _z_undeclare_liveliness_token(token);
_z_session_rc_drop(&sess_rc);
}
_z_session_weak_drop(&token->_zn);
_z_keyexpr_clear(&token->_key);
*token = _z_liveliness_token_null();

return ret;
}

_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_liveliness_token_t, liveliness_token, _z_liveliness_token_check,
Expand All @@ -70,7 +71,7 @@ z_result_t z_liveliness_declare_token(const z_loaned_session_t *zs, z_owned_live
}

z_result_t z_liveliness_undeclare_token(z_moved_liveliness_token_t *token) {
return _z_undeclare_liveliness_token(&token->_this._val);
return _z_liveliness_token_clear(&token->_this._val);
}

/**************** Liveliness Subscriber ****************/
Expand Down
1 change: 0 additions & 1 deletion src/net/liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ z_result_t _z_undeclare_liveliness_token(_z_liveliness_token_t *token) {
ret = _z_send_n_msg(_Z_RC_IN_VAL(&token->_zn), &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK);
_z_n_msg_clear(&n_msg);

*token = _z_liveliness_token_null();
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion src/session/liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ z_result_t _z_liveliness_subscription_undeclare(_z_session_t *zn, uint32_t id, c

if (key != NULL) {
ret = _z_trigger_liveliness_subscriptions_undeclare(zn, *key, timestamp);
_z_keyexpr_clear(key);
_z_keyexpr_free(&key);
}

return ret;
Expand Down
3 changes: 3 additions & 0 deletions tests/z_api_liveliness_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ void test_liveliness_sub(bool multicast, bool history) {
z_sleep_s(1);
assert(context.token2_drop);

z_closure_sample_drop(z_closure_sample_move(&closure));
z_subscriber_drop(z_subscriber_move(&sub));

assert_ok(zp_stop_read_task(z_loan_mut(s1)));
assert_ok(zp_stop_read_task(z_loan_mut(s2)));
assert_ok(zp_stop_lease_task(z_loan_mut(s1)));
Expand Down
Loading