From 0808be1aff6389f5a0ed4cb62685ddc69b765844 Mon Sep 17 00:00:00 2001 From: Nanang Izzuddin Date: Tue, 13 Aug 2024 11:45:28 +0700 Subject: [PATCH] Assertion when sending SUBSCRIBE failed synchronously (#3985) * Handle possibility of reset buddy states after SUBSCRIBE send failure --- pjsip/src/pjsua-lib/pjsua_pres.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pjsip/src/pjsua-lib/pjsua_pres.c b/pjsip/src/pjsua-lib/pjsua_pres.c index f273c1bc5d..083782e062 100644 --- a/pjsip/src/pjsua-lib/pjsua_pres.c +++ b/pjsip/src/pjsua-lib/pjsua_pres.c @@ -1995,6 +1995,7 @@ static void subscribe_buddy(pjsua_buddy_id buddy_id, pjsip_tpselector tp_sel; pj_status_t status; const char *sub_str = presence? "presence": "dialog event"; + pjsip_dialog *dlg; /* Event subscription callback. */ pj_bzero(&pres_callback, sizeof(pres_callback)); @@ -2139,14 +2140,22 @@ static void subscribe_buddy(pjsua_buddy_id buddy_id, pjsua_process_msg_data(tdata, NULL); + /* Send request. Note that if the send operation fails sync-ly, e.g: + * gethostbyname() error, tsx callback may have been invoked which may + * get the subscription terminated and the buddy states reset, we need + * to be prepared for such scenario here. + */ + dlg = buddy->dlg; + if (presence) { status = pjsip_pres_send_request(buddy->sub, tdata); } else { status = pjsip_dlg_event_send_request(buddy->sub, tdata); } if (status != PJ_SUCCESS) { - pjsip_dlg_dec_lock(buddy->dlg); - pjsip_pres_terminate(buddy->sub, PJ_FALSE); + pjsip_dlg_dec_lock(dlg); + if (buddy->sub) + pjsip_pres_terminate(buddy->sub, PJ_FALSE); buddy->sub = NULL; pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE", status);