Skip to content

Commit

Permalink
fix race during proxy running
Browse files Browse the repository at this point in the history
  • Loading branch information
galihrivanto committed Feb 5, 2021
1 parent 346f32a commit c34a6a4
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os/exec"
"strings"
"sync"
"time"

"github.com/galihrivanto/runner"
Expand Down Expand Up @@ -62,8 +63,11 @@ type Proxy struct {

cmd *exec.Cmd

// input and output
stdin io.WriteCloser
// input
lock sync.RWMutex
stdin io.WriteCloser

// output
stdout chan []byte
stderr chan []byte
}
Expand All @@ -86,16 +90,24 @@ func (p *Proxy) runBackground(ctx context.Context, commandPath string, vars ...s
if err != nil {
return err
}

p.lock.Lock()
p.stdin = stdin
p.lock.Unlock()

defer func() {
// only close channel when command closes
close(p.stdout)
close(p.stderr)
}()

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

debug("run in background")
return cmd.Run()

return cmd.Wait()
}

// Run start inkscape proxy
Expand Down Expand Up @@ -137,10 +149,13 @@ func (p *Proxy) waitReady(timeout time.Duration) error {
go func() {
for {
// query stdin availability every second
p.lock.RLock()
if p.stdin != nil {
p.lock.RUnlock()
close(ready)
return
}
p.lock.RUnlock()

<-time.After(time.Second)
}
Expand Down

0 comments on commit c34a6a4

Please sign in to comment.