Commit a9e9d52

Eric Bower  ·  2026-08-09 10:47:31 -0400 EDT
parent 234c991
fix: proper job cancellation
2 files changed,  +26, -4
+23, -4
......@@ -829,9 +829,9 @@ func waitAndReport(cfg *Cfg, log *slog.Logger, name, jobID, eventType string) er
829829 ticker := time.NewTicker(interval)
830830 defer ticker.Stop()
831831
832- // Handle ^C gracefully
832+ // Handle ^C and termination signals gracefully
833833 sigCh := make(chan os.Signal, 1)
834- signal.Notify(sigCh, syscall.SIGINT)
834+ signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
835835 defer signal.Stop(sigCh)
836836
837837 fmt.Fprintln(os.Stdout) //nolint:errcheck
......@@ -855,8 +855,9 @@ func waitAndReport(cfg *Cfg, log *slog.Logger, name, jobID, eventType string) er
855855 fmt.Fprintln(os.Stdout, "\n⏹ job cancelled") //nolint:errcheck
856856 return cfg.Ctx.Err()
857857 case <-sigCh:
858- fmt.Fprintln(os.Stdout, "\n⏹ cancelled") //nolint:errcheck
859- return nil
858+ cancelJobSessions(prefix)
859+ fmt.Fprintln(os.Stdout, "\n⏹ job cancelled") //nolint:errcheck
860+ return fmt.Errorf("job cancelled by signal")
860861 case <-ticker.C:
861862 }
862863
......@@ -1842,6 +1843,24 @@ func killSessions(names []string) error {
18421843 return nil
18431844 }
18441845
1846+// cancelJobSessions finds and kills all active zmx sessions with the given prefix.
1847+func cancelJobSessions(prefix string) {
1848+ listOutput, err := exec.Command("zmx", "list").CombinedOutput()
1849+ if err != nil {
1850+ return
1851+ }
1852+ sessions := parseZMXList(string(listOutput))
1853+ var toKill []string
1854+ for _, s := range sessions {
1855+ if strings.HasPrefix(s.Name, prefix) && s.Ended == "" {
1856+ toKill = append(toKill, s.Name)
1857+ }
1858+ }
1859+ if len(toKill) > 0 {
1860+ _ = killSessions(toKill)
1861+ }
1862+}
1863+
18451864 // runGC kills all ci. zmx sessions older than 3 hours, regardless of status.
18461865 func runGC(cfg *Cfg) error {
18471866 log := cfg.Logger.With("cmd", "gc")
+3, -0
......@@ -285,6 +285,9 @@ zmx run fast-step echo "done"
285285 artifactDir := t.TempDir()
286286 ctx, cancel := context.WithCancel(context.Background())
287287 defer cancel()
288+ t.Cleanup(func() {
289+ _ = exec.Command("zmx", "kill", "-f", "local.dup-repo").Run()
290+ })
288291
289292 makeCfg := func(eventSource io.ReadCloser) *Cfg {
290293 return &Cfg{