Skip to content

Commit 458e894

Browse files
committed
Ensure the fd will be release completely
Signed-off-by: jokemanfire <[email protected]>
1 parent 44b31f7 commit 458e894

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

Diff for: src/sync/client.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Client {
6262

6363
fn new_client(pipe_client: ClientConnection) -> Result<Client> {
6464
let client = Arc::new(pipe_client);
65-
65+
let weak_client = Arc::downgrade(&client);
6666
let (sender_tx, rx): (Sender, Receiver) = mpsc::channel();
6767
let recver_map_orig = Arc::new(Mutex::new(HashMap::new()));
6868

@@ -98,20 +98,26 @@ impl Client {
9898
trace!("Sender quit");
9999
});
100100

101-
//Recver
101+
//Reciver
102102
let receiver_connection = connection;
103-
let receiver_client = client.clone();
103+
//Clone as weak Arc , it will not add the count of client
104+
let receiver_client = weak_client.clone();
104105
thread::spawn(move || {
105106
loop {
106-
match receiver_client.ready() {
107-
Ok(None) => {
108-
continue;
109-
}
110-
Ok(_) => {}
111-
Err(e) => {
112-
error!("pipeConnection ready error {:?}", e);
113-
break;
107+
108+
if let Some(receiver_client) = receiver_client.upgrade(){
109+
match receiver_client.ready() {
110+
Ok(None) => {
111+
continue;
112+
}
113+
Ok(_) => {}
114+
Err(e) => {
115+
error!("pipeConnection ready error {:?}", e);
116+
break;
117+
}
114118
}
119+
}else{
120+
break;
115121
}
116122

117123
match read_message(&receiver_connection) {
@@ -140,10 +146,6 @@ impl Client {
140146
};
141147
}
142148

143-
let _ = receiver_client
144-
.close_receiver()
145-
.map_err(|e| warn!("failed to close with error: {:?}", e));
146-
147149
trace!("Receiver quit");
148150
});
149151

@@ -191,7 +193,9 @@ impl Client {
191193

192194
impl Drop for ClientConnection {
193195
fn drop(&mut self) {
196+
//close all fd , make sure all fd have been release
194197
self.close().unwrap();
198+
self.close_receiver().unwrap();
195199
trace!("Client is dropped");
196200
}
197201
}

Diff for: src/sync/sys/unix/net.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::common::{self, client_connect, SOCK_CLOEXEC};
2828
use crate::common::set_fd_close_exec;
2929
use nix::sys::socket::{self};
3030

31+
const POLL_MAX_TIME: i32 = 10;
3132
pub struct PipeListener {
3233
fd: RawFd,
3334
monitor_fd: (RawFd, RawFd),
@@ -104,7 +105,7 @@ impl PipeListener {
104105
libc::poll(
105106
pollers as *mut _ as *mut libc::pollfd,
106107
pollers.len() as _,
107-
-1,
108+
POLL_MAX_TIME,
108109
)
109110
};
110111

@@ -278,7 +279,7 @@ impl ClientConnection {
278279
libc::poll(
279280
pollers as *mut _ as *mut libc::pollfd,
280281
pollers.len() as _,
281-
-1,
282+
POLL_MAX_TIME,
282283
)
283284
};
284285

0 commit comments

Comments
 (0)