Skip to content

Commit

Permalink
Improve exit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dmunozv04 committed Jan 6, 2025
1 parent 284b7a1 commit edca2bf
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/iSponsorBlockTV/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,23 @@ async def skip(self, time_to, position, uuids):

async def cancel(self):
self.cancelled = True
try:
await self.lounge_controller.disconnect()
if self.task:
self.task.cancel()
except Exception:
pass
if self.lounge_controller.subscribe_task_watchdog:
self.lounge_controller.subscribe_task_watchdog.cancel()
if self.lounge_controller.subscribe_task:
self.lounge_controller.subscribe_task.cancel()
await asyncio.gather(
self.task,
self.lounge_controller.subscribe_task_watchdog,
self.lounge_controller.subscribe_task,
return_exceptions=True
)


async def finish(devices, web_session, tcp_connector):
for device in devices:
await device.cancel()
await asyncio.gather(*(device.cancel() for device in devices), return_exceptions=True)
await web_session.close()
await tcp_connector.close()

Expand Down Expand Up @@ -169,9 +177,10 @@ def main(config, debug):
loop.run_forever()
except KeyboardInterrupt:
print("Cancelling tasks and exiting...")
loop.run_until_complete(finish(devices, web_session, tcp_connector))
for task in tasks:
task.cancel()
loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
loop.run_until_complete(finish(devices, web_session, tcp_connector))
finally:
loop.close()
print("Exited")

0 comments on commit edca2bf

Please sign in to comment.