Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions juju/client/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,14 @@ def _exit_tasks():
for task in jasyncio.all_tasks():
task.cancel()

loop = jasyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(sig, _exit_tasks)
try:
loop = jasyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(sig, _exit_tasks)
except (ValueError, OSError, RuntimeError) as e:
# add_signal_handler doesn't work in a thread
if 'main thread' not in str(e):
raise

return (await websockets.connect(
url,
Expand Down Expand Up @@ -474,9 +479,14 @@ async def close(self, to_reconnect=False):
self.proxy.close()

# Remove signal handlers
loop = jasyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.remove_signal_handler(sig)
try:
loop = jasyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.remove_signal_handler(sig)
except (ValueError, OSError, RuntimeError) as e:
# add_signal_handler doesn't work in a thread
if 'main thread' not in str(e):
raise

async def _recv(self, request_id):
if not self.is_open:
Expand Down