Skip to content

Commit

Permalink
[jsinterp] Implement timeout
Browse files Browse the repository at this point in the history
Workaround for #4716
  • Loading branch information
pukkandan committed Aug 22, 2022
1 parent 822d66e commit 992dc6b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
10 changes: 7 additions & 3 deletions yt_dlp/extractor/openload.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def get(self, url, html=None, video_id=None, note=None, note2='Executing JS on w

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"""
if 'phantom.exit();' not in jscode:
jscode += ';\nphantom.exit();'
Expand All @@ -231,8 +231,12 @@ def execute(self, jscode, video_id=None, note='Executing JS'):

cmd = [self.exe, '--ssl-protocol=any', self._TMP_FILES['script'].name]
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:
raise ExtractorError(f'Executing JS failed:\n{stderr.strip()}')
raise ExtractorError(f'{note} failed:\n{stderr.strip()}')

return stdout
2 changes: 1 addition & 1 deletion yt_dlp/extractor/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2630,7 +2630,7 @@ def _decrypt_nsig(self, s, video_id, player_url):
ret = extract_nsig(jsi, func_code)(s)
except JSInterpreter.Exception as e:
try:
jsi = PhantomJSwrapper(self)
jsi = PhantomJSwrapper(self, timeout=5000)
except ExtractorError:
raise e
self.report_warning(
Expand Down
4 changes: 2 additions & 2 deletions yt_dlp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,9 +860,9 @@ def kill(self, *, timeout=0):
self.wait(timeout=timeout)

@classmethod
def run(cls, *args, **kwargs):
def run(cls, *args, timeout=None, **kwargs):
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


Expand Down

0 comments on commit 992dc6b

Please sign in to comment.