Fix deadlock on mountpoint destroy during RTSP reconnect #2700
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes a deadlock which occurs under some conditions when an RTSP mountpoint destroy is requested from the API, while the mountpoint is reconnecting to the RTSP server.
When a
destroy
message is handled, themountpoints_mutex
is locked (janus-gateway/plugins/janus_streaming.c
Line 3777 in c621bb5
janus-gateway/plugins/janus_streaming.c
Line 3798 in c621bb5
janus_streaming_mountpoint_destroy()
callback.janus_streaming_mountpoint_destroy()
interrupt the poll then join the relay thread to wait for it to exit (janus-gateway/plugins/janus_streaming.c
Line 1284 in c621bb5
But, if the mountpoint was trying to reconnect at the same moment (
janus-gateway/plugins/janus_streaming.c
Line 7643 in c621bb5
mountpoint->destroyed
not set to1
yet, then the relay thread will also try to lockmountpoints_mutex
(janus-gateway/plugins/janus_streaming.c
Line 6485 in c621bb5
join()
on that mountpoint thread.As the value of
mountpoint->destroyed
could change while waiting for the lock, the solution was to useg_mutex_trylock()
to be able to abort the lock attempt whendestroyed
is eventually set to1
.I've added a
janus_mutex_unlock()
macro, with no trailing;
to be able to use the macro inif
orwhile
statement (otherwise the compiler was not happy). I've also removed the semicolon in otherjanus_mutex_*
definitions for consistency.This is based on top of #2699. So I'll rebase this PR once the other one is merged. I've chosen to open two separated PRs as they are fixing two different issues.
Fixes: #2691