Skip to content

Commit

Permalink
Merge pull request #28 from codecrafters-io/exe-kill-patch
Browse files Browse the repository at this point in the history
CC-1165: Fix exe#kill issues
  • Loading branch information
ryan-gang authored Apr 29, 2024
2 parents 794d14e + c0a2978 commit b170a07
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.history/
17 changes: 9 additions & 8 deletions executable/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ func (e *Executable) Start(args ...string) error {
}

// TODO: Use timeout!
e.cmd = exec.Command(e.path, args...)
e.cmd.Dir = e.WorkingDir
e.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd := exec.Command(e.path, args...)
cmd.Dir = e.WorkingDir
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
e.readDone = make(chan bool)
e.atleastOneReadDone = false

// Setup stdout capture
e.stdoutPipe, err = e.cmd.StdoutPipe()
e.stdoutPipe, err = cmd.StdoutPipe()
if err != nil {
return err
}
Expand All @@ -142,24 +142,25 @@ func (e *Executable) Start(args ...string) error {
e.stdoutLineWriter = linewriter.New(newLoggerWriter(e.loggerFunc), 500*time.Millisecond)

// Setup stderr relay
e.stderrPipe, err = e.cmd.StderrPipe()
e.stderrPipe, err = cmd.StderrPipe()
if err != nil {
return err
}
e.stderrBytes = []byte{}
e.stderrBuffer = bytes.NewBuffer(e.stderrBytes)
e.stderrLineWriter = linewriter.New(newLoggerWriter(e.loggerFunc), 500*time.Millisecond)

e.StdinPipe, err = e.cmd.StdinPipe()
e.StdinPipe, err = cmd.StdinPipe()
if err != nil {
return err
}

err = e.cmd.Start()
err = cmd.Start()
if err != nil {
return err
}

// At this point, it is safe to set e.cmd as cmd, if any of the above steps fail, we don't want to leave e.cmd in an inconsistent state
e.cmd = cmd
e.setupIORelay(e.stdoutPipe, e.stdoutBuffer, e.stdoutLineWriter)
e.setupIORelay(e.stderrPipe, e.stderrBuffer, e.stderrLineWriter)

Expand Down

0 comments on commit b170a07

Please sign in to comment.