Skip to content

Commit ece6484

Browse files
committed
avoid using write_all to ensure select! cancellation safe
1 parent f639376 commit ece6484

File tree

3 files changed

+12
-21
lines changed

3 files changed

+12
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "omnip"
3-
version = "0.6.2"
3+
version = "0.6.3"
44
edition = "2021"
55

66
[lib]

src/server.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -976,32 +976,23 @@ impl Server {
976976
Ok(0)
977977
}
978978
Ok(n) => {
979+
// this method is used as a select! branch, use while loop to write the data
980+
// instead of using write_all (which is not cancellation safe) to ensure
981+
// the code cancellation safe
982+
let mut written_bytes = 0;
983+
while written_bytes < n {
984+
written_bytes += writer
985+
.write(&buffer[written_bytes..n])
986+
.await
987+
.map_err(|_| ProxyError::InternalError)?;
988+
}
979989
*out_bytes += n as u64;
980-
writer
981-
.write_all(&buffer[..n])
982-
.await
983-
.map_err(|_| ProxyError::InternalError)?;
984990
Ok(n)
985991
}
986992
Err(_) => Err(ProxyError::InternalError), // Connection mostly reset by peer
987993
}
988994
}
989995

990-
// async fn resolve_net_addr(&self, addr: &NetAddr) -> Result<SocketAddr> {
991-
// if addr.is_ip() {
992-
// return Ok(addr.to_socket_addr().unwrap());
993-
// }
994-
//
995-
// let resolver = if addr.is_internal_domain() {
996-
// inner_state!(self, system_dns_resolver).clone()
997-
// } else {
998-
// inner_state!(self, dns_resolver).clone()
999-
// };
1000-
//
1001-
// let ip_arr = resolver.unwrap().lookup(addr.unwrap_domain()).await?;
1002-
// Ok(SocketAddr::new(*ip_arr.first().unwrap(), addr.port))
1003-
// }
1004-
1005996
fn collect_and_report_server_stats(&self, mut stats_receiver: Receiver<ServerStats>) {
1006997
let inner_state = self.inner_state.clone();
1007998
tokio::spawn(async move {

0 commit comments

Comments
 (0)