Skip to content

Commit be3c65d

Browse files
committed
fix: prevent race due shared verbose variable
1 parent 23eab15 commit be3c65d

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

proxy.go

+19-23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log"
9+
"os"
910
"os/exec"
1011
"strings"
1112
"time"
@@ -25,18 +26,8 @@ var (
2526
ErrCommandNotReady = errors.New("inkscape not ready")
2627
)
2728

28-
var (
29-
bufferPool = NewSizedBufferPool(5, 1024*1024)
30-
verbose bool
31-
)
32-
33-
func debug(v ...interface{}) {
34-
if !verbose {
35-
return
36-
}
37-
38-
log.Print(append([]interface{}{"proxy:"}, v...)...)
39-
}
29+
// bytes.Buffer pool
30+
var bufferPool = NewSizedBufferPool(5, 1024*1024)
4031

4132
type chanWriter struct {
4233
out chan []byte
@@ -60,6 +51,7 @@ func (w *chanWriter) Write(data []byte) (int, error) {
6051
// instance via stdin
6152
type Proxy struct {
6253
options Options
54+
logger *log.Logger
6355

6456
// context and cancellation
6557
ctx context.Context
@@ -76,6 +68,12 @@ type Proxy struct {
7668
stderr chan []byte
7769
}
7870

71+
func (p *Proxy) debug(args ...interface{}) {
72+
if p.options.verbose {
73+
p.logger.Println(args...)
74+
}
75+
}
76+
7977
// runBackground run inkscape instance in background
8078
func (p *Proxy) runBackground(ctx context.Context, commandPath string, vars ...string) error {
8179
args := []string{
@@ -108,7 +106,7 @@ func (p *Proxy) runBackground(ctx context.Context, commandPath string, vars ...s
108106
defer stdin.Close()
109107

110108
// start command and wait it close
111-
debug("run in background")
109+
p.debug("run in background")
112110
if err := cmd.Start(); err != nil {
113111
return err
114112
}
@@ -140,7 +138,7 @@ wait:
140138
return cmd.Wait()
141139

142140
case command := <-p.requestQueue:
143-
debug("write command ", string(command))
141+
p.debug("write command ", string(command))
144142
if _, err := stdin.Write(command); err != nil {
145143
p.stderr <- []byte(err.Error())
146144
}
@@ -173,7 +171,7 @@ func (p *Proxy) Run(args ...string) error {
173171
return ErrCommandNotAvailable
174172
}
175173

176-
debug(commandPath)
174+
p.debug(commandPath)
177175

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

@@ -215,14 +213,14 @@ func (p *Proxy) sendCommand(b []byte, waitPrompt ...bool) ([]byte, error) {
215213
}
216214

217215
// wait available
218-
debug("wait prompt available")
216+
p.debug("wait prompt available")
219217
<-p.requestLimiter
220218
defer func() {
221219
// make it available again
222220
p.requestLimiter <- struct{}{}
223221
}()
224222

225-
debug("send command to stdin ", string(b))
223+
p.debug("send command to stdin ", string(b))
226224

227225
// drain old err and out
228226
drain(p.stderr)
@@ -250,7 +248,7 @@ waitLoop:
250248
for {
251249
// wait until received prompt
252250
bytesOut := <-p.stdout
253-
debug(string(bytesOut))
251+
p.debug(string(bytesOut))
254252
parts := bytes.Split(bytesOut, []byte("\n"))
255253
for _, part := range parts {
256254
if isPrompt(part) {
@@ -267,7 +265,7 @@ errLoop:
267265
select {
268266
case bytesErr := <-p.stderr:
269267
if len(bytesErr) > 0 {
270-
debug(string(bytesErr))
268+
p.debug(string(bytesErr))
271269
err = fmt.Errorf("%s", string(bytesErr))
272270
}
273271
default:
@@ -303,7 +301,7 @@ func (p *Proxy) Svg2Pdf(svgIn, pdfOut string) error {
303301
return err
304302
}
305303

306-
debug("result", string(res))
304+
p.debug("result", string(res))
307305

308306
return nil
309307
}
@@ -320,13 +318,11 @@ func NewProxy(opts ...Option) *Proxy {
320318
// merge options
321319
options = mergeOptions(options, opts...)
322320

323-
// check verbosity
324-
verbose = options.verbose
325-
326321
return &Proxy{
327322
options: options,
328323
stdout: make(chan []byte, 100),
329324
stderr: make(chan []byte, 100),
325+
logger: log.New(os.Stdout, "[debug]", log.Lshortfile),
330326

331327
// limit request to one request at time
332328
requestLimiter: make(chan struct{}, 1),

0 commit comments

Comments
 (0)