{"record":{"id":"1da6ad3748238b13","repo":"binary-husky/gpt_academic","slug":"error-1da6ad","errorCode":null,"errorMessage":"程序终止。","messagePattern":"程序终止。","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"request_llms/bridge_qwen.py","lineNumber":23,"sourceCode":"\nmodel_name = 'Qwen'\n\ndef predict_no_ui_long_connection(inputs:str, llm_kwargs:dict, history:list=[], sys_prompt:str=\"\",\n                                  observe_window:list=[], console_silence:bool=False):\n    \"\"\"\n        ⭐多线程方法\n        函数的说明请见 request_llms/bridge_all.py\n    \"\"\"\n    watch_dog_patience = 5\n    response = \"\"\n\n    from .com_qwenapi import QwenRequestInstance\n    sri = QwenRequestInstance()\n    for response in sri.generate(inputs, llm_kwargs, history, sys_prompt):\n        if len(observe_window) >= 1:\n            observe_window[0] = response\n        if len(observe_window) >= 2:\n            if (time.time()-observe_window[1]) > watch_dog_patience: raise RuntimeError(\"程序终止。\")\n    return response\n\ndef predict(inputs, llm_kwargs, plugin_kwargs, chatbot, history=[], system_prompt='', stream = True, additional_fn=None):\n    \"\"\"\n        ⭐单线程方法\n        函数的说明请见 request_llms/bridge_all.py\n    \"\"\"\n    chatbot.append((inputs, \"\"))\n    yield from update_ui(chatbot=chatbot, history=history)\n\n    # 尝试导入依赖，如果缺少依赖，则给出安装建议\n    try:\n        check_packages([\"dashscope\"])\n    except:\n        yield from update_ui_latest_msg(f\"导入软件依赖失败。使用该模型需要额外依赖，安装方法```pip install --upgrade dashscope```。\",\n                                         chatbot=chatbot, history=history, delay=0)\n        return\n","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/request_llms/bridge_qwen.py#L5-L41","documentation":"Watchdog RuntimeError('程序终止。') in bridge_qwen.predict_no_ui_long_connection: while draining QwenRequestInstance.generate, observe_window[1] (heartbeat timestamp) aged past watch_dog_patience (5s), so the loop aborts. It mirrors the cancellation contract of all other bridges - the Qwen dashscope/Tongyi stream itself rarely stalls this long, so the usual cause is the caller no longer feeding the dog.","triggerScenarios":"Caller stops refreshing observe_window[1] (Stop button, dead feeder thread); dashscope stream hangs >5s with no yield and the feeder was never started; pausing in a debugger.","commonSituations":"User cancels a Qwen generation in the WebUI; plugin reuses predict_no_ui_long_connection but omits the heartbeat thread; long first-token latency on Qwen API plus tight patience.","solutions":["If user-cancelled: expected behavior, handle cleanup and return","Run a feeder thread updating observe_window[1] every 1-2 seconds for the duration you want output","Increase watch_dog_patience if Qwen cold-start latency regularly exceeds 5s","Wrap the call in try/except RuntimeError to separate cancellation from API errors"],"exampleFix":"# before\now = [partial, time.time()]  # timestamp set once\n\n# after\nimport threading, time\nstop = threading.Event()\ndef feed():\n    while not stop.is_set():\n        ow[1] = time.time(); time.sleep(1)\nthreading.Thread(target=feed, daemon=True).start()","handlingStrategy":"try-catch","validationCode":"import time\nassert len(observe_window) >= 2\nobserve_window[1] = time.time()  # prime heartbeat before first chunk","typeGuard":null,"tryCatchPattern":"try:\n    result = predict_no_ui_long_connection(inputs, llm_kwargs, history, sys_prompt, observe_window=ow)\nexcept RuntimeError as e:\n    if str(e) == '程序终止。':\n        handle_user_cancel(ow[0])\n        return\n    raise","preventionTips":["Feed observe_window[1] from the UI thread that owns cancellation semantics","Keep feed interval well under watch_dog_patience (1-2s vs 5s)","Test cancellation paths in CI with a fake generator to keep the contract honest"],"tags":["qwen","watchdog","cancellation","concurrency","dashscope"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}