diff --git a/src/lib/globals/process.rs b/src/lib/globals/process.rs index 9c901e8..c7b88b1 100644 --- a/src/lib/globals/process.rs +++ b/src/lib/globals/process.rs @@ -4,11 +4,12 @@ use std::{ path::PathBuf, process::{Command, Stdio}, sync::Weak, + time::Duration, }; use mlua::prelude::*; use os_str_bytes::RawOsString; -use smol::channel::Sender; +use smol::{channel::Sender, Timer}; use crate::{ utils::{process::pipe_and_inherit_child_process_stdio, table::TableBuilder}, @@ -120,10 +121,15 @@ async fn process_exit(lua: &Lua, exit_code: Option) -> LuaResult<()> { .unwrap() .upgrade() .unwrap(); + // Send an exit signal to the main thread, which + // will try to exit safely and as soon as possible sender .send(LuneMessage::Exit(exit_code.unwrap_or(0))) .await .map_err(LuaError::external)?; + // Make sure to block the rest of this thread indefinitely since + // the main thread may not register the exit signal right away + Timer::after(Duration::MAX).await; Ok(()) } diff --git a/src/tests/process/exit.luau b/src/tests/process/exit.luau index f55d04e..d83892f 100644 --- a/src/tests/process/exit.luau +++ b/src/tests/process/exit.luau @@ -1,3 +1,14 @@ -process.exit(0) +local function assert(condition, err) + if not condition then + console.error(err) + process.exit(0) + end +end -error("Process should have exited successfully") +local path = process.cwd .. "asdfghjkl" + +assert(fs.isDir(path), "Process should exit") +assert(fs.isDir(path), "Process should exit") +assert(fs.isDir(path), "Process should exit") + +error("Process should have exited...")