From 89ada4406af61c05f4259b10e2b1d26db92653f7 Mon Sep 17 00:00:00 2001 From: insaneracist Date: Tue, 3 Nov 2020 14:00:30 -0800 Subject: [PATCH] [heartbeat] replace daemon=True with join, send exception traceback to stderr --- youtube_dlc/heartbeat.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/youtube_dlc/heartbeat.py b/youtube_dlc/heartbeat.py index 26d62b769..c67566963 100644 --- a/youtube_dlc/heartbeat.py +++ b/youtube_dlc/heartbeat.py @@ -1,12 +1,12 @@ # coding: utf-8 from __future__ import unicode_literals -import time import threading import traceback from .utils import ( compat_str, + encode_compat_str, sanitized_Request ) @@ -27,11 +27,11 @@ class Heartbeat(object): self.interval = params.get('interval', 30) self.cancelled = False - self.thread = threading.Thread(target=self.__heartbeat, daemon=True) + self.parent_thread = threading.current_thread() + self.thread = threading.Thread(target=self.__heartbeat) def start(self): - if self.ydl.params.get('verbose'): - self.ydl.to_screen('[heartbeat] Heartbeat every %s seconds' % self.interval) + self.ydl.to_screen('[heartbeat] Heartbeat every %s seconds' % self.interval) self.thread.start() def cancel(self): @@ -49,7 +49,9 @@ class Heartbeat(object): self.ydl.to_screen('[heartbeat]') self.ydl.urlopen(self.request) except Exception: + self.ydl.report_warning("[heartbeat] Heartbeat failed") if self.ydl.params.get('verbose'): - traceback.print_exc() - self.ydl.to_screen("[heartbeat] Heartbeat failed") - time.sleep(self.interval) + self.ydl.to_stderr(encode_compat_str(traceback.format_exc())) + self.parent_thread.join(self.interval) + if not self.parent_thread.is_alive(): + break