Skip to content

armgit / muxin

Public Imported from GitHub
0

All your machines, one app. For you and your AI agents: agent sessions, desktops, files, logs and GitHub across your Macs, Linux and Windows machines, with an MCP server.

Windows runner stop: ask the listener with Ctrl+C before killing it (#29)

Adib Hannab69372a092b9
diff --git a/src/ops/mod.rs b/src/ops/mod.rs
index 2a70311..1c6cc07 100644
--- a/src/ops/mod.rs
+++ b/src/ops/mod.rs
@@ -1200,6 +1200,18 @@ Match host x
}
}
+ #[test]
+ fn windows_stop_asks_the_runner_before_killing_it() {
+ // Killed outright, the listener leaves its session on GitHub for minutes
+ // and a runner started meanwhile can't connect. Ctrl+C in its console
+ // (what the runner's own service sends) makes it delete the session first.
+ let stop = &CONTROL_PS1[CONTROL_PS1.find("function Send-CtrlC").unwrap()..CONTROL_PS1.find("switch ($OR_ACTION)").unwrap()];
+ let ctrl_c = stop.find("$sent = Send-CtrlC").unwrap();
+ let kill = stop.find("foreach ($p in $left) { Stop-Process").unwrap();
+ assert!(stop.contains("GenerateConsoleCtrlEvent(0, 0)"));
+ assert!(ctrl_c < kill, "{stop}");
+ }
+
#[test]
fn fallback_release_has_a_checksum_for_every_package() {
let release = fallback_release();
diff --git a/src/ops/scripts/control.ps1 b/src/ops/scripts/control.ps1
index fff1ff6..77002a1 100644
--- a/src/ops/scripts/control.ps1
+++ b/src/ops/scripts/control.ps1
@@ -57,6 +57,37 @@ function Start-Runner {
}
}
+# Asks the listener to stop the way the runner's own service does: Ctrl+C in its
+# console. It then deletes its session on GitHub and exits. Killed outright, it
+# leaves that session behind for a few minutes, and a runner started in that time
+# can't connect ("A session for this runner already exists"). Sending it takes a
+# process of its own, one that can leave this console for the listener's and
+# ignore the Ctrl+C it sends there. Says whether it was sent.
+function Send-CtrlC($id) {
+ # Its progress (Add-Type loading a module) would reach this script's output as CLIXML.
+ $helper = @'
+$ProgressPreference = 'SilentlyContinue'
+$ErrorActionPreference = 'Stop'
+try {
+ Add-Type -Namespace Muxin -Name Console -MemberDefinition '
+ [DllImport("kernel32.dll")] public static extern bool FreeConsole();
+ [DllImport("kernel32.dll")] public static extern bool AttachConsole(uint id);
+ [DllImport("kernel32.dll")] public static extern bool SetConsoleCtrlHandler(IntPtr handler, bool add);
+ [DllImport("kernel32.dll")] public static extern bool GenerateConsoleCtrlEvent(uint ctrlEvent, uint group);'
+ [void][Muxin.Console]::FreeConsole()
+ if (-not [Muxin.Console]::AttachConsole($id)) { exit 2 }
+ [void][Muxin.Console]::SetConsoleCtrlHandler([IntPtr]::Zero, $true)
+ if (-not [Muxin.Console]::GenerateConsoleCtrlEvent(0, 0)) { exit 3 }
+ exit 0
+} catch { exit 1 }
+'@
+ $encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("`$id = $id`n$helper"))
+ # Whatever goes wrong there ends in the forced stop, not in this script failing.
+ $ErrorActionPreference = 'Continue'
+ & powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -EncodedCommand $encoded *> $null
+ $LASTEXITCODE -eq 0
+}
+
function Stop-Runner {
if ($svcName) {
say "Stopping service ${svcName}…"
@@ -65,7 +96,25 @@ function Stop-Runner {
}
$procs = @(Get-RunnerProcesses)
if ($procs.Count -eq 0) { say 'Not running.'; return }
- foreach ($p in $procs) { Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue }
+ $listener = $procs | Where-Object { $_.Name -eq 'Runner.Listener.exe' } | Select-Object -First 1
+ $sent = $false
+ if ($listener) {
+ # The cmd.exe running run.cmd would get the Ctrl+C too, and then wait on
+ # "Terminate batch job (Y/N)?" in a window no one sees. The listener outlives it.
+ $cmd = Get-CimInstance Win32_Process -Filter "ProcessId=$($listener.ParentProcessId)"
+ if ($cmd -and $cmd.Name -eq 'cmd.exe' -and $cmd.CommandLine -match '/c .*run\.cmd') {
+ Stop-Process -Id $cmd.ProcessId -Force -ErrorAction SilentlyContinue
+ }
+ $sent = Send-CtrlC $listener.ProcessId
+ }
+ # 30 seconds, as the service gives it: a running job is cancelled first.
+ if ($sent) { Wait-Process -Id $listener.ProcessId -Timeout 30 -ErrorAction SilentlyContinue }
+ $left = @(Get-RunnerProcesses)
+ if ($left.Count -gt 0) {
+ if ($sent) { say 'Still running after 30 seconds; ending it.' }
+ elseif ($listener) { say "Couldn't send it Ctrl+C; ending it." }
+ foreach ($p in $left) { Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue }
+ }
say 'Stopped.'
}