Skip to content

Commit

Permalink
fix: handle panic when shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
galihrivanto committed Feb 1, 2021
1 parent a7a279d commit b504482
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/galihrivanto/go-inkscape

go 1.14

require github.com/galihrivanto/runner v0.1.0
require github.com/galihrivanto/runner v0.1.1
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
github.com/galihrivanto/runner v0.1.0 h1:E30VDypMgxBVMBtYM9dTfN3lbbo3eRBuIi7Kp8ljpsA=
github.com/galihrivanto/runner v0.1.0/go.mod h1:w4LT8uzPbepnSAuwVq6/7dVMz1hcW40FNWIgg8IHBaI=
github.com/galihrivanto/runner v0.1.1/go.mod h1:w4LT8uzPbepnSAuwVq6/7dVMz1hcW40FNWIgg8IHBaI=
31 changes: 22 additions & 9 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ var (
verbose bool
)

func debug(v ...interface{}) {
if !verbose {
return
}

log.Print("proxy:")
log.Println(v...)
}

type chanWriter struct {
out chan []byte
}
Expand Down Expand Up @@ -79,7 +88,13 @@ func (p *Proxy) runBackground(ctx context.Context, commandPath string, vars ...s
}
p.stdin = stdin

log.Println("proxy: run in background")
defer func() {
// only close channel when command closes
close(p.stdout)
close(p.stderr)
}()

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

Expand All @@ -90,7 +105,7 @@ func (p *Proxy) Run(args ...string) error {
return ErrCommandNotAvailable
}

log.Println("proxy:", commandPath)
debug(commandPath)

p.ctx, p.cancel = context.WithCancel(context.Background())

Expand All @@ -111,8 +126,6 @@ func (p *Proxy) Run(args ...string) error {
func (p *Proxy) Close() error {
p.cancel()
p.stdin.Close()
close(p.stdout)
close(p.stderr)

return nil
}
Expand Down Expand Up @@ -142,13 +155,13 @@ func (p *Proxy) waitReady(timeout time.Duration) error {
}

func (p *Proxy) sendCommand(b []byte) ([]byte, error) {
log.Println("proxy: wait ready")
debug("wait ready")
err := p.waitReady(30 * time.Second)
if err != nil {
return nil, err
}

log.Println("proxy: send command to stdin", string(b))
debug("send command to stdin", string(b))

// append new line
if !bytes.HasSuffix(b, []byte{'\n'}) {
Expand All @@ -170,20 +183,20 @@ waitLoop:
// for now, we can only check error message pattern
// ignore WARNING
if bytes.Contains(output, []byte("WARNING")) {
log.Println(string(bytesErr))
debug(string(bytesErr))
break
}

err = fmt.Errorf("%s", string(bytesErr))
break waitLoop
case output = <-p.stdout:
log.Println(string(output))
if len(output) == 0 {
break
}

// check if shell mode banner
if bytes.Contains(output, []byte(shellModeBanner)) {
debug(string(output))
break
}

Expand Down Expand Up @@ -217,7 +230,7 @@ func (p *Proxy) Svg2Pdf(svgIn, pdfOut string) error {
return err
}

log.Println("proxy: result", string(res))
debug("result", string(res))

return nil
}
Expand Down

0 comments on commit b504482

Please sign in to comment.