Skip to content

Commit c7b81c4

Browse files
committed
Ensure the fd will be release completely
This problem is caused by the reference count not being able to return to 0 properly. Signed-off-by: jokemanfire <hu.dingyang@zte.com.cn>
1 parent 44b31f7 commit c7b81c4

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/sync/client.rs

Lines changed: 21 additions & 15 deletions
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,28 @@ impl Client {
9898
trace!("Sender quit");
9999
});
100100

101-
//Recver
101+
//Reciver
102102
let receiver_connection = connection;
103-
let receiver_client = client.clone();
103+
//this thread should use weak arc for ClientConnection, otherwise the thread will occupy a reference count of ClientConnection's arc,
104+
//ClientConnection's drop will be not call until the thread finished. It means if all the external references are finished,
105+
//this thread should be release.
106+
let receiver_client = weak_client.clone();
104107
thread::spawn(move || {
105108
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;
109+
//The count of ClientConnection's Arc will be add one , and back to original value when this code ends.
110+
if let Some(receiver_client) = receiver_client.upgrade(){
111+
match receiver_client.ready() {
112+
Ok(None) => {
113+
continue;
114+
}
115+
Ok(_) => {}
116+
Err(e) => {
117+
error!("pipeConnection ready error {:?}", e);
118+
break;
119+
}
114120
}
121+
} else {
122+
break;
115123
}
116124

117125
match read_message(&receiver_connection) {
@@ -140,10 +148,6 @@ impl Client {
140148
};
141149
}
142150

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

@@ -191,7 +195,9 @@ impl Client {
191195

192196
impl Drop for ClientConnection {
193197
fn drop(&mut self) {
198+
//close all fd , make sure all fd have been release
194199
self.close().unwrap();
200+
self.close_receiver().unwrap();
195201
trace!("Client is dropped");
196202
}
197203
}

src/sync/sys/unix/net.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ use crate::common::{self, client_connect, SOCK_CLOEXEC};
2828
use crate::common::set_fd_close_exec;
2929
use nix::sys::socket::{self};
3030

31+
//The libc::poll's max wait time
32+
const POLL_MAX_TIME: i32 = 10;
33+
3134
pub struct PipeListener {
3235
fd: RawFd,
3336
monitor_fd: (RawFd, RawFd),
@@ -104,7 +107,7 @@ impl PipeListener {
104107
libc::poll(
105108
pollers as *mut _ as *mut libc::pollfd,
106109
pollers.len() as _,
107-
-1,
110+
POLL_MAX_TIME,
108111
)
109112
};
110113

@@ -278,7 +281,7 @@ impl ClientConnection {
278281
libc::poll(
279282
pollers as *mut _ as *mut libc::pollfd,
280283
pollers.len() as _,
281-
-1,
284+
POLL_MAX_TIME,
282285
)
283286
};
284287

0 commit comments

Comments
 (0)