问题现象
Windows 便携版 ComfyUI,本机生成绘图一切正常;局域网另一台电脑浏览器访问 ComfyUI 上传图片时,控制台间歇性爆出两条红色错误日志,生成任务不受影响,但是图片上传失败,日志节选:
ValueError: Reading after EOF
UnboundLocalError: cannot access local variable 'type_dir' where it is not associated with a value
完整报错堆栈:
[ERROR] Error handling request from 192.168.31.127
Traceback (most recent call last):
File "...\aiohttp\multipart.py", line 408, in _read_chunk_from_stream
raise ValueError("Reading after EOF")
ValueError: Reading after EOF
UnboundLocalError: cannot access local variable 'type_dir' where it is not associated with a value
注意:日志里的进度条
16%|█████████████▎ | 4/25属于 AI 绘图生成任务,绘图任务和图片上传报错是两条完全独立的请求,上传报错并不会打断文生图、图生图的渲染进程。
报错根因拆解
报错一:ValueError: Reading after EOF
触发位置:upload_image 图片上传接口、aiohttp multipart 文件解析器。
- 本质:客户端上传图片中途网络连接断开,浏览器提前终止了上传数据流,但是 ComfyUI 后端还在尝试读取表单数据。
- 高发诱因(局域网远程访问场景)
- 上传图片时浏览器切换标签页、刷新页面、关闭网页,上传被强制中断
- 局域网 WiFi 信号不稳定,出现网络丢包
- ComfyUI 便携包内置新版
aiohttp 3.10+存在兼容性 Bug,客户端断流后未优雅捕获 EOF 异常,直接抛出崩溃日志 - FRP、Nginx、路由器端口转发等反向代理中途截断上传请求
- 第三方移动端面板、远程前端插件发起异常上传请求
报错二:UnboundLocalError: cannot access local variable 'type_dir' where it is not associated with a value
触发文件:ComfyUI/server.py,函数 get_dir_by_type()
- 问题根源:原始源码中
get_dir_by_type只做了input/temp/output三个分支判断,缺少 else 兜底分支。当远程客户端上传请求携带了一个非法、未知的image_upload_type参数时,所有if‑elif条件全部跳过,变量type_dir、dir_type自始至终没有被赋值;代码最后执行return type_dir, dir_type,变量不存在直接抛出异常。
分步修复方案(由易到难)
修复 1:源码补丁根治 UnboundLocalError
打开文件路径: ComfyUI_windows_portable\ComfyUI\server.py 找到 get_dir_by_type 原始函数,原版缺陷代码:
def get_dir_by_type(image_upload_type):
if image_upload_type == "input":
type_dir = folder_paths.get_input_directory()
dir_type = "input"
elif image_upload_type == "temp":
type_dir = folder_paths.get_temp_directory()
dir_type = "temp"
elif image_upload_type == "output":
type_dir = folder_paths.get_output_directory()
dir_type = "output"
# 缺失else兜底分支!非法参数直接跳过所有赋值
return type_dir, dir_type
打上兜底补丁后的修复代码(直接全量替换):
def get_dir_by_type(image_upload_type):
if image_upload_type == "input":
type_dir = folder_paths.get_input_directory()
dir_type = "input"
elif image_upload_type == "temp":
type_dir = folder_paths.get_temp_directory()
dir_type = "temp"
elif image_upload_type == "output":
type_dir = folder_paths.get_output_directory()
dir_type = "output"
else:
# 非法上传类型参数,兜底默认存入input目录,防止变量未赋值报错
type_dir = folder_paths.get_input_directory()
dir_type = "input"
return type_dir, dir_type
保存文件,重启 ComfyUI 服务即可。
修复 2:降级 aiohttp,解决 Reading after EOF 断流报错
新版 aiohttp(3.10 及以上版本)multipart 上传断流处理存在缺陷,ComfyUI 便携包推荐锁定稳定版本 3.9.5。
- 在
ComfyUI_windows_portable根目录打开 CMD 命令行 - 执行命令:
python_embeded\python.exe -m pip install aiohttp==3.9.5 --force-reinstall
修复 3:修改启动脚本,延长局域网 HTTP 超时时间
编辑启动脚本 run_nvidia_gpu.bat,找到启动main.py的代码行,添加--listen局域网监听参数以及超时配置:
python main.py --listen 0.0.0.0 --timeout 600
--listen 0.0.0.0:允许局域网内其他设备访问 ComfyUI 服务--timeout 600:HTTP 连接超时设置为 10 分钟,大图片上传不容易被服务端主动切断
修复 4:客户端侧排查优化(报错设备 192.168.31.127)
- 使用 Chrome / Edge 浏览器无痕模式打开 ComfyUI 网页,禁用所有浏览器插件,排除扩展程序干扰
- 上传图片期间禁止切换标签页、刷新页面、关闭网页,等待上传进度完成
- 优先上传小尺寸图片测试链路稳定性
- 如果使用第三方移动端、远程前端面板访问 ComfyUI,优先换回官方原版网页界面,很多第三方客户端会发送错误的上传类型参数触发报错
修复 5:反向代理场景额外配置
如果你通过 FRP、Nginx、路由器虚拟服务器端口转发访问 ComfyUI,需要在代理配置文件调高客户端上传超时时间,禁止代理中途切断长连接。
临时规避方案(不想修改源码时)
直接在运行 ComfyUI 的本机浏览器打开 http://127.0.0.1:8188 上传素材,本地访问不会触发该局域网上传异常;两条报错几乎只会出现在跨设备远程访问场景。
总结
Reading after EOF:网络层面上传中途断流 + aiohttp 版本兼容问题;降级 aiohttp + 加长超时即可大幅缓解UnboundLocalError:ComfyUI 后端源码缺少参数兜底分支,打上 else 补丁可以一劳永逸根治- 两条报错均属于图片上传接口异常,不会影响 AI 绘图生成任务正常运行。