Skip to content

Commit

Permalink
Fix crashes when jack gives back error code -1, retry when connection…
Browse files Browse the repository at this point in the history
… failed (#2)

* implemented retries on failed connections

---------

Co-authored-by: Max Weidauer <[email protected]>
  • Loading branch information
themaxw and Max Weidauer authored Oct 23, 2024
1 parent 9af3166 commit a0185a2
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/jack_connection_manager/ConnectionManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import logging
import sys
import queue
from threading import Event
from threading import Event, Timer

log = logging.getLogger()

reconnect_wait_time = 2
reconnect_number_retries = 20

retry_timer = 1
n_retries = 15


def add_to_dict_of_sets(d: dict, key, value):
if key in d:
Expand All @@ -32,7 +35,7 @@ def __init__(self, config_path: Path, clientname="jack_connection_manager") -> N
self.build_connection_dict(config_path)

self.connect_to_jack_server(clientname)
self.queue = queue.Queue()
self.queue: queue.Queue[tuple[jack.Port, jack.Port, int]] = queue.Queue()
self.stop_event = Event()
self.c.set_port_registration_callback(self.set_connection_for_port, False)
self.c.activate()
Expand All @@ -47,7 +50,7 @@ def connect_to_jack_server(self, clientname, servername=None):
)
break
except jack.JackOpenError:
logging.warn("couldn't connect to jack server. retrying...")
logging.warning("couldn't connect to jack server. retrying...")
n_tries += 1
sleep(reconnect_wait_time)
else:
Expand Down Expand Up @@ -107,16 +110,20 @@ def set_connection_for_port(self, port: jack.Port, registered: bool = True):
if sink_port not in connections:
log.debug(f"connecting {port.name} -> {sink_port.name}")
if port.is_output:
self.queue.put((port, sink_port))
self.queue.put((port, sink_port, n_retries))
else:
self.queue.put((sink_port, port))
self.queue.put((sink_port, port, n_retries))

def connection_loop(self):
"""main loop that checks if new connections were put into the conection queue"""
while not self.stop_event.is_set():
try:
(out_port, in_port) = self.queue.get(timeout=1)
out_port, in_port, retries_remaining = self.queue.get(timeout=1)
except queue.Empty:
continue
except TypeError:
log.error("TypeError while unpacking ports from queue...")
continue

try:
self.c.connect(out_port, in_port)
Expand All @@ -125,13 +132,20 @@ def connection_loop(self):
if e.code == 17:
pass
else:

log.error(
f"Jack-Error {e.code} while setting connection: {e.message}"
)
pass

raise
if retries_remaining > 0:
log.warning(
f"Jack-Error {e.code} while setting connection: {e.message}, retrying..."
)
t = Timer(
retry_timer,
self.queue.put,
args=((out_port, in_port, retries_remaining - 1),),
)
t.start()
else:
log.error(
f"Jack-Error {e.code} while setting connection: {e.message}"
)

def print_missing_connections(self):
missing_ports = set()
Expand Down

0 comments on commit a0185a2

Please sign in to comment.