Skip to content
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

fix: prevent stdout from disappearing in script templates. Fixes #11330 #11368

Merged
merged 1 commit into from
Aug 10, 2023
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
9 changes: 9 additions & 0 deletions cmd/argoexec/commands/emissary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,21 @@ func TestEmissary(t *testing.T) {
assert.Equal(t, "1", string(data))
})
t.Run("Stdout", func(t *testing.T) {
_ = os.Remove(varRunArgo + "/ctr/main/stdout")
err := run("echo hello")
assert.NoError(t, err)
data, err := os.ReadFile(varRunArgo + "/ctr/main/stdout")
assert.NoError(t, err)
assert.Contains(t, string(data), "hello")
})
t.Run("Sub-process", func(t *testing.T) {
_ = os.Remove(varRunArgo + "/ctr/main/stdout")
err := run("(sleep 60; echo 'should not wait for sub-process')& echo -n hello")
assert.NoError(t, err)
data, err := os.ReadFile(varRunArgo + "/ctr/main/stdout")
assert.NoError(t, err)
assert.Equal(t, "hello", string(data))
})
t.Run("Combined", func(t *testing.T) {
err := run("echo hello > /dev/stderr")
assert.NoError(t, err)
Expand Down
8 changes: 7 additions & 1 deletion workflow/executor/os-specific/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"os"
"os/exec"
"time"

log "github.com/sirupsen/logrus"
"golang.org/x/term"
Expand All @@ -16,7 +17,12 @@ func simpleStart(cmd *exec.Cmd) (func(), error) {
return nil, err
}

return func() {}, nil
closer := func() {
cmd.WaitDelay = 100 * time.Millisecond
_ = cmd.Wait()
}

return closer, nil
}

func isTerminal(stdin io.Reader) bool {
Expand Down
47 changes: 47 additions & 0 deletions workflow/executor/os-specific/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package os_specific

import (
"bytes"
"io"
"os/exec"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestSimpleStartCloser(t *testing.T) {
cmd := exec.Command("sh", "-c", "echo -n A123456789B123456789C123456789D123456789E123456789")
var stdoutWriter bytes.Buffer
slowWriter := SlowWriter{
&stdoutWriter,
}
// Command outputs are asynchronously written to cmd.Stdout.
// Using SlowWriter causes the situation where the invoked command has exited but its outputs have not been written yet.
cmd.Stdout = slowWriter

closer, err := StartCommand(cmd)
assert.NoError(t, err)
err = cmd.Process.Release()
assert.NoError(t, err)
// Wait for echo command to exit before calling closer
time.Sleep(100 * time.Millisecond)
closer()

assert.Equal(t, "A123456789B123456789C123456789D123456789E123456789", stdoutWriter.String())
}

type SlowWriter struct {
Writer io.Writer
}

func (s SlowWriter) Write(data []byte) (n int, err error) {
for i := range data {
_, err := s.Writer.Write(data[i : i+1])
if err != nil {
return i, err
}
time.Sleep(7 * time.Millisecond)
}
return len(data), nil
}