You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

正常运行时API返回500,调试模式却正常的异步请求问题

问题分析:异步请求生产环境500错误但调试正常

正常运行代码时API端点返回500错误,逐行调试却能正常执行,怀疑问题出在异步HTTP请求逻辑中。目前观察到ic无输出、异常日志中的{e}为空,控制台输出如下:

ERROR::subtask_logger::External API request returned error: 
INFO:     127.0.0.1:58716 - "POST /fwr/connections/order HTTP/1.1" 500 Internal Server Error

核心请求代码

try:
    async with httpx.AsyncClient(verify=False) as client:
        response = await client.post(
            config['api']['url'],
            json=json.loads(json_data),
            headers=headers,
        )
    ic(response.content)
    ic(response.headers)
    response.raise_for_status()
    return response
except httpx.ProxyError as e:
    logger.error(f"Error connecting to proxy: {e}", extra=d)
    raise HTTPException(status_code=503, detail="Error connecting to proxy") from e
except httpx.HTTPStatusError as e:
    logger.error(f"External API returned error (status: {e.response.status_code}): {e.response.text}", extra=d)
    raise HTTPException(status_code=e.response.status_code,
                        detail="External API returned error: {e.response.text}") from e
except httpx.ConnectError as e:
    logger.error(f"Could not connect to External API: {e}", extra=d)
    raise HTTPException(status_code=503, detail="Could not connect to External API") from e
except httpx.RequestError as e:  # General httpx request error
    logger.error(f"External API request returned error: {e}", extra=d)
    raise HTTPException(status_code=500, detail="External API request returned error: {e}") from e
except OSError as e:  # This might still catch underlying OS errors
    logger.error(f"OS error: {e}", extra=d)
    raise HTTPException(status_code=500, detail="OS error: {e}") from e

请求调用代码片段

async def request(self, task_id: uuid.UUID, db: Session, chunk_size=100): 

...
        crh = ProcessingRequestHandler(task_id)
...
        processing_payload_json_string = connections_json_payload.model_dump_json()
        processing_response = await crh.request(processing_payload_json_string)
...
    if i > 0: # There are records to process
        return task_id
    else: # no records to process
        return None

API端点代码

async def request_processing(db: Session = Depends(get_db)) -> JSONResponse:
task_id = uuid4()
try:
    processing = ProcessingTool()
    result = await processing.request(task_id, chunk_size=100, db=db)
    if result is None:
        message = {"task_id": str(task_id), "status": "failure", "message": "No items to process"}
    else:
        message = {"task_id": str(task_id), "status": "success", "message": "Request processed successfully"}
    return JSONResponse(status_code=200, content=message)
except HTTPException:
    raise
except Exception as e:
    raise HTTPException(status_code=500, detail="Internal error while processing request") from e

排查方向

  • ic无输出:说明异常发生在async with httpx.AsyncClient块内部(请求发送或客户端初始化阶段),导致后续代码未执行。
  • 异常信息为空:检查异常抛出的字符串格式化,比如detail="External API request returned error: {e}"未使用f-string,会导致{e}无法被解析为实际异常内容;同时需确认e本身是否被正确捕获。
  • 异步环境差异:调试时为同步执行,生产环境是异步调度,可能存在资源竞争、未设置超时(外部API响应慢触发超时异常)、json.loads(json_data)在异步场景下的隐式数据问题。
  • 数据库会话问题:异步环境中使用同步Session可能存在线程安全问题,单线程调试无异常,多线程异步调度时出错。
  • 超时配置缺失:给client.post添加timeout参数(如timeout=httpx.Timeout(10.0)),避免因无超时导致的隐性异常。

内容的提问来源于stack exchange,提问作者Smok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 22:13:12