Skip to content

Commit

Permalink
Fix race condition in Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Jan 25, 2024
1 parent 5d6b4d1 commit 1a6a68a
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions minify.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,30 +253,23 @@ func (m *M) Reader(mediatype string, r io.Reader) io.Reader {

// writer makes sure that errors from the minifier are passed down through Close (can be blocking).
type writer struct {
w io.WriteCloser
io.WriteCloser
wg sync.WaitGroup
closed bool
err error
}

// Write intercepts any writes to the writer.
func (z *writer) Write(b []byte) (int, error) {
if z.err != nil {
return 0, z.err
}
return z.w.Write(b)
}

// Close must be called when writing has finished. It returns the error from the minifier.
func (z *writer) Close() error {
if z.closed {
return nil
}
z.closed = true
if err := z.w.Close(); z.err == nil {
z.err = err
}
err := z.WriteCloser.Close()
z.wg.Wait()
if z.err == nil {
return err
}
return z.err
}

Expand Down

0 comments on commit 1a6a68a

Please sign in to comment.