Skip to content

Commit

Permalink
stdinReader.Read returns EOF only when the child process exits
Browse files Browse the repository at this point in the history
  • Loading branch information
makiuchi-d committed Apr 5, 2023
1 parent ef8ceab commit 8de0a4f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
21 changes: 3 additions & 18 deletions arelo.go
Expand Up @@ -295,33 +295,19 @@ type bytesErr struct {
// cmd.Wait() blocks until stdin.Read() returns.
// so stdinReader.Read() returns EOF when the child process exited.
type stdinReader struct {
ch <-chan bytesErr
input <-chan bytesErr
chldDone <-chan struct{}
done <-chan struct{}
}

func (s *stdinReader) discard() {
for {
select {
case <-s.ch:
case <-s.chldDone:
case <-s.done:
return
}
}
}

func (s *stdinReader) Read(b []byte) (int, error) {
select {
case be, ok := <-s.ch:
case be, ok := <-s.input:
if !ok {
return 0, io.EOF
}
return copy(b, be.bytes), be.err
case <-s.chldDone:
return 0, io.EOF
case <-s.done:
return 0, io.EOF
}
}

Expand Down Expand Up @@ -387,7 +373,7 @@ func runner(ctx context.Context, wg *sync.WaitGroup, cmd []string, delay time.Du
go func() {
log.Printf("[ARELO] start: %s", pcmd)
clearChBuf(chldDone)
stdin := &stdinReader{stdinC, chldDone, cmdctx.Done()}
stdin := &stdinReader{stdinC, chldDone}
err := runCmd(cmdctx, cmd, sig, stdin)
if err != nil {
log.Printf("[ARELO] command error: %v", err)
Expand All @@ -398,7 +384,6 @@ func runner(ctx context.Context, wg *sync.WaitGroup, cmd []string, delay time.Du
close(restart)
}

stdin.discard()
close(done)
}()

Expand Down
11 changes: 8 additions & 3 deletions arelo_windows.go
Expand Up @@ -40,11 +40,17 @@ func makeChildDoneChan() <-chan struct{} {
err := windows.GetExitCodeProcess(p, &code)
if err != nil {
log.Printf("GetExitCodeProcess: %v", err)
c <- struct{}{}
select {
case c <- struct{}{}:
default:
}
break
}
if code != STILL_ACTIVE {
c <- struct{}{}
select {
case c <- struct{}{}:
default:
}
break
}
}
Expand All @@ -57,7 +63,6 @@ func makeChildDoneChan() <-chan struct{} {
func waitCmd(cmd *exec.Cmd) error {
p, err := windows.OpenProcess(
windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(cmd.Process.Pid))
log.Printf("pid=%v handle=%v", cmd.Process.Pid, p)
if err != nil {
return xerrors.Errorf("OpenProcess: %w", err)
}
Expand Down

0 comments on commit 8de0a4f

Please sign in to comment.