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

Kgenjac/save sip reason state on callback #3210

Conversation

kenangenjac
Copy link
Contributor

@kenangenjac kenangenjac commented Apr 27, 2023

Hello,

We didn't have variables from the sip reason header on failure events such as nua_i_terminated or on nua_i_state event where sip is NULL, so this PR adds an option to save the state of these variables.

@lminiero
Copy link
Member

lminiero commented May 4, 2023

Not sure what specific problem this is trying to fix, but this will definitely cause memory leaks when nua_i_bye and nua_i_cancel are handled, since a new g_strdup would be done in their branches, replacing the value of the variable you allocate in your patch making the previous one memory that will never be freed. I'd say that rather than always doing it, as you're doing now, it would be better to add that info to the cases you're interested in.

@kenangenjac
Copy link
Contributor Author

Oh yes, I overlooked that, thank you for pointing that out.

The problem is that we need to read the reason header from final SIP responses, but SIP object is only present on nua_i_bye and nua_i_cancel events, whereas on nua_i_state and nua_i_terminated SIP object is NULL, so we cannot get the SIP reason header in those cases.

If it is a satisfiable solution, I think freeing the memory and setting these variables to NULL before a new g_strdup in nua_i_bye and nua_i_cancel branches would solve this problem.

@lminiero
Copy link
Member

lminiero commented May 4, 2023

I'm not sure that would be enough: your code doesn't do any g_free before the g_strdup either, meaning that a state followed by a terminated or viceversa would still cause the same leak. Maybe a simpler fix is to only set those variables where you do (so removing the allocation in cancel/bye) and ensuring there's a free before the strdup, even though I feel it could be semantically inaccurate, and there would be a ton of reallocations any time there's an incoming message (who all have a reason, even e.g., 200 OK).

Or maybe you should figure out why you're not getting the info in your case: I understand state/terminated don't have the sip object, but the call before probably does, are you getting to those states not via cancel/bye but somehow else? Maybe it's there that the reason string should also be added to? If you are going there via cancel/bye, I'd argue that you do have the reason: you need to intercept bye/cancel events too.

@kenangenjac
Copy link
Contributor Author

After some exploring, I found that we could use SIP reason received on nua_r_invite with the similar logic of allocation as in nua_i_bye. For reference, this diff shows what I mean: master...kenangenjac:janus-gateway:kgenjac/log-reasons

If this seems okay, I would discard the previous changes and update the PR with the new ones.

@lminiero
Copy link
Member

lminiero commented May 8, 2023

That still saves the response code even for provisional and successful responses too. Shouldn't that code only be in the error branch? e.g., error codes >= 400?

@kenangenjac
Copy link
Contributor Author

Yes, I updated the PR to save the reason only on error codes > 400, I also added it for 401 and 407 cases and synced with master.
Also, I moved the code to a method as it would now be repeated 4 times.
If the code is okay, I hope the method naming and method placement is okay, too.

Copy link
Member

@lminiero lminiero left a comment

Choose a reason for hiding this comment

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

Thanks! I just added a mini-note inline but after that I think it's good to merge. I'll take care of porting it to 0.x too then.

@@ -1504,6 +1504,7 @@ static void janus_sip_media_reset(janus_sip_session *session) {
gpointer janus_sip_sofia_thread(gpointer user_data);
/* Sofia callbacks */
void janus_sip_sofia_callback(nua_event_t event, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[]);
void janus_sip_save_reason(sip_t const *sip, janus_sip_session *session);
Copy link
Member

Choose a reason for hiding this comment

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

Good idea about the helper method! This should probably be static, but I see other functions aren't either, so that's something I can fix myself in a later commit.

return;

if (sip->sip_reason && sip->sip_reason->re_text) {
session->hangup_reason_header = g_strdup(sip->sip_reason->re_text);
Copy link
Member

Choose a reason for hiding this comment

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

I'd add a

g_free(session->hangup_reason_header);

before the g_strdup, so that we avoid memory leaks when a new reason replaces one received before. This was an issue we had already, apparently, so it may be a goot opportunity to fix this for good. The same should be done for the other two g_strdup before.

Copy link
Member

Choose a reason for hiding this comment

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

Ah actually I see they're freed in nua_i_state so it may not be needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I saw that it is freed in nua_i_state so I didn't add it in the method.

@lminiero
Copy link
Member

lminiero commented May 9, 2023

Considering my additional comment, I think it's safe to merge as it. We can then track if memory leaks occur anyway.
Thanks!

@lminiero lminiero merged commit abad2a6 into meetecho:master May 9, 2023
8 checks passed
@kenangenjac
Copy link
Contributor Author

Thank you for merging!

@lminiero
Copy link
Member

lminiero commented May 9, 2023

On second thought I think I'll add the g_free anyway, just to stay on the safe side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants