From b2fcc5e54661d9c7525d273c036ad5dd4cedf47c Mon Sep 17 00:00:00 2001 From: MrS0m30n3 Date: Sun, 10 Dec 2017 00:01:04 +0200 Subject: [PATCH] downloaders.py: Handle errors during process initialization Handle errors during process initialization (raised exceptions & non-zero exit codes), log them and exit normally instead of crashing unexpectedly Related to: #244 --- youtube_dl_gui/downloaders.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/youtube_dl_gui/downloaders.py b/youtube_dl_gui/downloaders.py index 638e88e..f030854 100644 --- a/youtube_dl_gui/downloaders.py +++ b/youtube_dl_gui/downloaders.py @@ -161,7 +161,8 @@ class YoutubeDLDownloader(object): cmd = self._get_cmd(url, options) self._create_process(cmd) - self._stderr_reader.attach_filedescriptor(self._proc.stderr) + if self._proc is not None: + self._stderr_reader.attach_filedescriptor(self._proc.stderr) while self._proc_is_alive(): stdout = self._proc.stdout.readline().rstrip() @@ -185,6 +186,17 @@ class YoutubeDLDownloader(object): else: self._set_returncode(self.ERROR) + # Set return code to ERROR if we could not start the download process + # or the childs return code is not equal to zero + # NOTE: In Linux if the called script is just empty Python exits + # normally (ret=0), so we cant detect this or similar cases + # using the code below + if self._proc is None or self._proc.returncode: + self._return_code = self.ERROR + + if self._proc is not None and self._proc.returncode: + self._log('Child process exited with non-zero code: {}'.format(self._proc.returncode)) + self._last_data_hook() return self._return_code @@ -330,11 +342,15 @@ class YoutubeDLDownloader(object): if sys.version_info < (3, 0): cmd = [item.encode(self._encoding, 'ignore') for item in cmd] - self._proc = subprocess.Popen(cmd, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - preexec_fn=preexec, - startupinfo=info) + try: + self._proc = subprocess.Popen(cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + preexec_fn=preexec, + startupinfo=info) + except (ValueError, OSError) as error: + self._log('Failed to start process: {}'.format(cmd)) + self._log(unicode(error)) def extract_data(stdout):