-
Notifications
You must be signed in to change notification settings - Fork 802
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 tdata leak when pjsip_inv_initial_answer() return error #3741
Conversation
pjsip/src/pjsip-ua/sip_inv.c
Outdated
if (status2 != PJ_SUCCESS) { | ||
pjsip_tx_data_dec_ref(tdata); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here the ref count is not decremented when not-success, but below after pjsip_timer_update_resp
it is decremented. Is it okay?
Also, please make sure that the pattern is consistent for every pjsip_dlg_modify_response
in the lib.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is that the tdata->ref_cnt
will be 2 when pjsip_inv_initial_answer()
returns.
e.g: the pjsip_tx_data_dec_ref()
after pjsip_timer_update_resp()
will not release the tdata
.
I think setting the tdata->last_answer
would be better to avoid the inconsistency and let inv/tsx decrement it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Setting
inv->last_answer
is fine only whenp_tdata
is set (so app will send it or dec ref it), otherwise it will still leak? pjsip_dlg_modify_response
does not mention about add ref count in its spec/doc, I think it's better to add it now.- Also from a quick check, in some other places, e.g: when
process_answer()
fails, it seems that the tdata ref count is not properly decremented? (need test all affected scenarios to make sure things are fine).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- At the moment we are missing a dec ref due to
pjsip_dlg_modify_response
. So, it will still leak even ifp_tdata
is not set. We can setinv->last_answer
only ifp_tdata
is set, however we need to call an additionalpjsip_tx_data_dec_ref
in the casep_tdata
is not set. - Right, will add the info to the doc
- You are right, we need to check the code after
pjsip_dlg_modify_response
. We need to an additionalpjsip_tx_data_dec_ref
e.g:
pjproject/pjsip/src/pjsip-ua/sip_inv.c
Line 2758 in 43dd94f
pjsip_tx_data_dec_ref(last_res);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am actually a bit puzzled as to why we need to add tdata ref in pjsip_dlg_modify_response()
. Is there a way for us to avoid adding the ref?
- Adding the reference in a function called 'modify' doesn't seem very intuitive, and rather unexpected. Even we did not know this before (and especially so because the doc never says anything about it).
- It also seems "ugly" if we have to decrement the ref twice on some failure cases.
From the code, it states: "Must add reference counter, since tsx_send_msg() will decrement it." And it will probably causes crash if we remove it, but perhaps there's another way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the API has been there since the beginning, see here, so I think modifying the behavior will cause a major backward compatibility issue.
We can always introduce an alternative API (without add ref) and replace all usages of the old one in the library, but perhaps too much hassle and we may still need to maintain the old one for quite some time for existing apps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if it introduces backward incompatibility, I agree that we shouldn't change it. Also, we don't need to create additional API since it's not a major issue.
I was just wondering why we need the additional tdata ref increment inside modify()
, and making the tdata ref to become two, while if we use create()
, it seems that the tdata ref is only one before we send it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a guess. Perhaps because modify_response()
is designated to work with a saved tdata
such as inv->last_answer
or tsx->last_tx
, so the saved tdata
will not be destroyed after being sent, e.g:
tdata = inv->last_answer;
modify_response(tdata);
send_response(tdata);
/* inv->last_answer will still be maintained here */
- Add doc - Add pjsip_tx_data_dec_ref() on failure case
pjsip/include/pjsip-ua/sip_inv.h
Outdated
* Notes: | ||
* - Upon failure, p_tdata might still get set. To avoid leak, | ||
* it needs to be sent or otherwise decrement the reference count using | ||
* #pjsip_tx_data_dec_ref(). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- while here, perhaps add all params docs too?
- adding a bit background about why p_tdata is set upon failure sounds nice? For example
When this function returning non-PJ_SUCCESS, it may be caused by an unacceptable INVITE request. In such cases the function will generate an appropriate answer in p_tdata, e.g: when session timer header Session-Expires is too low, the generated answer will include Min-SE header. If the generated answer is not sent, it must be destroyed, i.e: using pjsip_tx_data_dec_ref(), to avoid resource leak
.
When
pjsip_inv_initial_answer()
return error, it might set thetdata
to be sent.This will lead to leaked
tdata
if the caller doesn't manually destroy it (e.g: callingpjsip_tx_data_dec_ref()
or sending thetdata
usingpjsip_inv_send_msg()
).The issues related to this:
pjsip_dlg_modify_response()
inpjproject/pjsip/src/pjsip-ua/sip_inv.c
Line 2666 in 43dd94f
will add the reference to the returned
tdata
. This will lead to a leak.pjsip_inv_initial_answer()
is not checked and lead to a leak.pjproject/pjsip/src/pjsua-lib/pjsua_call.c
Line 5551 in 43dd94f