From 76b6e179279389e41be8caa89030c9c930ae4f35 Mon Sep 17 00:00:00 2001 From: caoqianming Date: Fri, 6 Mar 2026 09:38:14 +0800 Subject: [PATCH] feat: save event_loop handle to avoid ghost session --- src/connection.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index d314322..2dba2fa 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -98,6 +98,7 @@ pub struct ConnectionStatus { pub poll_points: Arc>, // 正在轮询的点集合 poll_handle: Option>, // 统一的轮询任务句柄 heartbeat_handle: Option>, // 心跳任务句柄 + event_loop_handle: Option>, // event_loop 任务句柄 } #[derive(Clone)] @@ -577,8 +578,13 @@ impl ConnectionManager { } }; - let _ = event_loop.spawn(); - session.wait_for_connection().await; + let event_loop_handle = event_loop.spawn(); + + if !session.wait_for_connection().await { + let error = "Session connection failed".to_string(); + self.fail_connect(source_id, &error).await; + return Err(error); + } let mut status = self.status.write().await; status.insert( @@ -595,6 +601,7 @@ impl ConnectionManager { poll_points: Arc::new(Vec::new()), poll_handle: None, heartbeat_handle: None, + event_loop_handle: Some(event_loop_handle), }, ); drop(status); // 显式释放锁,在调用 start_unified_poll_task 之前 @@ -625,6 +632,7 @@ impl ConnectionManager { poll_points: Arc::new(Vec::new()), poll_handle: None, heartbeat_handle: None, + event_loop_handle: None, }, ); } @@ -661,6 +669,10 @@ impl ConnectionManager { if let Some(handle) = conn_status.heartbeat_handle.take() { handle.abort(); } + // 停止 event_loop 任务 + if let Some(handle) = conn_status.event_loop_handle.take() { + handle.abort(); + } } } @@ -693,6 +705,10 @@ impl ConnectionManager { if let Some(handle) = conn_status.heartbeat_handle.take() { handle.abort(); } + // 停止 event_loop 任务 + if let Some(handle) = conn_status.event_loop_handle.take() { + handle.abort(); + } } }