{"record":{"id":"27d59289b6ae10ac","repo":"ATH-MaaS/Pixelle-Video","slug":"task-id-task-id-self-max-polls","errorCode":null,"errorMessage":"可灵视频生成超时 (task_id={task_id}, 已等待 {self.max_polls * self.poll_interval}s)","messagePattern":"可灵视频生成超时 \\(task_id=(.+?), 已等待 (.+?)s\\)","errorType":"exception","errorClass":"TimeoutError","httpStatus":null,"severity":"error","filePath":"pixelle_video/services/api_services/video_kling.py","lineNumber":322,"sourceCode":"        for attempt in range(self.max_polls):\n            result = self._query_task(task_id, endpoint=endpoint)\n            status = result.get(\"task_status\", \"\")\n\n            if status == \"succeed\":\n                logger.info(f\"KlingVideoClient: 任务完成 task_id={task_id}\")\n                return result\n            elif status == \"failed\":\n                msg = result.get(\"task_status_msg\", \"未知错误\")\n                raise RuntimeError(f\"可灵视频生成失败: {msg} (task_id={task_id})\")\n            else:\n                # submitted / processing\n                logger.debug(\n                    f\"KlingVideoClient: 任务进行中 task_id={task_id}, \"\n                    f\"status={status}, attempt={attempt + 1}/{self.max_polls}\"\n                )\n                time.sleep(self.poll_interval)\n\n        raise TimeoutError(f\"可灵视频生成超时 (task_id={task_id}, 已等待 {self.max_polls * self.poll_interval}s)\")\n\n    # ─── 下载视频 ───\n\n    @staticmethod\n    def _download_video(video_url: str, save_path: str) -> None:\n        \"\"\"从 URL 下载视频到本地\"\"\"\n        save_dir = os.path.dirname(save_path)\n        if save_dir:\n            os.makedirs(save_dir, exist_ok=True)\n        # 下载也用 TLS 安全 Session\n        dl_session = _build_session(max_retries=2)\n        resp = dl_session.get(video_url, stream=True, timeout=600)\n        resp.raise_for_status()\n        with open(save_path, \"wb\") as f:\n            for chunk in resp.iter_content(chunk_size=8192):\n                if chunk:\n                    f.write(chunk)\n        logger.info(f\"KlingVideoClient: 视频已保存: {save_path}\")","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/ATH-MaaS/Pixelle-Video/blob/848b054e4fae40dabc62ec58e960b573e83793ac/pixelle_video/services/api_services/video_kling.py#L304-L340","documentation":"_poll_until_done polled max_polls times without the task reaching 'succeed' or 'failed'; the task is still submitted/processing. It raises TimeoutError reporting the total wait (max_polls * poll_interval) so the caller can decide to give up or re-check later.","triggerScenarios":"All attempts in range(self.max_polls) return task_status in ('submitted','processing') (video_kling.py:304-322). Typical with pro-mode or long-duration (15s) generations on kling-v3, or when max_polls*poll_interval is shorter than Kling's actual generation time under load.","commonSituations":"Peak-hour Kling queue delays pushing generation beyond the configured timeout, pro mode + long duration combos, overly tight max_polls/poll_interval settings in the client config, or a stuck task that never transitions (rare) — the timeout surfaces it.","solutions":["Increase max_polls or poll_interval on the client so total wait covers pro-mode/long-duration generation (e.g. 15-30 minutes).","Check the task via the Kling console or a manual GET /v1/videos/{endpoint}/{task_id} — the task may still complete after the timeout; query it once more and use the result if succeeded.","Use std mode or shorter duration when latency matters; pro + 15s is the slowest combination.","If the task is genuinely stuck (processing far beyond normal), resubmit a new task; the old task_id can be discarded."],"exampleFix":"# before\nclient = KlingVideoClient(max_polls=60, poll_interval=5)  # 最长等待 300s\n# after\nclient = KlingVideoClient(max_polls=120, poll_interval=10)  # 最长等待 1200s","handlingStrategy":"retry","validationCode":"# 估算所需等待时间，确保配置足够\nneeded = 30 * 60  # pro 模式长时长生成可达 30 分钟\nassert client.max_polls * client.poll_interval >= needed, \"轮询预算不足\"","typeGuard":null,"tryCatchPattern":"try:\n    url = client.generate_video(...)\nexcept TimeoutError as e:\n    logger.warning(f\"轮询超时，任务可能仍在进行: {e}\")\n    # 稍后手动复查一次任务状态，或加大 max_polls 后重试\n    time.sleep(300)\n    result = client._query_task(task_id, endpoint=endpoint)\n    if result.get(\"task_status\") == \"succeed\":\n        ...","preventionTips":["Set max_polls*poll_interval to 15-30+ minutes for pro mode / long durations","Prefer std mode or shorter durations when latency matters","Persist task_id so a timed-out job can be re-checked instead of resubmitted","Avoid resubmitting immediately — the original task may still complete"],"tags":["timeout","kling","polling","async-task"],"backgroundTag":"task-poll-timeout","analyzedSha":"848b054e4fae40dabc62ec58e960b573e83793ac","analyzedAt":"2026-08-30T03:24:41.468Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}