From e134120afd6e1a9108a73e27ce6232d8919f99ab Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Tue, 25 Jun 2024 11:52:59 +0530 Subject: [PATCH] feat: add tests and default exit code for kill() on ChildProcess --- crates/lune-std-process/src/lib.rs | 8 +++----- tests/process/create/kill.luau | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 tests/process/create/kill.luau diff --git a/crates/lune-std-process/src/lib.rs b/crates/lune-std-process/src/lib.rs index f546d47..b66bc0d 100644 --- a/crates/lune-std-process/src/lib.rs +++ b/crates/lune-std-process/src/lib.rs @@ -234,11 +234,9 @@ fn process_create( .with_async_function("status", move |lua, ()| { let code_rx_rc_clone = Rc::clone(&code_rx_rc); async move { - let code = code_rx_rc_clone - .borrow_mut() - .recv() - .await - .expect("Code sender unexpectedly dropped"); + // Exit code of 9 corresponds to SIGKILL, which should be the only case where + // the receiver gets suddenly dropped + let code = code_rx_rc_clone.borrow_mut().recv().await.unwrap_or(9); TableBuilder::new(lua)? .with_value("code", code)? diff --git a/tests/process/create/kill.luau b/tests/process/create/kill.luau new file mode 100644 index 0000000..e0cbbc7 --- /dev/null +++ b/tests/process/create/kill.luau @@ -0,0 +1,21 @@ +local process = require("@lune/process") + +-- Killing a child process should work as expected + +local message = "Hello, world!" +local child = process.create("cat") + +child.stdin:write(message) +child.kill() + +assert(child.status().code == 9, "Child process should have an exit code of 9 (SIGKILL)") + +assert( + child.stdout:readToEnd() == message, + "Reading from stdout of child process should work even after kill" +) + +local stdinWriteOk = pcall(function() + child.stdin:write(message) +end) +assert(not stdinWriteOk, "Writing to stdin of child process should not work after kill")