fix: improve exception handling and resource cleanup in HTTP request executor

This commit is contained in:
zhouyy 2025-03-18 17:59:52 +08:00
parent 98f6c516f1
commit 27d5535b54

View File

@ -283,7 +283,7 @@ class Executor:
return executor_response return executor_response
except Exception as e: except Exception as e:
# 确保在发生异常时关闭响应 # Ensure response is closed when an exception occurs
response.close() response.close()
raise e raise e
@ -310,12 +310,12 @@ class Executor:
"max_retries": self.max_retries, "max_retries": self.max_retries,
} }
# 使用 with 语句来确保资源正确释放 # Use with statement to ensure proper resource cleanup
with httpx.Client() as client: with httpx.Client() as client:
try: try:
response = getattr(client, self.method.lower())(**request_args) response = getattr(client, self.method.lower())(**request_args)
# 创建一个新的 Response 对象并复制需要的数据 # Create a new Response object and copy required data
# 这样可以安全地关闭原始响应 # This allows safe closure of the original response
copied_response = response.copy() copied_response = response.copy()
response.close() response.close()
return copied_response return copied_response