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

replace io.Copy with a scanner to avoid output related deadlocks on WSL2 #674

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 7 additions & 15 deletions runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,12 @@ func (e *Engine) runCommand(command string) error {
stdout.Close()
stderr.Close()
}()
_, _ = io.Copy(os.Stdout, stdout)
_, _ = io.Copy(os.Stderr, stderr)

copyOutput(os.Stdout, stdout)
copyOutput(os.Stderr, stderr)

// wait for command to finish
err = cmd.Wait()
if err != nil {
return err
}
return nil
return cmd.Wait()
}

// run cmd option in .air.toml
Expand Down Expand Up @@ -551,15 +549,9 @@ func (e *Engine) runBin() error {
go killFunc(cmd, stdout, stderr, killCh, processExit)
})

go func() {
_, _ = io.Copy(os.Stdout, stdout)
_, _ = cmd.Process.Wait()
}()
go copyOutput(os.Stdout, stdout)
go copyOutput(os.Stderr, stderr)

go func() {
_, _ = io.Copy(os.Stderr, stderr)
_, _ = cmd.Process.Wait()
}()
state, _ := cmd.Process.Wait()
close(processExit)
switch state.ExitCode() {
Expand Down
9 changes: 9 additions & 0 deletions runner/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package runner

import (
"bufio"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -206,6 +208,13 @@ func (e *Engine) logWithLock(f func()) {
e.ll.Unlock()
}

func copyOutput(dst io.Writer, src io.Reader) {
scanner := bufio.NewScanner(src)
for scanner.Scan() {
dst.Write([]byte(scanner.Text() + "\n"))
}
}

func expandPath(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
home := os.Getenv("HOME")
Expand Down