Skip to content

Commit 1028725

Browse files
lifupanTim-Zhang
authored andcommitted
Merge pull request #298 from Tim-Zhang/fix-server-drop
async/server: Fix the the dropping of the server object
2 parents 7365e72 + c1f8cde commit 1028725

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/asynchronous/server.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ impl Server {
219219
drop(incoming);
220220

221221
fd_tx.send(dup_fd).await.unwrap();
222-
break;
223222
}
223+
break;
224224
}
225225
}
226226
}
@@ -327,14 +327,17 @@ impl Builder for ServerBuilder {
327327
server_shutdown: self.shutdown_waiter.clone(),
328328
handler_shutdown: disconnect_notifier,
329329
},
330-
ServerWriter { rx, _server_shutdown: self.shutdown_waiter.clone() },
330+
ServerWriter {
331+
rx,
332+
_server_shutdown: self.shutdown_waiter.clone(),
333+
},
331334
)
332335
}
333336
}
334337

335338
struct ServerWriter {
336339
rx: MessageReceiver,
337-
_server_shutdown: shutdown::Waiter
340+
_server_shutdown: shutdown::Waiter,
338341
}
339342

340343
#[async_trait]
@@ -381,8 +384,8 @@ impl ReaderDelegate for ServerReader {
381384
async fn handle_msg(&self, msg: GenMessage) {
382385
let handler_shutdown_waiter = self.handler_shutdown.subscribe();
383386
let context = self.context();
384-
//Check if it is already shutdown no need select wait
385-
if !handler_shutdown_waiter.is_shutdown(){
387+
//Check if it is already shutdown no need select wait
388+
if !handler_shutdown_waiter.is_shutdown() {
386389
let (wait_tx, wait_rx) = tokio::sync::oneshot::channel::<()>();
387390
spawn(async move {
388391
select! {
@@ -683,3 +686,36 @@ impl HandlerContext {
683686
.ok();
684687
}
685688
}
689+
690+
#[cfg(target_os = "linux")]
691+
#[cfg(test)]
692+
mod tests {
693+
use super::*;
694+
695+
pub const SOCK_ADDR: &str = r"unix://@/tmp/ttrpc-server-unit-test";
696+
697+
pub fn is_socket_in_use(sock_path: &str) -> bool {
698+
let output = std::process::Command::new("bash")
699+
.args(["-c", &format!("lsof -U|grep {}", sock_path)])
700+
.output()
701+
.expect("Failed to execute lsof command");
702+
703+
output.status.success()
704+
}
705+
706+
#[tokio::test]
707+
async fn test_server_lifetime() {
708+
let addr = SOCK_ADDR
709+
.strip_prefix("unix://@")
710+
.expect("socket address is not expected");
711+
{
712+
let mut server = Server::new().bind(SOCK_ADDR).unwrap();
713+
server.start().await.unwrap();
714+
assert!(is_socket_in_use(addr));
715+
}
716+
717+
// Sleep to wait for shutdown of server caused by server's lifetime over
718+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
719+
assert!(!is_socket_in_use(addr));
720+
}
721+
}

0 commit comments

Comments
 (0)