From fefd9b99cc277aad3a0959f37b88f42ab95ece15 Mon Sep 17 00:00:00 2001 From: Cocoon-Break <54054995+kuishou68@users.noreply.github.com> Date: Thu, 16 Apr 2026 11:31:35 +0800 Subject: [PATCH] fix: guard _parse_cooldown against malformed every_ repeat; close file handle with context manager (Closes #75) Signed-off-by: Cocoon-Break <54054995+kuishou68@users.noreply.github.com> --- reflect/scheduler.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/reflect/scheduler.py b/reflect/scheduler.py index 91429e7..28d701e 100644 --- a/reflect/scheduler.py +++ b/reflect/scheduler.py @@ -36,12 +36,15 @@ def _parse_cooldown(repeat): if repeat == 'weekly': return timedelta(days=6) if repeat == 'monthly': return timedelta(days=27) if repeat.startswith('every_'): - parts = repeat.split('_') - n = int(parts[1].rstrip('hdm')) - u = parts[1][-1] - if u == 'h': return timedelta(hours=n) - if u == 'm': return timedelta(minutes=n) - if u == 'd': return timedelta(days=n) + try: + parts = repeat.split('_') + n = int(parts[1].rstrip('hdm')) + u = parts[1][-1] + if u == 'h': return timedelta(hours=n) + if u == 'm': return timedelta(minutes=n) + if u == 'd': return timedelta(days=n) + except (ValueError, IndexError): + pass # fall through to warning below _logger.warning(f'Unknown repeat type: {repeat}, fallback to 20h cooldown') return timedelta(hours=20) @@ -78,7 +81,8 @@ def check(): if not f.endswith('.json'): continue tid = f[:-5] try: - task = json.loads(open(os.path.join(TASKS, f), encoding='utf-8').read()) + with open(os.path.join(TASKS, f), encoding='utf-8') as fp: + task = json.loads(fp.read()) except Exception as e: _logger.error(f'JSON parse error for {f}: {e}') continue