Repository navigation
fix: ProxyManager stop 后端口未释放 — graceful_shutdown + task.abort 双保险 - #207
Merged
Merged
Conversation
silent failure 修: 用户点"停止转发"后 UI/日志显示 "forwarding stopped" 但端口实际仍被占用。 根因: stop_silent (proxy_runner.rs:127) 只 fire-and-forget 发 oneshot shutdown 信号立刻返, 但 axum::serve.with_graceful_shutdown (start line 81-87) 收到信号后**等所有 in-flight HTTP connection 完成**才 drop listener。SSE / long polling / keep-alive idle / hung connection 可能 永远等不到 → listener 不 drop → 端口不释放, 但 UI / log 已"stopped" → 用户感知"日志说停了实际没停"。 修法: - ProxyHandle 加 task: tokio::task::JoinHandle<()> 字段, start 把 tokio::spawn 的 JoinHandle 存进 handle。 - stop / stop_silent 在 shutdown_tx.send(()) **之后**立刻调 h.task.abort() 强制 drop server future → listener 同步 drop → 端口立刻释放, 不依赖 graceful drain。 - race condition path (proxy already started by another path) 同样 abort 自己的 task 防 listener leak。 代价: in-flight HTTP connection 被强断, client 见 connection reset。 但"用户点停止 = 真要停"语义对, 这是合理 trade-off。 测试: 95/95 pass, 无 regression。proxy_runner.rs 现无单测基础设施, unit test 加 mock config_file 工程量大, 靠 PR 真机测验: 1. 启转发, lsof -iTCP:18080 应看到 axum process 2. 点停止 3. lsof -iTCP:18080 应**立即**空, 无残留 4. 立刻重启转发应成功(端口已释放, bind 不报 Address already in use)
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This was referenced May 18, 2026
Cmochance
added a commit
that referenced
this pull request
May 19, 2026
* fix(#208): 停止转发用 tokio::select! 强制 drop axum::serve 释放端口 之前用 `axum::serve(...).with_graceful_shutdown(rx)`:语义是等所有 in-flight connection 自然 drain 完才 drop listener。在 keep-alive HTTP / SSE / long-polling 场景永远 drain 不完 → listener 永远不 drop → **端口永远占用** → 用户感知"停止后仍能转发"(2026-05-18 真机多次复现)。 PR #207 的 `task.abort()` 也不够:tokio cancellation 是异步的,不保证 同步 drop listener;且 abort outer task 不影响 axum::serve 内部 spawn 的 connection sub-task。 改成 `tokio::select! { axum::serve, shutdown_rx }`:signal 一来 select 立即 wake → axum::serve future 整个 drop → 内部 sub-task + TcpListener **同步销毁** → 端口立即 free,不等 in-flight drain。 同时保留 application-level gate(in-flight request 二保险):signal send → task wake → future drop 有纳秒级窗口,gate 在 stop 时先 set true 让 middleware 立刻 503 + Connection: close 任何窗口内的 req。 Verify: - `crates/proxy/tests/stop_releases_port.rs`:2 个 integration test pass, 含关键 keep-alive client 持有 connection 时仍 ≤500ms 释放端口 - 实测:.app launch 后 lsof 18080 LISTEN,kill app → lsof 立即空 + python socket.bind 直接成功 Closes #208 * fix(#208): 自己写 accept loop + JoinSet 强制 abort in-flight connection PR #209 select! 方案不够强:虽然 axum::serve future drop 让 listener 释放(新 connection refused),但 axum::serve 内部对每个 connection 用裸 `tokio::spawn` 出 detached task,**外部无 handle**,future drop 不影响已 spawn 的 sub-task → 用户发过 1 条 message 后点停止转发, **同 connection 上的后续 keep-alive request 仍能 process** → "停止 后还在转发"(2026-05-18 用户真机复现)。 修复:不用 axum::serve,自己写 accept loop: - per-connection task 用 `JoinSet.spawn` 而**不**是裸 `tokio::spawn` → 拿到所有 connection task handle - stop 时 `connections.shutdown().await` = abort_all + 等所有 task die → reqwest stream / SSE / keep-alive request 全部立即被打断 → client 收 FIN/RST connection 关闭 Hyper service 桥接:axum Router 是 Service<Request<Body>>,hyper serve_connection 要 Service<Request<Incoming>>,用 tower map_request + TowerToHyperService 适配。 Verify:`crates/proxy/tests/stop_releases_port.rs` 重写: - spawn_production_like 跟 production proxy_runner.rs 同 pattern - test 1: 端口 ≤500ms 释放(基础) - test 2 **关键**: req1 keep-alive → stop → req2 必须失败(同 connection 上后续 req 应被 abort,或 reqwest 尝试新 connection 被 refused) ← 这是 PR #209 select! 方案 fail 的场景 两个 test pass。 * fix(#208): 加 CancellationToken + sub-task select,不再 task.abort PR #209 第二 commit (07d2664) 的 JoinSet + abort_all 仍 fail (2026-05-18 用户真机第二次复现): 18080 已无 LISTEN (listener drop ✓), 但 ESTABLISHED `127.0.0.1:18080->...` connection 仍 alive process keep-alive req → 用户感知"停止后还在转发"。 根因 (本 commit 修): 1. **stop_silent 调 task.abort() 抢先 cancel server task** —— task 跑不 到 `connections.shutdown().await`, sub-task 仍 detached。 2. 即使去掉 abort 让 task 跑完 shutdown(), tokio abort 是 schedule cancellation, 必须 sub-task **yield** 才生效。reqwest stream / SSE await upstream response 可能很久不 yield → abort 永远滞后。 修复 (本 commit): - 加 `tokio_util::sync::CancellationToken` - 每个 connection sub-task 内 `tokio::select! { conn.await, cancel.cancelled().await }` —— cancel 来时 select arm 立刻 wake → drop conn future → hyper connection drop → **TCP socket 同步 close → client 收 FIN/RST 立即断**, 不依赖 sub-task 主动 yield - stop / stop_silent: gate + cancel.cancel() + send shutdown_tx 三步同步触发, **不**调 task.abort() (否则抢先 cancel task) CancellationToken.cancel() 是 sync API, wake 所有 listener immediate, 比 tokio abort 的 schedule cancellation 强得多。 Integration test 同步更新 spawn_production_like 加 cancel token 参数 + sub-task select pattern。2 个 test pass。 * fix(#208): dup raw socket fd + cancel arm 直接 shutdown(SHUT_RDWR) 强制关 TCP PR #209 第三 commit (2398948) CancellationToken + sub-task select drop conn 仍 fail (2026-05-18 用户真机第三次复现): - 18080 listener drop ✓ (curl 直连 → Connection refused) - 但 `127.0.0.1:18080->127.0.0.1:<client_port> (ESTABLISHED)` server side socket **持续不消失** (lsof + netstat 双 verify) - → hyper conn future drop 不可靠让 underlying TcpStream 真 close socket 修复: 跨过 hyper / tokio 间接 cancellation 链,直接 OS-level 强制关 socket: 1. accept stream 后立即 `libc::dup(stream.as_raw_fd())` —— 持有 fd 副本 (跟 stream 共享同 socket inner state, 跟 stream 的 lifetime 解耦) 2. sub-task 内 select: - 正常完成 arm: conn 自然结束 - cancel arm: `libc::shutdown(dup_fd, SHUT_RDWR)` —— OS 立即发 FIN/RST 给 client, server side socket 强制 CLOSE 3. sub-task 退出时 `libc::close(dup_fd)` 释放副本 shutdown(SHUT_RDWR) 是 OS-level syscall, 绕开所有 user-space 的 future drop / async cancellation 延迟, **保证 socket 立即 close**, client 立即收 FIN/RST 断开。 Cross-platform: `#[cfg(unix)]` macOS + Linux 实现。Windows TODO (WSADuplicateSocket + closesocket), 加 #218 followup。 无需更新 integration test —— test 验 "req2 fail" 已 pass(因 reqwest 端口 refused), 真正 verify 在 user 真机 lsof 看 ESTABLISHED 是否消失。 * fix(#208): active_fds 表 + stop_silent 主动 shutdown 绕过整个 cancel chain PR #209 第四 commit (1be7c20) sub-task cancel arm 内 raw fd shutdown 仍 fail (2026-05-18 用户真机第四次复现): lsof 显示 sub-task 的 dup fd **消失**(说明 sub-task 已退),但 hyper accept 出的 stream fd 仍持续 hold,18080 ESTABLISHED connection 不消失。 矛盾分析: - sub-task 已退 → conn drop → io drop → stream Drop 应 close fd - 但 stream fd 仍 hold → 说明某环节断了 - 最可能 root: CancellationToken cancel signal 在 child_token 路径或 tokio runtime 调度未 propagate, sub-task 卡 select await 不退 修复 (本 commit): 完全绕过 user-space cancellation chain —— ProxyHandle 新增 `active_fds: Arc<Mutex<Vec<i32>>>` 表, 记录所有 active connection 的 dup socket fd; stop_silent / stop / race-condition 路径 **直接** lock 表 + `libc::shutdown(fd, SHUT_RDWR)` 主动让 OS kernel 发 FIN/RST。 - accept 时: `libc::dup(stream.as_raw_fd())` + push 到 active_fds - stop_silent 时: snapshot active_fds + foreach shutdown - sub-task 完成时: remove 自己 entry + close fd (避免 leak) shutdown 是 OS syscall, 跟 user-space 调度 / future drop / cancellation 完全无关 —— **kernel 同步发 FIN/RST 给 client, server side socket 立即进 closing state**, lsof ESTABLISHED 消失。 Race 处理: shutdown 不 close (sub-task 负责 close), 避免 fd 双重 close 误伤 OS 后续 reassigned fd。 Cross-platform: `#[cfg(unix)]` macOS + Linux 实现。Windows TODO。 * fix(#208): sub-task 不 close dup_fd (留给 stop) + fmt fix PR #209 第五 commit (10056d5) active_fds 仍 fail (lsof verify): stream fd 14u 仍 hold 但 dup fd 已消失 → sub-task 完成时 close 了 dup_fd , 然后 stop_silent 时 active_fds 已空, 失去强制 shutdown 的 fd。 根因: hyper `auto::Builder` 协商 H2 时 spawn 独立 h2 task 持有 stream, **outer sub-task 完成 ≠ inner h2 task 完成**。outer 完成 close dup_fd , inner h2 task 仍通过原 stream fd 工作。 修复 (本 commit): - **sub-task 完成时不 close dup_fd, 不 remove active_fds entry** —— 留 fd 给 stop_silent 统一关 - stop_silent 时 `force_shutdown_active_fds` take 整 vec + foreach `shutdown(SHUT_RDWR)` + `close(fd)` —— 强制 OS-level 关 socket, 不论 inner h2 task 状态如何 Trade-off: 连续 forward 期间 dup fd 累积 (每 connection 1 个), 直到 stop button 或 app exit 统一清理。Codex CLI keep-alive 连接复用, typical N=几十级, OS fd ulimit 256+ 够用。 同时修 cargo fmt (CI 10056d5 fail 是 fmt --check)。 * fix(#208): 移除所有兜底, proxy 跑独立 tokio Runtime + shutdown_background 一键停 前 5 个 commit 的层层兜底 (task.abort / select! drop / JoinSet abort_all / CancellationToken / raw fd shutdown / active_fds 表 / app-level gate middleware) 全部移除。用户明示: "停止转发等于停止所有功能只保留主界面, 我不知道你加这些兜底逻辑的意义是什么"。 核心方案改成最简单粗暴的: - proxy 跑在独立 `std::thread` + 独立 `tokio::runtime::Runtime` - stop 时调 `runtime.shutdown_background()` —— tokio 提供的 OS-level "杀光所有 task" 原语, 同步 abort runtime 上所有 spawn task, worker thread 退出, 没人 poll task → task drop, 持有的 TcpStream / TcpListener / 所有 fd cleanup - **完全不依赖** user-space cancellation chain (CancellationToken / JoinSet abort / hyper conn drop 等), shutdown_background 直接杀 worker thread 文件改动: - src-tauri/src/proxy_runner.rs: 重写, ProxyHandle 只持有 `tokio::runtime::Runtime` 一个核心字段 - crates/proxy/src/server.rs: 删 build_router_with_gate / shutdown_gate middleware, build_router 恢复纯路由 - crates/proxy/src/lib.rs: 删 build_router_with_gate export - crates/proxy/tests/stop_releases_port.rs: 删 (production 不再用 那 pattern) - src-tauri/Cargo.toml: 删 hyper / hyper-util / tokio-util / libc deps - crates/proxy/Cargo.toml: 删 hyper / hyper-util / tokio-util dev-deps 不影响: - Plugins 进程 (独立 OS process: macOS launcher / Windows IApplicationActivationManager), 不在 ProxyManager runtime 内 - Tauri 主 runtime / admin handler / 主界面 * hardening(#208): Runtime drop 移到独立 std::thread 避 async context panic stop / stop_silent 通过 admin handler (async fn) 触发时, ProxyHandle drop → Runtime drop 发生在 async context 内, 理论上踩 `Runtime::drop` "async context drop panic" 红线 (实测 tokio multi-thread + cross-runtime 没触发, 但 tokio 升级可能变严)。 加 `drop_runtime_off_thread` helper: ProxyHandle 整体 move 到独立 std::thread, 在 thread 内 shutdown_background + drop, 远离任何 async context, 100% 安全。Thread 在 closure 跑完后自动 exit, 不 leak。 5 行核心 (thread::Builder::new().name(...).spawn(move || { ... }).ok())。 * fix(#208): mpsc→oneshot + 删 std::thread wrapper (shutdown_background 本就 async-safe) 两条 Devin review: 1. `mpsc::recv()` 同步阻塞 Tauri worker thread (Devin P1 真问题): 改 `tokio::sync::oneshot.await`, start() 全程 non-blocking, yield 给 tokio 调度其他 task。 2. `drop_runtime_off_thread` spawn fail fallback panic: 重新审查发现 thread wrapper 整个是过度防御。`Runtime::shutdown_background` 是 tokio 显式提供 "useful if you want to drop a runtime from within another runtime" 的 API, **本身就 async-context-safe**, 不触发 panic。我之前判断错了。直接删 thread wrapper, 回到最简版本。 ChatGPT Codex 反馈 (WebSocket bypass): 评论旧 commit 的 shutdown_gate middleware, 已在 eb4d7a6 删除整个 middleware 改 runtime drop 方案。 runtime drop = 所有 task 一锅端 abort (含 WebSocket sub-task), bypass 问题不存在了。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
修用户报告的 silent failure:点"停止转发"后 UI 切 stopped + 日志
forwarding stopped,但端口实际未释放。Root cause
src-tauri/src/proxy_runner.rs:127-132stop_silent:配合
start里axum::serve(...).with_graceful_shutdown(rx)(line 81-87):Fix
ProxyHandle加task: JoinHandle<()>字段,stop/stop_silent双保险:shutdown_tx.send(())— graceful drain 机会让正常 request 跑完task.abort()— 强制 drop server future → listener 同步 drop → 端口立刻释放Race condition path("proxy already started by another path")同样 abort 自己 task 防 listener leak。
Trade-off:in-flight HTTP connection 被强断 client 见 connection reset。"用户点停止 = 真要停" 语义对,合理代价。
Test plan
cargo check -p codex-app-transferpasscargo fmt --checkcleancargo test --bin codex-app-transfer95/95 pass(无 regression)lsof -iTCP:18080(或当前 proxy 端口)应看到 axum processlsof -iTCP:18080应空(无残留 process listen)Address already in use)RunEvent::Exit→stop_silent)同 fix 路径,exit 后端口必释放