[jsinterp] Implement timeout

Workaround for #4716
pull/4739/head
pukkandan 2 years ago
parent 822d66e591
commit 992dc6b486
No known key found for this signature in database
GPG Key ID: 7EEE9E1E817D0A39

@ -219,7 +219,7 @@ class PhantomJSwrapper:
return html, stdout return html, stdout
def execute(self, jscode, video_id=None, note='Executing JS'): def execute(self, jscode, video_id=None, *, note='Executing JS'):
"""Execute JS and return stdout""" """Execute JS and return stdout"""
if 'phantom.exit();' not in jscode: if 'phantom.exit();' not in jscode:
jscode += ';\nphantom.exit();' jscode += ';\nphantom.exit();'
@ -231,8 +231,12 @@ class PhantomJSwrapper:
cmd = [self.exe, '--ssl-protocol=any', self._TMP_FILES['script'].name] cmd = [self.exe, '--ssl-protocol=any', self._TMP_FILES['script'].name]
self.extractor.write_debug(f'PhantomJS command line: {shell_quote(cmd)}') self.extractor.write_debug(f'PhantomJS command line: {shell_quote(cmd)}')
stdout, stderr, returncode = Popen.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try:
stdout, stderr, returncode = Popen.run(cmd, timeout=self.options['timeout'] / 1000,
text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except Exception as e:
raise ExtractorError(f'{note} failed: Unable to run PhantomJS binary', cause=e)
if returncode: if returncode:
raise ExtractorError(f'Executing JS failed:\n{stderr.strip()}') raise ExtractorError(f'{note} failed:\n{stderr.strip()}')
return stdout return stdout

@ -2630,7 +2630,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
ret = extract_nsig(jsi, func_code)(s) ret = extract_nsig(jsi, func_code)(s)
except JSInterpreter.Exception as e: except JSInterpreter.Exception as e:
try: try:
jsi = PhantomJSwrapper(self) jsi = PhantomJSwrapper(self, timeout=5000)
except ExtractorError: except ExtractorError:
raise e raise e
self.report_warning( self.report_warning(

@ -860,9 +860,9 @@ class Popen(subprocess.Popen):
self.wait(timeout=timeout) self.wait(timeout=timeout)
@classmethod @classmethod
def run(cls, *args, **kwargs): def run(cls, *args, timeout=None, **kwargs):
with cls(*args, **kwargs) as proc: with cls(*args, **kwargs) as proc:
stdout, stderr = proc.communicate_or_kill() stdout, stderr = proc.communicate_or_kill(timeout=timeout)
return stdout or '', stderr or '', proc.returncode return stdout or '', stderr or '', proc.returncode

Loading…
Cancel
Save