Skip to content

Instantly share code, notes, and snippets.

@rjohnsondev
Last active April 30, 2022 01:38
Show Gist options
  • Save rjohnsondev/babfd730d0076eb7c3404cd8c0c85d3d to your computer and use it in GitHub Desktop.
Save rjohnsondev/babfd730d0076eb7c3404cd8c0c85d3d to your computer and use it in GitHub Desktop.
demonstration of Asterisk crash due to ARI Bridge race condition
from multiprocessing.pool import Pool
from requests.auth import HTTPBasicAuth
from websocket import create_connection
import functools
import json
import random
import requests
import time
import urllib.parse
import threading
bridges = []
channels = []
auth = HTTPBasicAuth("admin", "peekaboo")
def create_bridges():
for x in range(4):
params = urllib.parse.urlencode(
{
"type": "mixing,dtmf_events,proxy_media",
"name": "bridge_{}".format(x),
}
)
url = "http://localhost:8088/ari/bridges?{}".format(params)
resp = requests.post(url, auth=auth)
bridges.append(resp.json()["id"])
print("Bridges created", bridges)
def switch_bridge(id, x):
bridge = random.choice(bridges)
params = urllib.parse.urlencode(
{
"absorbDTMF": 1,
"mute": 1,
"channel": id,
}
)
url = "http://localhost:8088/ari/bridges/{}/addChannel?{}".format(bridge, params)
requests.post(url, auth=auth)
# just to show something is happening...
print(".", end="")
if x % 80 == 0:
print()
return x
def start_race():
with Pool(processes=10) as pool:
pool.map(functools.partial(switch_bridge, ",".join(channels)), range(20000))
print("usually crashes before we get here...")
def listen_ws(ws):
while True:
result = json.loads(ws.recv())
if result["type"] == "StasisStart" and result["args"][0] == "3":
print("4 calls connected, starting bridge switching in 3 seconds...")
time.sleep(3)
race_thread = threading.Thread(target=start_race)
race_thread.start()
def start():
ws = create_connection(
"ws://localhost:8088/ari/events?api_key=admin:peekaboo&app=hello"
)
listen_thread = threading.Thread(target=listen_ws, args=(ws,))
listen_thread.start()
create_bridges()
for x in range(4):
params = urllib.parse.urlencode(
{
"endpoint": "PJSIP/localout/sip:127.0.0.1:5060",
"app": "hello",
"appArgs": "{}".format(x),
}
)
url = "http://localhost:8088/ari/channels?{}".format(params)
resp = requests.post(url, auth=HTTPBasicAuth("admin", "peekaboo"))
resp_json = resp.json()
channels.append(resp_json["id"])
print("calls originated")
if __name__ == "__main__":
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment