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

use proper function for state destruction #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions library/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -1252,12 +1252,14 @@ struct dnet_net_state *dnet_state_create(struct dnet_node *n,
err_out_send_destroy:
pthread_mutex_lock(&n->state_lock);
err_out_unlock:
dnet_state_rb_remove_nolock(st);
list_del_init(&st->storage_state_entry);
dnet_state_remove_nolock(st);
pthread_mutex_unlock(&n->state_lock);

dnet_state_put(st);
pthread_mutex_destroy(&st->send_lock);
pthread_mutex_destroy(&st->trans_lock);
dnet_state_put(st);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's undetermined what refcnt will left after dnet_state_move_to_dht call. In case of error, dnet_state_move_to_dht can put (-EEXIST) or not to put (other errorcode) the net state. So you can't know how many puts you must do at this line.

I think the good practic is to do like shared_ptr: we lock the resource in the component where we use this resource (in dnet_state_create), and don't do refcounting for another components. These another components must do clear refcounting by themself. For example: dnet_state_create must have scoped single refcnt-lock, and dnet_state_move_to_dht should inc (if success) or don't inc (if fail) refcnt. The same way in dnet_state_create: if success, we return net state and don't decrement refcnt; if fail, decrement refcnt and return NULL. The caller of dnet_state_create must keep the result object, or throw it away via put.


goto err_out_exit;

err_out_dup_destroy:
dnet_sock_close(n, st->write_s);
n2_old_protocol_rcvbuf_destroy(st);
Expand Down