【Bug已解决】codex: "network timeout" / Request timeout exceeded — CodeX 网络超时解决方案
1. 问题描述
CodeX CLI 在请求过程中遇到网络超时错误:
# 请求超时
$ codex "分析代码"
Error: Request timeout exceeded 30000ms
# 或连接超时
$ codex –print "task" –max-turns 5
Error: ETIMEDOUT
Connection timed out to api.openai.com.
# 或流式超时
$ codex "长任务"
Error: Stream timeout
No data received for 30000ms.
# 或 DNS 超时
$ codex "task"
Error: ENOTFOUND
Could not resolve api.openai.com.
这个问题在以下场景中特别常见:
- 网络不稳定导致连接频繁断开
- 服务端在高负载下响应缓慢
- DNS 解析延迟过高
- 并发请求过多触发服务端限流
- SSL/TLS 握手耗时长
- 流量高峰期服务端过载
2. 原因分析
核心原理拆解
CodeX CLI → 发起 HTTP 请求 → 等待服务端响应
↓
超时 / DNS 失败 / 连接断开
↓
Request timeout exceeded
原因分类表
| 网络不稳定 | ETIMEDOUT | 约 35% |
| 服务端慢 | 30s 超时 | 约 25% |
| DNS 慢 | ENOTFOUND | 约 20% |
| 并发过多 | 429 限流 | 约 10% |
| SSL 握手慢 | TLS timeout | 约 5% |
| 流量高峰 | 过载 | 约 5% |
3. 解决方案
方案一:增加超时(最推荐)
# 步骤 1:增加请求超时
export CODEX_REQUEST_TIMEOUT=120000 # 120 秒
# 步骤 2:永久设置
echo 'export CODEX_REQUEST_TIMEOUT=120000' >> ~/.bashrc
source ~/.bashrc
# 步骤 3:或使用 –timeout
codex –timeout 120000 "task"
# 步骤 4:同时增加流式超时
export CODEX_STREAM_TIMEOUT=180000 # 180 秒
# 步骤 5:验证
codex –print "hello" –max-turns 1
方案二:检查网络连通性
# 步骤 1:测试连通性
ping -c 5 api.openai.com
# 查看 packet loss 和 RTT
# 步骤 2:检查 DNS 解析
nslookup api.openai.com
# 确认能解析到正确 IP
# 步骤 3:检查端口连通性
curl -I https://api.openai.com –max-time 10
# 确认 443 端口可达
# 步骤 4:检查带宽
# 使用 speed-test 或 fast.com 测试带宽
# 如果带宽过低,可能导致请求超时
# 步骤 5:验证
codex –print "hello" –max-turns 1
方案三:优化 DNS 解析
# 步骤 1:检查 DNS 解析速度
time nslookup api.openai.com
# 如果解析超过 2 秒,DNS 有问题
# 步骤 2:更换更快的 DNS 服务器
# macOS
networksetup -setdnsservers Wi-Fi 8.8.8.8 1.1.1.1
# Linux
sudo sh -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
nslookup api.openai.com
# 步骤 3:刷新 DNS 缓存
# macOS
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
# Linux
sudo systemd-resolve –flush-caches 2>/dev/null || sudo nscd -i hosts 2>/dev/null
# 步骤 4:验证解析速度
time nslookup api.openai.com
# 解析应在 500ms 以内
# 步骤 5:验证 CodeX
codex –print "hello" –max-turns 1
方案四:使用 –no-stream
# 步骤 1:非流式模式
codex –no-stream "task"
# 步骤 2:或 –print –no-stream
codex –print –no-stream "task" –max-turns 10
# 步骤 3:增加超时配合使用
export CODEX_REQUEST_TIMEOUT=120000
codex –no-stream "长任务"
# 步骤 4:验证
codex –print –no-stream "hello" –max-turns 1
方案五:重试机制
# 步骤 1:简单重试
codex "task" # 如果超时,手动重试
# 步骤 2:自动重试脚本
for i in 1 2 3 4 5; do
output=$(codex –print –no-stream "task" –max-turns 5 2>&1)
if echo "$output" | grep -qi "timeout\\|ETIMEDOUT\\|ENOTFOUND"; then
echo "Timeout, retry $i in 10s…"
sleep 10
else
echo "$output"
break
fi
done
# 步骤 3:CI/CD 中重试
codex –print –no-stream "task" –max-turns 5 || sleep 30 && codex –print –no-stream "task" –max-turns 5
# 步骤 4:配置文件中设置自动重试
cat > ~/.codex/config.json << 'EOF'
{
"maxRetries": 3,
"retryDelay": 5000
}
EOF
方案六:使用 –model 切换
# 步骤 1:切换到更快模型
codex –model gpt-4o-mini "task"
# 步骤 2:减少 –max-turns
codex –print "task" –max-turns 5
# 步骤 3:简化提示词
codex –print "分析 src/index.js" –max-turns 5
# 步骤 4:验证
codex –model gpt-4o-mini –print "hello" –max-turns 1
4. 各方案对比总结
| 方案一:增加超时 | 通用 | ⭐⭐⭐⭐⭐ | 低 |
| 方案二:检查网络 | 排查 | ⭐⭐⭐⭐⭐ | 低 |
| 方案三:优化 DNS | DNS 慢 | ⭐⭐⭐⭐⭐ | 低 |
| 方案四:–no-stream | 稳定性 | ⭐⭐⭐⭐⭐ | 低 |
| 方案五:重试 | 临时 | ⭐⭐⭐⭐⭐ | 低 |
| 方案六:切换模型 | 快速 | ⭐⭐⭐⭐ | 低 |
5. 常见问题 FAQ
5.1 默认超时多少
通常 30 秒。通过 CODEX_REQUEST_TIMEOUT 调整。
5.2 如何增加超时
export CODEX_REQUEST_TIMEOUT=120000 # 120 秒
5.3 ETIMEDOUT 是什么
连接超时。网络无法在指定时间内建立连接。通常由网络不稳定或服务端负载过高导致。
5.4 ENOTFOUND 是什么
DNS 解析失败。无法解析域名。检查 DNS 服务器配置或更换为 8.8.8.8 / 1.1.1.1。
5.5 如何检查网络
ping -c 5 api.openai.com
nslookup api.openai.com
curl -I https://api.openai.com –max-time 10
5.6 并发请求导致超时
减少同时发起的请求数量,使用 –max-turns 限制轮次,避免触发服务端 429 限流。
5.7 –no-stream 有帮助吗
有时。非流式模式不保持长连接,减少因连接空闲被断开的超时风险。
5.8 如何重试
codex –print –no-stream "task" –max-turns 5 || sleep 30 && codex –print –no-stream "task" –max-turns 5
5.9 gpt-4o-mini 更快吗
是的。gpt-4o-mini 响应更快,减少超时概率。适合简单任务。
5.10 排查清单速查表
□ 1. CODEX_REQUEST_TIMEOUT=120000
□ 2. codex –timeout 120000 "task"
□ 3. ping api.openai.com 检查网络
□ 4. nslookup 检查 DNS
□ 5. 更换 DNS 8.8.8.8 / 1.1.1.1
□ 6. 刷新 DNS 缓存
□ 7. –no-stream 非流式
□ 8. 重试: for + sleep + grep timeout
□ 9. –model gpt-4o-mini 更快
□ 10. –max-turns 5 减少轮次
6. 总结
故障排查流程图
flowchart TD
A[网络超时] –> B[增加超时]
B –> C[CODEX_REQUEST_TIMEOUT=120000]
C –> D[codex 验证]
D –> E{成功?}
E –>|是| F[✅ 问题解决]
E –>|否| G[检查网络连通性]
G –> H[ping api.openai.com]
H –> I{网络正常?}
I –>|否| J[切换网络/修复]
I –>|是| K[检查并发请求]
J –> D
K –> L{并发过多?}
L –>|是| M[减少 –max-turns]
L –>|否| N[使用 –no-stream]
M –> D
N –> O[codex –no-stream "task"]
O –> P{成功?}
P –>|是| F
P –>|否| Q[重试脚本]
Q –> R[for + sleep + grep timeout]
R –> S{成功?}
S –>|是| F
S –>|否| T[切换模型]
T –> U[–model gpt-4o-mini]
U –> D
D –> V{成功?}
V –>|是| F
V –>|否| W[检查 DNS]
W –> X[nslookup api.openai.com]
X –> Y{DNS 正常?}
Y –>|否| Z[更换 DNS 8.8.8.8]
Y –>|是| AA[避开高峰期]
Z –> D
AA –> AB[非高峰使用]
AB –> D
F –> AC[长期: 超时 + –no-stream + 重试]
AC –> AD[✅ 长期方案]




