mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Potential fix for process.spawn yield issue
This commit is contained in:
parent
7eff6e4555
commit
d3046194bc
1 changed files with 23 additions and 25 deletions
|
@ -1,8 +1,12 @@
|
|||
use std::{env, process::Stdio, sync::Weak};
|
||||
use std::{
|
||||
env,
|
||||
process::{Command, Stdio},
|
||||
sync::Weak,
|
||||
};
|
||||
|
||||
use mlua::prelude::*;
|
||||
use os_str_bytes::RawOsString;
|
||||
use smol::{channel::Sender, process::Command, LocalExecutor};
|
||||
use smol::{channel::Sender, unblock};
|
||||
|
||||
use crate::{utils::table::TableBuilder, LuneMessage};
|
||||
|
||||
|
@ -116,30 +120,24 @@ async fn process_spawn(
|
|||
lua: &Lua,
|
||||
(program, args): (String, Option<Vec<String>>),
|
||||
) -> LuaResult<LuaTable> {
|
||||
let exec = lua
|
||||
.app_data_ref::<Weak<LocalExecutor>>()
|
||||
.unwrap()
|
||||
.upgrade()
|
||||
.unwrap();
|
||||
// Create and spawn a child process in a new task to prevent
|
||||
// Create and spawn a **blocking** child process to prevent
|
||||
// issues with yielding across the metamethod/c-call boundary
|
||||
let output = exec
|
||||
.spawn(async move {
|
||||
let pwd = env::current_dir()?;
|
||||
let output = unblock(move || {
|
||||
let mut cmd = Command::new(program);
|
||||
if let Some(args) = args {
|
||||
cmd.args(args);
|
||||
}
|
||||
let child = cmd
|
||||
.current_dir(env::current_dir().map_err(LuaError::external)?)
|
||||
.current_dir(pwd)
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.map_err(LuaError::external)?;
|
||||
let output = child.output().await.map_err(LuaError::external)?;
|
||||
Ok::<_, LuaError>(output)
|
||||
.spawn()?;
|
||||
child.wait_with_output()
|
||||
})
|
||||
.await?;
|
||||
.await
|
||||
.map_err(LuaError::external)?;
|
||||
// NOTE: If an exit code was not given by the child process,
|
||||
// we default to 1 if it yielded any error output, otherwise 0
|
||||
let code = output
|
||||
|
|
Loading…
Reference in a new issue