Repository navigation
Conversation
…hannels switch_core_media_bug_add() runs the INIT callback before it links the bug into session->bugs. For session recording, INIT starts recording_thread, which holds a session read lock until CLOSE signals it. If the session thread runs switch_core_media_bug_remove_all() in between, the bug is never closed, and the session waits forever in switch_core_session_write_lock() (zombie channel). After linking, close the bug right away if the channel is already down. Fixes signalwire#2067. Refs signalwire#1673, signalwire#2645.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Starting a session recording (
uuid_record,switch_ivr_record_session) at the moment the channel hangs up can leave a channel that is never destroyed. It stays inshow channels,hupallanduuid_killcannot remove it, it keeps its session slot and RTP ports, and shutdown has to wait for it. Reported in #1673, #2067 and #2645.Root cause
switch_core_media_bug_add()calls the bug'sSWITCH_ABC_TYPE_INITcallback before it links the bug intosession->bugs. For session recording, INIT startsrecording_thread, which takes a session read lock and waits on a condition variable untilSWITCH_ABC_TYPE_CLOSEsignals it.If the session thread runs
switch_core_media_bug_remove_all()(right afterswitch_core_session_run()returns inswitch_core_session_thread()) between INIT and the linking, the new bug is not on the list yet, so nothing ever closes it. The recording thread keeps its read lock, and the session thread blocks forever inswitch_core_session_write_lock().Thread dump of a stuck session on 1.11.3:
pthread_rwlock_wrlock<-switch_core_session_threadpthread_cond_wait<-recording_threadFix
After the bug is linked (and MEDIA_BUG_START is fired), if the channel is already down (
switch_channel_down_nosig), remove the bug right away. Removing it runs CLOSE, which signals and joins the recording thread.remove_all()only runs after the channel is down, and both linking and removal takebug_rwlock. So eitherremove_all()sees the linked bug and closes it, or this check sees the channel down and closes it; whichever comes second finds nothing to do.The function still returns
SWITCH_STATUS_SUCCESSand keeps*new_bug: callers such asswitch_ivr_record_session_event()keep using the bug pointer (session pool memory), and on failure they would destroy their user data again after CLOSE already did.Compared with #2218 (holding
bug_rwlockacross INIT), this does not run INIT and theexecute_on_record_start/api_on_record_starthooks under the bug lock, and it does not hold upremove_all()on the hangup path while INIT waits up to 2 seconds for the recording thread.Testing
Two FreeSWITCH 1.11.3 instances (these functions are identical on master). 200 iterations: originate to an answering peer, then
bgapi uuid_record <uuid> start <file> 0followed bybgapi uuid_kill <uuid>, with 0-9 ms between the two commands.DESTINATION_OUT_OF_ORDER.Normal recordings are unaffected (we have been running this patch since 2026-10-14).
Fixes #2067. Refs #1673, #2645.