fix(proxy-tunnel): 修复隧道连接池竞态与跨平台兼容问题

- 修复 TCP keepalive with_retries 在 Windows 上不可用的编译问题
- dispatcher 中 try_send 失败时记录警告日志而非静默丢弃
- 优化 handler_handles 清理策略,每 64 帧定期清理
- StreamState 记住原始连接引用,清理时避免连接池竞态
This commit is contained in:
fawney19
2026-02-27 21:41:43 +08:00
parent ee356c5e6e
commit 8de2f41924
4 changed files with 55 additions and 22 deletions

View File

@@ -63,7 +63,7 @@ class TunnelConnection:
raise TunnelStreamError("frame send timeout (writer congested)")
def create_stream(self, stream_id: int) -> _StreamState:
state = _StreamState(stream_id)
state = _StreamState(stream_id, conn=self)
self._pending_streams[stream_id] = state
return state
@@ -109,9 +109,10 @@ class _StreamState:
"_body_chunks",
"_done_event",
"_error",
"_conn",
)
def __init__(self, stream_id: int) -> None:
def __init__(self, stream_id: int, conn: TunnelConnection | None = None) -> None:
self.stream_id = stream_id
self.status: int = 0
self.headers: list[list[str]] = []
@@ -119,6 +120,7 @@ class _StreamState:
self._body_chunks: asyncio.Queue[bytes | None] = asyncio.Queue()
self._done_event = asyncio.Event()
self._error: str | None = None
self._conn = conn
def set_response_headers(self, status: int, headers: list[list[str]] | dict[str, str]) -> None:
self.status = status

View File

@@ -95,7 +95,10 @@ class TunnelTransport(httpx.AsyncBaseTransport):
def _cleanup_stream(self, manager: TunnelManager, stream_state: _StreamState | None) -> None:
if stream_state is None:
return
conn = manager.get_connection(self._node_id)
# 优先从 stream 记住的原始连接上移除,避免连接池竞态
conn = stream_state._conn
if conn is None:
conn = manager.get_connection(self._node_id)
if conn:
conn.remove_stream(stream_state.stream_id)
@@ -120,8 +123,10 @@ class TunnelResponseStream(httpx.AsyncByteStream):
yield chunk
async def aclose(self) -> None:
# 确保 stream 从 connection 的 pending 列表中移除,防止内存泄漏
conn = self._manager.get_connection(self._node_id)
# stream 记住的原始连接上精确移除,避免连接池竞态
conn = self._stream_state._conn
if conn is None:
conn = self._manager.get_connection(self._node_id)
if conn:
conn.remove_stream(self._stream_state.stream_id)