Skip to content

Commit

Permalink
nc sub ntf BUGFIX read lock for async sub tasks
Browse files Browse the repository at this point in the history
To avoid dead-lock in case the subscriptions
are accessed.

Fixes #1450
  • Loading branch information
michalvasko committed Aug 7, 2023
1 parent 2c500e3 commit 35b3282
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/netconf_subscribed_notifications.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,25 +198,21 @@ sub_ntf_cb_lock_clear(uint32_t sub_id)
* @brief Add a prepared and valid subscription into internal subscriptions.
*
* @param[in] sub Subscription to add.
* @param[out] sub_p Pointer to the stored subscription.
* @return 0 on success.
* @return -1 on error.
*/
static int
sub_ntf_add(const struct np2srv_sub_ntf *sub, struct np2srv_sub_ntf **sub_p)
sub_ntf_add(const struct np2srv_sub_ntf *sub)
{
void *mem;

*sub_p = NULL;

mem = realloc(info.subs, (info.count + 1) * sizeof *info.subs);
if (!mem) {
return -1;
}
info.subs = mem;

info.subs[info.count] = *sub;
*sub_p = &info.subs[info.count];
++info.count;

return 0;
Expand Down Expand Up @@ -362,9 +358,23 @@ np2srv_rpc_establish_sub_cb(sr_session_ctx_t *session, uint32_t UNUSED(sub_id),
INFO_WLOCK;

/* add into subscriptions, is not accessible before */
sub_ntf_add(&sub, &sub_p);
sub_ntf_add(&sub);

/* UNLOCK */
INFO_UNLOCK;

/* READ LOCK */
INFO_RLOCK;

/* find the subscription in case it moved when the lock was released */
sub_p = sub_ntf_find(nc_sub_id, 0, 0, 0);
if (!sub_p) {
EINT;
rc = SR_ERR_INTERNAL;
goto error_unlock;
}

/* start even asynchronous tasks that may access subscriptions and require lock to be held now */
/* start even asynchronous tasks that may access subscriptions and require read lock to be held now */
switch (type) {
case SUB_TYPE_SUB_NTF:
rc = sub_ntf_rpc_establish_sub_start_async(session, sub_p);
Expand Down

0 comments on commit 35b3282

Please sign in to comment.