From 890134085945f575605f6c34b76a507a832aa4f9 Mon Sep 17 00:00:00 2001 From: Zoe <62722391+juls0730@users.noreply.github.com> Date: Tue, 20 May 2025 14:01:06 +0000 Subject: [PATCH] use stderr instead of a control socket with pluginHost --- main.go | 35 ++++++++--------------------------- pluginHost/main.go | 5 ++++- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/main.go b/main.go index 65f2a04..3f79f75 100644 --- a/main.go +++ b/main.go @@ -273,43 +273,24 @@ func (gloom *GLoom) RegisterPlugin(pluginPath string, name string, domains []str pathStr := strconv.FormatUint(uint64(rand.Uint64()), 16) socketPath := path.Join(gloom.tmpDir, pathStr+".sock") - controlPath := path.Join(gloom.tmpDir, pathStr+"-control.sock") - slog.Debug("Starting pluginHost", "pluginPath", pluginPath, "socketPath", socketPath, "controlPath", controlPath) + slog.Debug("Starting pluginHost", "pluginPath", pluginPath) processPath := path.Join(gloom.gloomDir, "pluginHost") - args := []string{pluginPath, socketPath, controlPath} + args := []string{pluginPath, socketPath} cmd := exec.Command(processPath, args...) + stderrPipe, err := cmd.StderrPipe() + if err != nil { + return fmt.Errorf("failed to get stderr pipe: %w", err) + } + if err := cmd.Start(); err != nil { return fmt.Errorf("failed to start pluginHost: %w", err) } process := cmd.Process - timeout := time.After(5 * time.Second) - - for { - select { - case <-timeout: - _ = process.Signal(os.Interrupt) - return fmt.Errorf("timed out waiting for pluginHost to start (this is likely a GLoom bug)") - default: - } - _, err := os.Stat(controlPath) - if err == nil { - break - } - time.Sleep(100 * time.Millisecond) - } - - conn, err := net.DialTimeout("unix", controlPath, 5*time.Second) - if err != nil { - _ = process.Signal(os.Interrupt) - return fmt.Errorf("failed to connect to plugin control socket: %w", err) - } - defer conn.Close() - - reader := bufio.NewReader(conn) + reader := bufio.NewReader(stderrPipe) readTimeout := time.After(30 * time.Second) select { diff --git a/pluginHost/main.go b/pluginHost/main.go index aad7cc6..63d4239 100644 --- a/pluginHost/main.go +++ b/pluginHost/main.go @@ -19,12 +19,15 @@ var controlPath string func init() { if len(os.Args) < 3 { - fmt.Fprintf(os.Stderr, "Usage: pluginHost ") + fmt.Fprintf(os.Stderr, "Usage: pluginHost [controlPath]") os.Exit(1) } pluginPath = os.Args[1] socketPath = os.Args[2] + // Idk why I originally wrote this solution when stderr is literally just the best solution for me, but this + // makes the pluginHost more generally useful outside of GLoom, so I'm keeping it + // TODO: maybe make it a compiler flag, though I'm sure its not making the binary *that* much bigger if len(os.Args) > 3 { controlPath = os.Args[3] }