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
25 changes: 11 additions & 14 deletions tailcat_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net"
"os"
"os/exec"
"sync"
)

// ExecConnHandler returns a handler that runs argv for each incoming
Expand Down Expand Up @@ -87,27 +86,25 @@ func runConnCommand(c net.Conn, cmd *exec.Cmd) error {
c.Close()
return err
}
var wg sync.WaitGroup
wg.Add(2)
stdinDone := make(chan struct{})
go func() {
defer wg.Done()
defer close(stdinDone)
io.Copy(stdin, c)
stdin.Close()
}()
go func() {
defer wg.Done()
io.Copy(c, stdout)
if cw, ok := c.(closeWriter); ok {
cw.CloseWrite()
}
}()
// Wait closes the stdout pipe once the process exits, so the copy
// out of it must finish first or a fast-exiting command's output
// can be lost. The stdin copy finishes on its own: once the process
// is gone, writes to its stdin fail.
wg.Wait()
// can be lost.
io.Copy(c, stdout)
if cw, ok := c.(closeWriter); ok {
cw.CloseWrite()
}
err = cmd.Wait()
// The stdin copy may still be blocked reading from a client that
// hasn't sent anything since the command stopped reading and hasn't
// half-closed either. Closing the connection unblocks it.
closeProxyConn(c)
<-stdinDone
if err != nil {
return fmt.Errorf("%v exited: %w", cmd.Path, err)
}
Expand Down
41 changes: 41 additions & 0 deletions tailcat_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ package tailcat

import (
"context"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -65,3 +68,41 @@ func TestExecConnHandlerResetBeforeServed(t *testing.T) {
t.Error("the command ran for a connection that was already reset")
}
}

// TestRunConnCommandInteractive covers a command that reads a line from
// the connection, replies, and exits while the client keeps its sending
// side open. The client's input must reach the command before the
// command exits, and runConnCommand must return once it has exited
// rather than waiting for the client to half-close.
func TestRunConnCommandInteractive(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("needs /bin/sh")
}
client, server := net.Pipe()
defer client.Close()
done := make(chan error, 1)
go func() {
done <- runConnCommand(server, exec.Command("/bin/sh", "-c", "read line; echo got $line"))
}()

client.SetDeadline(time.Now().Add(10 * time.Second))
if _, err := io.WriteString(client, "hi\n"); err != nil {
t.Fatalf("write: %v", err)
}
got, err := io.ReadAll(client)
if err != nil {
t.Fatalf("read: %v", err)
}
if string(got) != "got hi\n" {
t.Errorf("got %q; want %q", got, "got hi\n")
}

select {
case err := <-done:
if err != nil {
t.Fatalf("runConnCommand: %v", err)
}
case <-time.After(10 * time.Second):
t.Fatal("runConnCommand didn't return after the command exited")
}
}
Loading