Skip to content

Commit

Permalink
perf(rsjudge-runner): ⚡️ make wait_for_resource_usage really async
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisu-Woniu committed Jun 7, 2024
1 parent 0901040 commit 02d116a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/rsjudge-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ nix = { version = "0.29.0", features = ["user", "resource", "process"] }
rsjudge-traits.workspace = true
rsjudge-utils.workspace = true
thiserror = "1.0.61"
tokio = { version = "1.38.0", features = ["process", "sync", "time"] }
tokio = { version = "1.38.0", features = ["process", "sync", "time", "signal"] }
tokio-util = "0.7.11"
uzers = "0.12.0"

Expand Down
21 changes: 19 additions & 2 deletions crates/rsjudge-runner/src/utils/resources/rusage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use nix::{
use tokio::{
process::Child,
select,
task::{spawn, spawn_blocking},
signal::unix::{signal, SignalKind},
task::spawn,
time::sleep,
};
use tokio_util::sync::CancellationToken;
Expand Down Expand Up @@ -110,7 +111,23 @@ pub fn wait4<P: Into<Option<Pid>>>(
impl WaitForResourceUsage for Child {
async fn wait_for_resource_usage(&mut self) -> Result<Option<(ExitStatus, ResourceUsage)>> {
if let Some(pid) = self.id() {
Ok(spawn_blocking(move || wait4(Pid::from_raw(pid as _), None)).await??)
Ok(Some(
spawn(async move {
let mut sigchld_stream = signal(SignalKind::child())?;
loop {
if let Some(status) =
wait4(Pid::from_raw(pid as _), Some(WaitPidFlag::WNOHANG))?
{
return Ok::<_, Error>(status);
}

if sigchld_stream.recv().await.is_none() {
Err(Errno::ECHILD)?
}
}
})
.await??,
))
} else {
let exit_status = self
.try_wait()?
Expand Down

0 comments on commit 02d116a

Please sign in to comment.