Browse Source

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
doc-issue-template
MrS0m30n3 7 years ago
parent
commit
b2fcc5e546
1 changed files with 22 additions and 6 deletions
  1. 28
      youtube_dl_gui/downloaders.py

28
youtube_dl_gui/downloaders.py

@ -161,7 +161,8 @@ class YoutubeDLDownloader(object):
cmd = self._get_cmd(url, options) cmd = self._get_cmd(url, options)
self._create_process(cmd) 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(): while self._proc_is_alive():
stdout = self._proc.stdout.readline().rstrip() stdout = self._proc.stdout.readline().rstrip()
@ -185,6 +186,17 @@ class YoutubeDLDownloader(object):
else: else:
self._set_returncode(self.ERROR) 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() self._last_data_hook()
return self._return_code return self._return_code
@ -330,11 +342,15 @@ class YoutubeDLDownloader(object):
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
cmd = [item.encode(self._encoding, 'ignore') for item in cmd] 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): def extract_data(stdout):

Loading…
Cancel
Save