Python
fix: reconcile Windows supervisor exit after job object closes
Rohit P0a082d708187
diff --git a/.github/workflows/rust-sdk.yml b/.github/workflows/rust-sdk.ymlindex 7bea983e..2cc84570 100644--- a/.github/workflows/rust-sdk.yml+++ b/.github/workflows/rust-sdk.yml@@ -63,7 +63,8 @@ jobs:run: |rustup show active-toolchaincargo test --locked -p openworker-sdk --features test-support --lib --test desktop_api --test agent_team -- --test-threads=1- cargo test --locked -p openworker-runtime --test connections --test model_endpoints --test direct_macos --test coding --test coding_extended --test assistant+ cargo test --locked -p openworker-runtime --lib direct::windows::tests -- --test-threads=1+ cargo test --locked -p openworker-runtime --test windows --test connections --test model_endpoints --test direct_macos --test coding --test coding_extended --test assistantcargo test --locked -p openworker-sdk --features test-support --test coding --test assistant -- --test-threads=1- name: Build TypeScript SDK and test transporttimeout-minutes: 5diff --git a/harness/runtime/src/direct_windows.rs b/harness/runtime/src/direct_windows.rsindex e3d335cd..38f23350 100644--- a/harness/runtime/src/direct_windows.rs+++ b/harness/runtime/src/direct_windows.rs@@ -27,6 +27,9 @@ pub fn identity(pid: u32, token: &str) -> Result<Identity> {})}pub async fn cleanup(id: &Identity) -> Result<()> {+ cleanup_with_timeout(id, Duration::from_secs(10)).await+}+async fn cleanup_with_timeout(id: &Identity, timeout: Duration) -> Result<()> {let p = match process(id.pid) {Ok(p) => p,Err(e)@@ -44,11 +47,32 @@ pub async fn cleanup(id: &Identity) -> Result<()> {bail!("Wrong platform process identity");}let name = job_name(&id.token)?;- let job = Job(Handle::new(unsafe {+ let job = Handle::new(unsafe {OpenJobObjectW(JOB_OBJECT_TERMINATE | JOB_OBJECT_QUERY, 0, name.as_ptr())- })?);+ });+ let job = match job {+ Ok(handle) => Job(handle),+ Err(error)+ if error+ .downcast_ref::<std::io::Error>()+ .is_some_and(|e| e.raw_os_error() == Some(ERROR_FILE_NOT_FOUND as i32)) =>+ {+ // EOF can make the supervisor drop its kill-on-close job before+ // its process is signaled. A missing name alone is NOT proof of+ // cleanup: wait on the original, creation-time-verified handle.+ return tokio::time::timeout(timeout, async {+ while !p.exited()? {+ tokio::time::sleep(Duration::from_millis(10)).await;+ }+ Ok::<_, anyhow::Error>(())+ })+ .await+ .context("Windows supervisor exit pending after job disappeared")?;+ }+ Err(error) => return Err(error),+ };job.terminate()?;- tokio::time::timeout(Duration::from_secs(10), async {+ tokio::time::timeout(timeout, async {while job.active()? != 0 || !p.exited()? {tokio::time::sleep(Duration::from_millis(10)).await;}@@ -113,3 +137,67 @@ pub fn supervise(owner: u32, workspace: &Path, command: &[String], token: &str)}Ok(0)}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn exit_fixture() {+ let Ok(token) = std::env::var("OW_TEST_CLOSED_JOB_TOKEN") else {+ return;+ };+ // Deterministically hold the gap between dropping the named job and+ // exiting, instead of relying on a scheduler race in the regression.+ drop(Job::new(Some(&token)).unwrap());+ println!("JOB_CLOSED");+ std::io::stdout().flush().unwrap();+ let mut input = String::new();+ std::io::stdin().read_line(&mut input).unwrap();+ }++ #[tokio::test]+ async fn missing_job_requires_verified_supervisor_exit() {+ use tokio::io::{AsyncBufReadExt, BufReader};+ let token = crate::id("supervisor");+ let mut child = tokio::process::Command::new(std::env::current_exe().unwrap())+ .args([+ "--exact",+ "direct::windows::tests::exit_fixture",+ "--nocapture",+ ])+ .env("OW_TEST_CLOSED_JOB_TOKEN", &token)+ .stdin(std::process::Stdio::piped())+ .stdout(std::process::Stdio::piped())+ .kill_on_drop(true)+ .spawn()+ .unwrap();+ let input = child.stdin.take().unwrap();+ let mut lines = BufReader::new(child.stdout.take().unwrap()).lines();+ tokio::time::timeout(Duration::from_secs(5), async {+ while !lines+ .next_line()+ .await+ .unwrap()+ .unwrap()+ .contains("JOB_CLOSED")+ {}+ })+ .await+ .unwrap();+ let id = identity(child.id().unwrap(), &token).unwrap();+ let owned_process = process(id.pid).unwrap();+ assert!(cleanup_with_timeout(&id, Duration::from_millis(30))+ .await+ .is_err());+ assert!(!owned_process.exited().unwrap());+ let release = tokio::spawn(async move {+ tokio::time::sleep(Duration::from_millis(30)).await;+ drop(input);+ });+ cleanup(&id).await.unwrap();+ assert!(owned_process.exited().unwrap());+ assert!(child.wait().await.unwrap().success());+ release.await.unwrap();+ }+}diff --git a/harness/runtime/tests/native_tools.rs b/harness/runtime/tests/native_tools.rsindex 79e31044..b8e7a710 100644--- a/harness/runtime/tests/native_tools.rs+++ b/harness/runtime/tests/native_tools.rs@@ -92,7 +92,6 @@ fn registry_requires_exact_names_and_rejects_ambiguity() {json!(["unknown.probe"]),json!(["partner.probe", "partner.probe"]),json!(["probe"]),- json!([]),] {bundle(tmp.path(), names);assert!(Bundle::load_with_registry(tmp.path(), &r).is_err());@@ -113,6 +112,19 @@ fn registry_requires_exact_names_and_rejects_ambiguity() {assert!(Bundle::load_with_registry(tmp.path(), &r).is_err());}#[test]+fn empty_tool_selection_does_not_inherit_registered_tools() {+ let tmp = tempfile::tempdir().unwrap();+ bundle(tmp.path(), json!([]));+ let r = registry("1", Default::default());+ let b = Bundle::load_with_registry(tmp.path(), &r).unwrap();+ assert!(b.tools.bindings.is_empty());+ assert!(+ openworker_runtime::bundle::bound_tools(&b, &Default::default())+ .unwrap()+ .is_empty()+ );+}+#[test]fn bundle_selects_only_named_tools_and_pins_registry_without_an_acl() {let tmp = tempfile::tempdir().unwrap();bundle(tmp.path(), json!(["partner.probe"]));diff --git a/harness/runtime/tests/protocol.rs b/harness/runtime/tests/protocol.rsindex 73ee76fe..f4571307 100644--- a/harness/runtime/tests/protocol.rs+++ b/harness/runtime/tests/protocol.rs@@ -22,7 +22,14 @@ async fn stdio_initialization_validation_snapshot_and_eof() {runner_fixture(&runner);let bundle = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples/assistant");let mut child = tokio::process::Command::new(env!("CARGO_BIN_EXE_openworker"))- .args(["serve", "--stdio", "--scripted", "--state-dir"])+ .args([+ "serve",+ "--stdio",+ "--scripted",+ "--sandbox",+ "off",+ "--state-dir",+ ]).arg(&state).arg("--bundle").arg(bundle)@@ -102,7 +109,14 @@ async fn oversized_stdio_frame_fails_closed() {runner_fixture(&runner);let bundle = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples/assistant");let mut child = tokio::process::Command::new(env!("CARGO_BIN_EXE_openworker"))- .args(["serve", "--stdio", "--scripted", "--state-dir"])+ .args([+ "serve",+ "--stdio",+ "--scripted",+ "--sandbox",+ "off",+ "--state-dir",+ ]).arg(root.path().join("state")).arg("--bundle").arg(bundle)@@ -143,7 +157,14 @@ async fn sigint_exits_with_stdin_still_open() {runner_fixture(&runner);let bundle = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples/assistant");let mut child = tokio::process::Command::new(env!("CARGO_BIN_EXE_openworker"))- .args(["serve", "--stdio", "--scripted", "--state-dir"])+ .args([+ "serve",+ "--stdio",+ "--scripted",+ "--sandbox",+ "off",+ "--state-dir",+ ]).arg(root.path().join("state")).arg("--bundle").arg(bundle)