Skip to content
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
10 changes: 10 additions & 0 deletions internal/provider/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package provider

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"time"

"github.com/suprbdev/noty/internal/render"
"gopkg.in/yaml.v3"
Expand All @@ -24,6 +26,10 @@ type execConfig struct {
StdinBody *bool `yaml:"stdin_body"`
}

// execWaitDelay bounds how long Send waits for output pipes after the
// command exits or is killed; background grandchildren can hold them open.
const execWaitDelay = time.Second

type execSender struct {
cfg execConfig
r *render.Renderer
Expand Down Expand Up @@ -68,6 +74,7 @@ func (e *execSender) Send(ctx context.Context, msg Message) error {
return fmt.Errorf("exec: %w", err)
}
cmd := exec.CommandContext(ctx, e.cfg.Command, args...)
cmd.WaitDelay = execWaitDelay
cmd.Env = os.Environ()
for k, v := range e.cfg.Env {
rv, err := e.r.RenderString("env."+k, v, msg.Data)
Expand All @@ -80,6 +87,9 @@ func (e *execSender) Send(ctx context.Context, msg Message) error {
cmd.Stdin = strings.NewReader(msg.Body)
}
out, err := cmd.CombinedOutput()
if errors.Is(err, exec.ErrWaitDelay) && cmd.ProcessState != nil && cmd.ProcessState.Success() {
err = nil
}
if err != nil {
return fmt.Errorf("exec %s: %w: %s", e.cfg.Command, err, truncate(string(out), 300))
}
Expand Down
35 changes: 35 additions & 0 deletions internal/provider/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/suprbdev/noty/internal/render"
)
Expand Down Expand Up @@ -99,3 +100,37 @@ args: ["{{ .Subject }}"]
t.Errorf("Describe should show rendered command line: %q", out)
}
}

func TestExecTimeoutWithLingeringChild(t *testing.T) {
s, err := New("exec", chanNode(t, `type: exec
command: /bin/sh
args: ["-c", "sleep 10; echo done"]`), &render.Renderer{})
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
start := time.Now()
if err := s.Send(ctx, testMsg("x")); err == nil {
t.Error("expected timeout error")
}
if d := time.Since(start); d > 5*time.Second {
t.Errorf("Send took %v; timeout not enforced", d)
}
}

func TestExecBackgroundChildIsSuccess(t *testing.T) {
s, err := New("exec", chanNode(t, `type: exec
command: /bin/sh
args: ["-c", "sleep 10 & echo started"]`), &render.Renderer{})
if err != nil {
t.Fatal(err)
}
start := time.Now()
if err := s.Send(context.Background(), testMsg("x")); err != nil {
t.Errorf("command exited 0; lingering background child must not fail the send: %v", err)
}
if d := time.Since(start); d > 5*time.Second {
t.Errorf("Send took %v waiting on a background child", d)
}
}
Loading