]> Cypherpunks.ru repositories - goredo.git/commitdiff
Kill children v1.17.0
authorSergey Matveev <stargrave@stargrave.org>
Tue, 19 Oct 2021 13:23:50 +0000 (16:23 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 19 Oct 2021 13:23:50 +0000 (16:23 +0300)
doc/news.texi
js.go
main.go
run.go
usage.go

index e98554e57063b033c9a12d4da166873a5d416a73..a28696e2d042743efa124e42b5a9e6dce5577721 100644 (file)
@@ -1,6 +1,14 @@
 @node News
 @unnumbered News
 
+@anchor{Release 1_17_0}
+@section Release 1.17.0
+@itemize
+@item
+    If @command{redo*} process is killed, then it sends @code{SIGTERM}
+    to all his children too, properly terminating the whole job queue.
+@end itemize
+
 @anchor{Release 1_16_0}
 @section Release 1.16.0
 @itemize
diff --git a/js.go b/js.go
index ba5cff134a4dd645f2f81b6b0b6297827360a671..1f2ec3985a463333781e738b291bcc00ab4d5e70 100644 (file)
--- a/js.go
+++ b/js.go
@@ -24,11 +24,9 @@ import (
        "fmt"
        "log"
        "os"
-       "os/signal"
        "regexp"
        "strconv"
        "sync"
-       "syscall"
 )
 
 const (
@@ -170,20 +168,6 @@ func jsInit() {
                jsTokens[jsToken]++
                jsRelease("ifchange entered", jsToken)
        }
-
-       killed := make(chan os.Signal, 0)
-       signal.Notify(killed, syscall.SIGTERM, syscall.SIGINT)
-       go func() {
-               <-killed
-               jsTokensM.Lock()
-               for token, i := range jsTokens {
-                       for ; i > 0; i-- {
-                               jsReleaseNoLock(token)
-                       }
-               }
-               jsTokensM.Unlock()
-               os.Exit(1)
-       }()
 }
 
 func jsReleaseNoLock(token byte) {
@@ -203,6 +187,16 @@ func jsRelease(ctx string, token byte) {
        jsTokensM.Unlock()
 }
 
+func jsReleaseAll() {
+       jsTokensM.Lock()
+       for token, i := range jsTokens {
+               for ; i > 0; i-- {
+                       jsReleaseNoLock(token)
+               }
+       }
+       jsTokensM.Unlock()
+}
+
 func jsAcquire(ctx string) byte {
        if JSR == nil {
                return BMakeGoodToken
diff --git a/main.go b/main.go
index 67c428c14a6b6661bdc9bf94ae4dac0a0cf1062b..c3b1be78677b731291db45ca8af4cfab7b74a5d3 100644 (file)
--- a/main.go
+++ b/main.go
@@ -27,11 +27,13 @@ import (
        "io/ioutil"
        "log"
        "os"
+       "os/signal"
        "path"
        "path/filepath"
        "runtime"
        "sort"
        "strconv"
+       "syscall"
 
        "go.cypherpunks.ru/recfile"
        "golang.org/x/sys/unix"
@@ -282,6 +284,20 @@ func main() {
                }
        }
 
+       killed := make(chan os.Signal, 0)
+       signal.Notify(killed, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
+       go func() {
+               <-killed
+               trace(CDebug, "[%s] killed", BuildUUID)
+               jsReleaseAll()
+               RunningProcsM.Lock()
+               for pid, proc := range RunningProcs {
+                       trace(CDebug, "[%s] killing child %d", BuildUUID, pid)
+                       proc.Signal(syscall.SIGTERM)
+               }
+               os.Exit(1)
+       }()
+
        ok := true
        err = nil
        trace(
diff --git a/run.go b/run.go
index 1e8472f3602b514f9335712028411f48f5c163a0..ca514578fcc9feaa0ee4d5d1110b09a427585d50 100644 (file)
--- a/run.go
+++ b/run.go
@@ -73,6 +73,9 @@ var (
        flagStderrSilent *bool
 
        TracedAll bool
+
+       RunningProcs  = map[int]*os.Process{}
+       RunningProcsM sync.Mutex
 )
 
 func init() {
@@ -546,6 +549,9 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                        errs <- runErr
                        return
                }
+               RunningProcsM.Lock()
+               RunningProcs[cmd.Process.Pid] = cmd.Process
+               RunningProcsM.Unlock()
                pid := fmt.Sprintf("[%d]", cmd.Process.Pid)
                trace(CDebug, "%s runs %s", tgtOrig, pid)
 
@@ -582,6 +588,9 @@ func runScript(tgtOrig string, errs chan error, traced bool) error {
                // Wait for job completion
                <-stderrTerm
                err = cmd.Wait()
+               RunningProcsM.Lock()
+               delete(RunningProcs, cmd.Process.Pid)
+               RunningProcsM.Unlock()
                finished = time.Now()
                runErr.Finished = &finished
                if err != nil {
index d8460b9236ad0eec4bfb252d117133fceae876e5..2cfee2d2e7f372c80ebc5b8959d0abf9bbc09826 100644 (file)
--- a/usage.go
+++ b/usage.go
@@ -24,7 +24,7 @@ import (
 )
 
 const (
-       Version  = "1.16.0"
+       Version  = "1.17.0"
        Warranty = `Copyright (C) 2020-2021 Sergey Matveev
 
 This program is free software: you can redistribute it and/or modify