Skip to content

Commit

Permalink
b2b_entities: avoid deadlock for certain scenarios
Browse files Browse the repository at this point in the history
When a call was terminted with a BYE from the handler of the 200 OK
reply, the entities lock would remain locked, generating a deadlock.
This commit fixes this (and possible other) situations by generically
keeping a track when the lock is taken and unlock it before exiting.

Thanks to Vlad Patrascu for helping with debugging and solution for this

(cherry picked from commit 87661e1)
  • Loading branch information
razvancrainea committed Nov 16, 2022
1 parent ffa0a36 commit d848992
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions modules/b2b_entities/dlg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2658,6 +2658,7 @@ void b2b_tm_cback(struct cell *t, b2b_table htable, struct tmcb_params *ps)
int old_route_type;
bin_packet_t storage;
int b2b_ev = -1;
int lock_taken = 0;
struct b2b_context *ctx;
int b2b_cb_flags = 0;
unsigned int reqmask;
Expand Down Expand Up @@ -3394,26 +3395,28 @@ void b2b_tm_cback(struct cell *t, b2b_table htable, struct tmcb_params *ps)
b2b_route:

if (B2BE_SERIALIZE_STORAGE()) {
lock_get(&htable[hash_index].lock);
lock_taken = 1;
if (dlg_state == B2B_CONFIRMED && prev_state == B2B_MODIFIED) {
lock_get(&htable[hash_index].lock);

if (dlg->state != B2B_TERMINATED) {
b2b_ev = B2B_EVENT_UPDATE;
b2b_run_cb(dlg, hash_index, etype, B2BCB_TRIGGER_EVENT, b2b_ev,
&storage, serialize_backend);
} else
} else {
b2b_ev = -1;
}
} else if (b2b_ev == B2B_EVENT_CREATE) {
lock_get(&htable[hash_index].lock);

if (dlg->state != B2B_TERMINATED) {
b2b_run_cb(dlg, hash_index, etype, B2BCB_TRIGGER_EVENT, b2b_ev,
&storage, serialize_backend);

if (b2be_db_mode == WRITE_THROUGH)
b2be_db_insert(dlg, etype);
} else
} else {
b2b_ev = -1;
}
}
}

Expand All @@ -3437,8 +3440,9 @@ void b2b_tm_cback(struct cell *t, b2b_table htable, struct tmcb_params *ps)
if (b2be_db_update(dlg, etype) < 0)
LM_ERR("Failed to update in database\n");
lock_release(&htable[hash_index].lock);
} else if (b2b_ev != -1)
} else if (lock_taken) {
lock_release(&htable[hash_index].lock);
}

if (b2be_cluster) {
if (b2b_ev == B2B_EVENT_UPDATE)
Expand Down

0 comments on commit d848992

Please sign in to comment.