Skip to content

bug: try not to orphan daemon processes #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package main

import (
"os"

"github.com/acorn-io/cmd"
"github.com/gptscript-ai/gptscript/pkg/cli"
"github.com/gptscript-ai/gptscript/pkg/daemon"
"github.com/gptscript-ai/gptscript/pkg/mvl"

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

var log = mvl.Package()

func main() {
if len(os.Args) > 2 && os.Args[1] == "sys.daemon" {
if err := daemon.SysDaemon(); err != nil {
log.Fatalf("failed running daemon: %v", err)
}
os.Exit(0)
}
cmd.Main(cli.New())
}
24 changes: 24 additions & 0 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package daemon

import (
"context"
"io"
"os"
"os/exec"
)

func SysDaemon() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
_, _ = io.ReadAll(os.Stdin)
cancel()
}()

cmd := exec.CommandContext(ctx, os.Args[2], os.Args[3:]...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}
4 changes: 4 additions & 0 deletions pkg/engine/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (e *Engine) startDaemon(_ context.Context, tool types.Tool) (string, error)
return "", err
}

// Loop back to gptscript to help with process supervision
cmd.Args = append([]string{os.Args[0], "sys.daemon", cmd.Path}, cmd.Args[1:]...)
cmd.Path = self()

cmd.Stdin = r
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Expand Down
27 changes: 27 additions & 0 deletions pkg/engine/self.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build !linux

package engine

import (
"os"
"os/exec"
"path/filepath"
)

// Copied from github.com/moby/moby/pkg/reexec d25b0bd7ea6ce17ca085c54d5965eeeb66417e52

func self() string {
name := os.Args[0]
if filepath.Base(name) == name {
if lp, err := exec.LookPath(name); err == nil {
return lp
}
}
// handle conversion of relative paths to absolute
if absName, err := filepath.Abs(name); err == nil {
return absName
}
// if we couldn't get absolute name, return original
// (NOTE: Go only errors on Abs() if os.Getwd fails)
return name
}
5 changes: 5 additions & 0 deletions pkg/engine/self_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package engine

func self() string {
return "/proc/self/exe"
}