Skip to content

Commit

Permalink
Fix a potential error which may case server halt.
Browse files Browse the repository at this point in the history
Some exceptions are ignored. In some critical situation, race condition
may cause server halt.
  • Loading branch information
d0u9 committed Apr 11, 2018
1 parent c7879bc commit 96062d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
12 changes: 8 additions & 4 deletions youtube_dl_webui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ def event_batch(cls, svr, event, data, arg):
class WorkMsgDispatcher(object):

_task_mgr = None
logger = logging.getLogger('ydl_webui')

@classmethod
def init(cls, task_mgr):
Expand All @@ -212,10 +213,13 @@ def event_fatal(cls, svr, event, data, arg):
@classmethod
def event_progress(cls, svr, event, data, arg):
tid, data = data['tid'], data['data']
cls._task_mgr.progress_update(tid, data)

if data['status'] == 'finished':
cls._task_mgr.finish_task(tid)
try:
cls._task_mgr.progress_update(tid, data)
except TaskInexistenceError:
cls.logger.error('Cannot update progress, task does not exist')
else:
if data['status'] == 'finished':
cls._task_mgr.finish_task(tid)


def load_conf_from_file(cmd_args):
Expand Down
6 changes: 6 additions & 0 deletions youtube_dl_webui/msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ def __init__(self):
def new_cli(self, cli_name=None):
uuid = None
if cli_name is not None:
# For named client, we create unique queue for communicating with server
uuid = cli_name
cli = CliMsg(cli_name, Queue(), self._svrQ)
else:
# Anonymous client is a client who needn't to talk to the server.
uuid = new_uuid()
cli = CliMsg(uuid, None, self._svrQ)

Expand All @@ -61,6 +63,10 @@ def new_cli(self, cli_name=None):
return cli

def reg_event(self, event, cb_func, arg=None):
# callback functions should have the signature of callback(svr, event, data, arg)
#
# svr is an instance of SrvMsg class, so the callback can directly send
# mssages via svr to its corresponding client.
self._evnt_cb_dict[event] = (cb_func, arg)

def run(self):
Expand Down
9 changes: 7 additions & 2 deletions youtube_dl_webui/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def start_task(self, tid, ignore_state=False):
if status['state'] == state_index['finished']:
raise TaskError('Task is finished')

task = Task(tid, self._msg_cli, ydl_opts=ydl_opts, info=info,
task = Task(tid, self._msg_cli, ydl_opts=ydl_opts, info=info,
status=status, log_size=self._conf['general']['log_size'])
self._tasks_dict[tid] = task

Expand Down Expand Up @@ -279,5 +279,10 @@ def launch_unfinished(self):
tid_list = self._db.launch_unfinished()

for t in tid_list:
self.start_task(t)
try:
self.start_task(t)
except TaskError as e:
self.logger.warn("Task %s is in downloading or finished state", tid)
except TaskInexistenceError:
self.logger.error('Task does not exist')

0 comments on commit 96062d9

Please sign in to comment.