The Asterisk Direct Media Re-INVITE Trap: Why Dynamic Bridging Drops
calricon / field note
Learn why Asterisk PJSIP direct media re-INVITEs trigger SIP
The Asterisk Direct Media Re-INVITE Trap: Why Dynamic Bridging Drops Active Calls
Telephony administrators deploying Asterisk with the PJSIP channel driver frequently configure Direct Media (direct_media = yes) to optimize media routing. By allowing RTP audio streams to flow peer-to-peer between endpoints, Asterisk eliminates unnecessary media proxying, reducing server CPU utilization and network bandwidth. However, in production environments bridging external carrier trunks to internal extensions, Direct Media frequently triggers severe call instability. Calls connect and establish two-way audio normally, only to drop into complete silence or terminate abruptly after five to ten seconds. System log files flood with WARNING: res_pjsip_session.c: Method is not allowed by SIP glare rule or remote end responded 491 Request Pending, leaving network engineers grappling with intermittent call drops.
The root cause is a protocol-level SIP glare condition (RFC 3261 Section 14.1) combined with media flapping during dynamic call bridging. When two endpoints are bridged, Asterisk transmits simultaneous in-dialog re-INVITEs to both legs to swap media addresses. If an endpoint simultaneously initiates an in-dialog transaction—such as a session timer refresh, hold action, or early media update—both endpoints enter an INVITE_SENT state. The receiving endpoint rejects the overlapping transaction with SIP/2.0 491 Request Pending. In default PJSIP configurations, Asterisk lacks active glare mitigation, causing session state desynchronization. Furthermore, if dialplan features like call recording (MixMonitor) or DTMF transfer monitoring are enabled, Asterisk continuously attempts to re-INVITE into direct media and immediately re-INVITE back into local proxy media, creating a re-INVITE storm that triggers carrier timeouts and drops calls.
Problem Diagnosis & Root Cause Architecture
Understanding how Direct Media re-INVITE glare disrupts active calls requires analyzing the transaction state machines and media bridging mechanics within chan_pjsip.
- The Peer-to-Peer Re-INVITE Exchange: When a call answers, Asterisk initially establishes local media sockets on both channels to handle early media and ringback. Once the bridge becomes active, Asterisk sends a re-INVITE to Channel A containing Channel B's RTP IP and port, and a simultaneous re-INVITE to Channel B containing Channel A's RTP IP and port.
- RFC 3261 SIP Glare Collision: If Channel A (such as a carrier Session Border Controller) transmits an in-dialog session timer refresh at the precise moment Asterisk sends its direct media re-INVITE, both user agents receive an INVITE while awaiting a response to their own outbound INVITE. RFC 3261 mandates that the receiving user agent must return
491 Request Pendingto signal that the transaction cannot proceed. - Absence of Glare Mitigation: By default, PJSIP's
direct_media_glare_mitigationparameter is set tonone. When Asterisk receives the 491 response, it does not apply the randomized backoff timer dictated by RFC 3261. Instead, it either aborts the media renegotiation or retransmits immediately, colliding again with the carrier's retries and desynchronizing the SDP origin session version (o=line). - Media Flapping via Audio Hook Contention: Direct media requires that Asterisk never inspects or manipulates audio frames. If an audio hook—such as
MixMonitor(),ChanSpy(), or bridge DTMF detection options (t,T,w,Win theDial()command)—is attached, Asterisk cannot permit peer-to-peer RTP. Asterisk re-INVITEs the endpoints into direct media, immediately detects the active audio hook, and fires a second re-INVITE back to local media. This cyclic flapping overwhelms carrier SBC transaction queues. - NAT Address Leakage: When bridging an internal office phone behind NAT (e.g.,
10.0.1.50) to an external carrier trunk, standard Direct Media inserts the private RFC 1918 IP directly into the SDP Connection line (c=IN IP4 10.0.1.50) sent to the external carrier. Public transit routers discard the carrier's outgoing RTP packets, resulting in immediate one-way audio.
Direct Media re-INVITEs collide with concurrent in-dialog transactions, triggering RFC 3261 491 Request Pending glare. Without mitigation, media flapping and private IP leakage desynchronize session state and terminate calls.
Why Standard Implementations Fail
Increasing SIP Session Timer Intervals
Extending session expiration intervals (such as setting timers_sess_expires = 3600) reduces the frequency of scheduled session refreshes, but does not prevent glare caused by initial call answer, call transfers, user-initiated hold events, or carrier-side keepalive pings.
Switching direct_media_method from Re-INVITE to UPDATE
Configuring direct_media_method = update attempts to use the SIP UPDATE method (RFC 3311) instead of re-INVITE. However, many legacy carrier trunks and enterprise PSTN gateways do not support mid-dialog UPDATE requests, rejecting them with 405 Method Not Allowed and forcing Asterisk back into error recovery loops.
Disabling SIP Session Timers Entirely
Disabling session timers eliminates refresh glare, but violates Tier-1 telecommunications carrier interconnect agreements. Without session timers, dropped cellular connections or unannounced network partitions leave channels hanging open indefinitely, consuming billable carrier minutes and exhausting PBX resources.
Authenticated Step-by-Step Resolution Playbook
Step 1: Capture SIP Signaling and Identify Re-INVITE Glare Storms
Utilize sngrep or capture raw network traffic on the Asterisk host to verify the occurrence of 491 Request Pending errors and detect media flapping cycles.
# Capture SIP signaling transactions on port 5060
tshark -i eth0 -f "udp port 5060" -Y "sip.CSeq.method == INVITE || sip.Status-Code == 491" -T fields \
-e frame.time_relative -e ip.src -e ip.dst -e sip.CSeq -e sip.Status-Code \
| sed 20q
# Sample output showing rapid re-INVITE flapping and 491 collision:
# 2.105120 10.0.1.10 198.51.100.1 102 INVITE
# 2.106240 198.51.100.1 10.0.1.10 100 Trying
# 2.108450 198.51.100.1 10.0.1.10 102 INVITE
# 2.109110 10.0.1.10 198.51.100.1 491 Request Pending
# 2.110250 198.51.100.1 10.0.1.10 491 Request PendingStep 2: Configure Glare Mitigation and NAT Safety in pjsip.conf
Update the PJSIP endpoint definitions in pjsip.conf to enforce outgoing glare mitigation and disable direct media automatically when endpoints traverse NAT boundaries.
# /etc/asterisk/pjsip.conf
[carrier-trunk-endpoint]
type = endpoint
context = from-carrier
transport = transport-udp
disallow = all
allow = ulaw,alaw,g722
# Enable direct media with active glare mitigation
direct_media = yes
direct_media_method = reinvite
direct_media_glare_mitigation = outgoing
# Prevent private RFC 1918 IP addresses from leaking across NAT boundaries
disable_direct_media_on_nat = yes
# Ensure direct media is delayed until two-way media is confirmed
direct_media_hold_off_time = 2500
# Standard signaling timers
timers = yes
timers_min_se = 90
timers_sess_expires = 1800Step 3: Programmatically Control Direct Media in Dialplan Logic
Ensure that dialplan extensions utilizing call recording (MixMonitor) or DTMF feature monitoring explicitly disable Direct Media to prevent audio hook contention and re-INVITE flapping.
# /etc/asterisk/extensions.conf
[internal-calls]
exten => _1XXX,1,NoOp(Processing Call to Extension ${EXTEN})
same => n,Set(CHANNEL(hangup_handler_push)=hangup-cleanup,s,1)
# If call recording is requested, direct media MUST be disabled
same => n,GotoIf($["${RECORD_CALL}" = "1"]?record:bridge)
same => n(record),NoOp(Disabling Direct Media for MixMonitor Recording)
same => n,Set(PJSIP_ENDPOINT(carrier-trunk-endpoint,direct_media)=no)
same => n,MixMonitor(${UNIQUEID}.wav,b)
same => n,Dial(PJSIP/${EXTEN},30,tT)
same => n,Hangup()
same => n(bridge),NoOp(Proceeding with Glare-Mitigated Direct Media)
same => n,Dial(PJSIP/${EXTEN},30)
same => n,Hangup()Step 4: Configure Global SIP Transaction Timers
Tune PJSIP system timers in pjsip.conf to ensure that when a 491 Request Pending is encountered, retransmission backoff intervals allow the peer transaction to complete cleanly.
# /etc/asterisk/pjsip.conf
[system]
type = system
timer_t1 = 500
timer_b = 32000
[global]
type = global
default_outbound_endpoint = default-endpointReload the PJSIP subsystem to apply all configuration changes immediately:
asterisk -rx "pjsip reload"Step 5: Verify Seamless Bridge Handover and Zero Glare Drops
Simulate concurrent calls with active session refreshes and mid-call hold events using a SIP traffic generator, verifying that direct media bridges smoothly without 491 errors.
# Inspect channel direct media status during an active bridged call
asterisk -rx "pjsip show channel $(asterisk -rx 'core show channels' | grep PJSIP | sed -n 1p | awk '{print $1}')" | grep -E "Direct Media|Address"
# Expected output confirming stable direct media:
# Direct Media: Yes
# Glare Mitigation: outgoing
# Monitor Asterisk log stream for glare warnings during hold/resume cycles
asterisk -rx "core set verbose 3"
asterisk -rx "core show taskprocessors" | grep -E "pjsip|session"By enforcing direct_media_glare_mitigation = outgoing, activating disable_direct_media_on_nat = yes, and programmatically disabling direct media whenever audio hooks like MixMonitor are present, Asterisk completely eliminates re-INVITE storms and 491 glare failures, ensuring robust, peer-to-peer voice paths across enterprise voice networks.
Experiencing this issue in your production systems?
Calricon conducts senior-level VoIP, SIP trunking, Asterisk, and cloud infrastructure audits. We review your live configs, identify packet drops or contention bottlenecks, and provide exact remediation runbooks.