Skip to content

Commit e669ba7

Browse files
Merge pull request #220 from ibuildthecloud/ls
bug: try not to orphan daemon processes
2 parents 4074644 + 0914611 commit e669ba7

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package main
22

33
import (
4+
"os"
5+
46
"github.com/acorn-io/cmd"
57
"github.com/gptscript-ai/gptscript/pkg/cli"
8+
"github.com/gptscript-ai/gptscript/pkg/daemon"
9+
"github.com/gptscript-ai/gptscript/pkg/mvl"
610

711
// Load all VCS
812
_ "github.com/gptscript-ai/gptscript/pkg/loader/vcs"
913
)
1014

15+
var log = mvl.Package()
16+
1117
func main() {
18+
if len(os.Args) > 2 && os.Args[1] == "sys.daemon" {
19+
if err := daemon.SysDaemon(); err != nil {
20+
log.Fatalf("failed running daemon: %v", err)
21+
}
22+
os.Exit(0)
23+
}
1224
cmd.Main(cli.New())
1325
}

pkg/daemon/daemon.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package daemon
2+
3+
import (
4+
"context"
5+
"io"
6+
"os"
7+
"os/exec"
8+
)
9+
10+
func SysDaemon() error {
11+
ctx, cancel := context.WithCancel(context.Background())
12+
defer cancel()
13+
14+
go func() {
15+
_, _ = io.ReadAll(os.Stdin)
16+
cancel()
17+
}()
18+
19+
cmd := exec.CommandContext(ctx, os.Args[2], os.Args[3:]...)
20+
cmd.Stdin = os.Stdin
21+
cmd.Stderr = os.Stderr
22+
cmd.Stdin = os.Stdin
23+
return cmd.Run()
24+
}

pkg/engine/daemon.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ func (e *Engine) startDaemon(_ context.Context, tool types.Tool) (string, error)
131131
return "", err
132132
}
133133

134+
// Loop back to gptscript to help with process supervision
135+
cmd.Args = append([]string{os.Args[0], "sys.daemon", cmd.Path}, cmd.Args[1:]...)
136+
cmd.Path = self()
137+
134138
cmd.Stdin = r
135139
cmd.Stderr = os.Stderr
136140
cmd.Stdout = os.Stdout

pkg/engine/self.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build !linux
2+
3+
package engine
4+
5+
import (
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
)
10+
11+
// Copied from github.com/moby/moby/pkg/reexec d25b0bd7ea6ce17ca085c54d5965eeeb66417e52
12+
13+
func self() string {
14+
name := os.Args[0]
15+
if filepath.Base(name) == name {
16+
if lp, err := exec.LookPath(name); err == nil {
17+
return lp
18+
}
19+
}
20+
// handle conversion of relative paths to absolute
21+
if absName, err := filepath.Abs(name); err == nil {
22+
return absName
23+
}
24+
// if we couldn't get absolute name, return original
25+
// (NOTE: Go only errors on Abs() if os.Getwd fails)
26+
return name
27+
}

pkg/engine/self_linux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package engine
2+
3+
func self() string {
4+
return "/proc/self/exe"
5+
}

0 commit comments

Comments
 (0)