使用multiprocessing生成子进程导致Socket未关闭的HTTP请求异常
HTTP请求异常排查:Uvicorn长连接超时后的奇怪行为
启动子进程后遇到以下异常情况:
- 当uvicorn的
timeout_keep_alive超时后,点击浏览器的“stop”按钮会出现无限加载,且无HTTP连接日志输出 - 点击其他按钮或刷新页面能正常工作,但会收到两次响应
- Netstat显示端口60862的Socket处于绑定打开状态,但uvicorn日志中却有
TRACE: 127.0.0.1:60862 - HTTP connection lost的记录
相关代码
import multiprocessing import os import time import uvicorn from fastapi import FastAPI app = FastAPI() processes = [] def keep_alive_process(): while True: print(f"process {os.getpid()} is alive") time.sleep(1) @app.post("/start") async def start_processes(): for i in range(4): process = multiprocessing.Process(target=keep_alive_process, args=()) processes.append(process) process.start() return {'status': 'started'} @app.post("/stop") async def stop_processes(): for process in processes: process.kill() processes.clear() return {'status': 'stopped'} if __name__ == '__main__': uvicorn.run('main:app', timeout_keep_alive=10, log_level='trace')
日志信息
TRACE: 127.0.0.1:60862 - HTTP connection made TRACE: 127.0.0.1:60862 - ASGI [2] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8010), 'client': ('127.0.0.1', 60862), 'scheme': 'http', 'root_path': '', 'headers': '<...>', 'method': 'POST', 'path': '/start', 'raw_path': b'/start', 'query_string': b''} TRACE: 127.0.0.1:60862 - ASGI [2] Send {'type': 'http.response.start', 'status': 200, 'headers': '<...>'} TRACE: 127.0.0.1:60862 - ASGI [2] Send {'type': 'http.response.body', 'body': '<20 bytes>'} TRACE: 127.0.0.1:60862 - ASGI [2] Completed process 63912 is alive process 63913 is alive process 63914 is alive process 63915 is alive INFO: 127.0.0.1:60862 - "POST /start HTTP/1.1" 200 OK process 63912 is alive process 63913 is alive TRACE: 127.0.0.1:60862 - HTTP connection lost .... process 63912 is alive INFO: 127.0.0.1:59092 - "POST /stop HTTP/1.1" 200 OK INFO: 127.0.0.1:59092 - "POST /stop HTTP/1.1" 200 OK TRACE: 127.0.0.1:59092 - HTTP connection made TRACE: 127.0.0.1:59092 - ASGI [3] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8010), 'client': ('127.0.0.1', 59092), 'scheme': 'http', 'root_path': '', 'headers': '<...>', 'method': 'POST', 'path': '/stop', 'raw_path': b'/stop', 'query_string': b''} TRACE: 127.0.0.1:59092 - ASGI [3] Send {'type': 'http.response.start', 'status': 200, 'headers': '<...>'} TRACE: 127.0.0.1:59092 - ASGI [3] Send {'type': 'http.response.body', 'body': '<20 bytes>'} TRACE: 127.0.0.1:59092 - ASGI [3] Completed TRACE: 127.0.0.1:59092 - ASGI [4] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8010), 'client': ('127.0.0.1', 59092), 'scheme': 'http', 'root_path': '', 'headers': '<...>', 'method': 'POST', 'path': '/stop', 'raw_path': b'/stop', 'query_string': b''} TRACE: 127.0.0.1:59092 - ASGI [4] Send {'type': 'http.response.start', 'status': 200, 'headers': '<...>'} TRACE: 127.0.0.1:59092 - ASGI [4] Send {'type': 'http.response.body', 'body': '<20 bytes>'} TRACE: 127.0.0.1:59092 - ASGI [4] Completed TRACE: 127.0.0.1:59092 - HTTP connection lost
Swagger界面截图

内容的提问来源于stack exchange,提问作者Xavatu
相关产品推荐
相关产品推荐

