Skip to content

Commit

Permalink
fix(service/serve): use AttachConsole for sending CTRL_C event
Browse files Browse the repository at this point in the history
  • Loading branch information
rupansh committed Jul 15, 2024
1 parent 31b08ad commit 2c20384
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions src/service/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ impl ServerProcess {

#[cfg(windows)]
unsafe {
use windows::Win32::System::Console::GenerateConsoleCtrlEvent;
use windows::Win32::System::Console::CTRL_C_EVENT;
use windows::Win32::System::Console::{
AttachConsole, GenerateConsoleCtrlEvent, CTRL_C_EVENT,
};

GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid)?;
AttachConsole(pid)?;
GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)?;
}

proc.wait().await?;
Expand Down Expand Up @@ -139,24 +141,6 @@ impl ServerProcess {
Ok(())
}

fn prepare_command<'a>(&self, command: &'a mut Command) -> &'a mut Command {
#[cfg(windows)]
{
use windows::Win32::System::Threading::CREATE_NEW_PROCESS_GROUP;

let flag = if self.graceful_exit {
CREATE_NEW_PROCESS_GROUP.0
} else {
0
};
command.creation_flags(flag)
}
#[cfg(not(windows))]
{
command
}
}

async fn start(&mut self) -> Result<()> {
let bin = &self.binary;
let child = if bin.exists() {
Expand Down Expand Up @@ -193,7 +177,7 @@ impl ServerProcess {

log::debug!("Serve running {}", GRAY.paint(bin_path.as_str()));
let mut cmd = Command::new(bin_path);
let cmd = self.prepare_command(cmd.envs(self.envs.clone()).args(bin_args));
let cmd = cmd.envs(self.envs.clone()).args(bin_args);

let child = Some(cmd.spawn()?);
let port = self
Expand Down

0 comments on commit 2c20384

Please sign in to comment.