From 8b25ed4ddaf5e1ff310886671ce6b58b66bf2d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=E1=BB=B3nh=20Minh=20Khang?= <91047241+akagiyuu@users.noreply.github.com> Date: Thu, 17 Sep 2026 01:39:21 +0700 Subject: [PATCH] Revert "debug" --- src/judge.rs | 53 +++++++++++++++++++---------------------- src/sandbox/mod.rs | 15 +++++------- src/sandbox/resource.rs | 3 +-- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/src/judge.rs b/src/judge.rs index 2fd79d3..59e572a 100644 --- a/src/judge.rs +++ b/src/judge.rs @@ -48,12 +48,12 @@ impl Judge { #[builder(default = Duration::from_secs(1))] idle_time_limit: Duration, ) -> io::Result> { let project_path = env::temp_dir().join(Uuid::new_v4().to_string()); - fs::create_dir(&project_path).await.unwrap(); + fs::create_dir(&project_path).await?; let main_path = project_path .join(MAIN) .with_extension(main.language.extension); - fs::write(&main_path, main.content).await.unwrap(); + fs::write(&main_path, main.content).await?; if let Some(checker) = &checker { let mut checker_path = project_path.join(CHECKER); if checker.language.is_interpreted() { @@ -65,10 +65,9 @@ impl Judge { .truncate(true) .mode(0o755) .open(&checker_path) - .await - .unwrap(); - checker_file.write_all(checker.content).await.unwrap(); - checker_file.sync_all().await.unwrap(); + .await?; + checker_file.write_all(checker.content).await?; + checker_file.sync_all().await?; } Ok(Judge { @@ -90,8 +89,8 @@ impl Judge { #[switch_to(Compiled)] pub async fn compile(self) -> io::Result, Verdict>> { if let Some(mut cmd) = self.language.get_compile_command(MAIN) { - let mut process = cmd.current_dir(&self.project_path).spawn().unwrap(); - let status = process.wait().await.unwrap(); + let mut process = cmd.current_dir(&self.project_path).spawn()?; + let status = process.wait().await?; if !status.success() { return Ok(Err(Verdict::CompilationError)); } @@ -122,38 +121,36 @@ impl Judge { pub async fn run(&self, input: &[u8]) -> io::Result { let checker_language = self .checker_language - .ok_or(io::Error::other("Missing checker")) - .unwrap(); + .ok_or(io::Error::other("Missing checker"))?; let mut checker = checker_language .get_run_command(CHECKER) .current_dir(&self.project_path) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::null()) - .spawn() - .unwrap(); + .spawn()?; let mut cstdin = checker.stdin.take().unwrap(); let mut cstdout = checker.stdout.take().unwrap(); - cstdin.write_all(input).await.unwrap(); - cstdin.write_all(b"\n").await.unwrap(); - cstdin.flush().await.unwrap(); + cstdin.write_all(input).await?; + cstdin.write_all(b"\n").await?; + cstdin.flush().await?; - let sandbox = Sandbox::new(self.resource, self.time_limit, self.idle_time_limit).unwrap(); + let sandbox = Sandbox::new(self.resource, self.time_limit, self.idle_time_limit)?; let mut cmd = self.language.get_run_command(MAIN); cmd.current_dir(&self.project_path) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()); - let mut main = sandbox.spawn(cmd).unwrap(); + let mut main = sandbox.spawn(cmd)?; let mut stdin = main.stdin.take().unwrap(); let mut stdout = main.stdout.take().unwrap(); let mut stderr = main.stderr.take().unwrap(); let monitor = tokio::spawn(async move { sandbox.monitor(main).await }); if !self.is_interactive { - stdin.write_all(input).await.unwrap(); - stdin.write_all(b"\n").await.unwrap(); - stdin.flush().await.unwrap(); + stdin.write_all(input).await?; + stdin.write_all(b"\n").await?; + stdin.flush().await?; } let stdin_thread = tokio::spawn(async move { tokio::io::copy(&mut cstdout, &mut stdin).await }); @@ -161,28 +158,28 @@ impl Judge { let mut out = vec![]; let mut buffer = [0u8; BUFFER_SIZE]; loop { - let n = stdout.read(&mut buffer).await.unwrap(); + let n = stdout.read(&mut buffer).await?; if n == 0 { break; } if cstdin.write_all(&buffer[..n]).await.is_err() { break; } - cstdin.flush().await.unwrap(); + cstdin.flush().await?; out.extend_from_slice(&buffer[0..n]); } Ok::<_, io::Error>(out) }); - let (verdict, run_time, memory_usage) = monitor.await.unwrap().unwrap(); - let checker_status = checker.wait().await.unwrap(); + let (verdict, run_time, memory_usage) = monitor.await.unwrap()?; + let checker_status = checker.wait().await?; drop(checker); let _ = stdin_thread.await; - let stdout = stdout_thread.await.unwrap().unwrap(); + let stdout = stdout_thread.await.unwrap()?; let mut err = vec![]; - stderr.read_to_end(&mut err).await.unwrap(); + stderr.read_to_end(&mut err).await?; if let Some(verdict) = verdict { return Ok(Metrics { @@ -221,7 +218,7 @@ impl Judge { // running sequentially to enable early exit, saving resources for input in inputs { - let metrics = self.run(input).await.unwrap(); + let metrics = self.run(input).await?; total_run_time += metrics.run_time; total_memory_usage = total_memory_usage .add(metrics.memory_usage) @@ -254,7 +251,7 @@ impl Judge { // running sequentially to enable early exit, saving resources while let Some(input) = inputs.next().await { - let metrics = self.run(input).await.unwrap(); + let metrics = self.run(input).await?; total_run_time += metrics.run_time; total_memory_usage = total_memory_usage .add(metrics.memory_usage) diff --git a/src/sandbox/mod.rs b/src/sandbox/mod.rs index 5b3caf9..b7b8f29 100644 --- a/src/sandbox/mod.rs +++ b/src/sandbox/mod.rs @@ -38,7 +38,7 @@ impl Sandbox { idle_time_limit: Duration, ) -> io::Result { Ok(Sandbox { - cgroup: resource.try_into().unwrap(), + cgroup: resource.try_into()?, cpu_usage_limit: time_limit, wall_time_limit: Duration::max(time_limit * 2, time_limit + Duration::from_secs(2)), idle_time_limit, @@ -55,18 +55,15 @@ impl Sandbox { }; self.cgroup .add_task_by_tgid(CgroupPid::from(id as u64)) - .map_err(io::Error::other) - .unwrap(); + .map_err(io::Error::other)?; let cpu: &CpuController = self .cgroup .controller_of() - .ok_or(io::Error::other("Missing cpu controller")) - .unwrap(); + .ok_or(io::Error::other("Missing cpu controller"))?; let memory: &MemController = self .cgroup .controller_of() - .ok_or(io::Error::other("Missing memory controller")) - .unwrap(); + .ok_or(io::Error::other("Missing memory controller"))?; let start = Instant::now(); let mut memory_usage = Byte::default(); @@ -75,7 +72,7 @@ impl Sandbox { let mut interval = interval(POLL); - while child.try_wait().unwrap().is_none() { + while child.try_wait()?.is_none() { let cpu_usage = cpu.usage(); memory_usage = memory_usage.max(memory.usage()); @@ -109,7 +106,7 @@ impl Sandbox { interval.tick().await; } - let status = child.try_wait().unwrap().unwrap(); + let status = child.try_wait()?.unwrap(); if status.success() { return Ok((None, prev_cpu_usage, memory_usage)); } diff --git a/src/sandbox/resource.rs b/src/sandbox/resource.rs index 9ee2eff..e90c83d 100644 --- a/src/sandbox/resource.rs +++ b/src/sandbox/resource.rs @@ -43,8 +43,7 @@ impl TryFrom for Cgroup { let cgroup = builder .build(hierarchies::auto()) - .map_err(io::Error::other) - .unwrap(); + .map_err(io::Error::other)?; Ok(cgroup) } }