Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(cron): 修复固定间隔任务因 store 并发替换导致的重复执行
weitongtonge0ba56808967
diff --git a/nanobot/cron/service.py b/nanobot/cron/service.pyindex 267613012..165ce54d7 100644--- a/nanobot/cron/service.py+++ b/nanobot/cron/service.py@@ -80,6 +80,7 @@ class CronService:self._store: CronStore | None = Noneself._timer_task: asyncio.Task | None = Noneself._running = False+ self._timer_active = Falseself.max_sleep_ms = max_sleep_msdef _load_jobs(self) -> tuple[list[CronJob], int]:@@ -171,7 +172,11 @@ class CronService:def _load_store(self) -> CronStore:"""Load jobs from disk. Reloads automatically if file was modified externally.- Reload every time because it needs to merge operations on the jobs object from other instances.+ - During _on_timer execution, return the existing store to prevent concurrent+ _load_store calls (e.g. from list_jobs polling) from replacing it mid-execution."""+ if self._timer_active and self._store:+ return self._storejobs, version = self._load_jobs()self._store = CronStore(version=version, jobs=jobs)self._merge_action()@@ -290,18 +295,23 @@ class CronService:"""Handle timer tick - run due jobs."""self._load_store()if not self._store:+ self._arm_timer()return- now = _now_ms()- due_jobs = [- j for j in self._store.jobs- if j.enabled and j.state.next_run_at_ms and now >= j.state.next_run_at_ms- ]+ self._timer_active = True+ try:+ now = _now_ms()+ due_jobs = [+ j for j in self._store.jobs+ if j.enabled and j.state.next_run_at_ms and now >= j.state.next_run_at_ms+ ]- for job in due_jobs:- await self._execute_job(job)+ for job in due_jobs:+ await self._execute_job(job)- self._save_store()+ self._save_store()+ finally:+ self._timer_active = Falseself._arm_timer()async def _execute_job(self, job: CronJob) -> None: