Skip to content

Commit

Permalink
Merge pull request #22 from hectorj/fix-memory-leak
Browse files Browse the repository at this point in the history
Fix memory leak, albeit in an hacky way.
  • Loading branch information
vjt authored Jun 21, 2021
2 parents 30810fd + 07d1c6e commit bb49060
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions scanner/clamav.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package scanner

import (
clamd "github.com/dutchcoders/go-clamd"
"io"

clamd "github.com/dutchcoders/go-clamd"
)

/*
Expand Down Expand Up @@ -36,7 +37,9 @@ func (c *Clamav) Scan(reader io.Reader) (*Result, error) {
c.logger.Println("Sending to clamav")
}

ch, err := c.clam.ScanStream(reader, nil)
abortChannel := make(chan bool)
defer close(abortChannel) // necessary to not leak a goroutine. See https://github.com/dutchcoders/go-clamd/issues/9
ch, err := c.clam.ScanStream(reader, abortChannel)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -65,6 +68,11 @@ func (c *Clamav) Scan(reader io.Reader) (*Result, error) {
c.logger.Println(" result of scan:", result)
}

go func() {
for range ch {
} // empty the channel so the goroutine from go-clamd/*CLAMDConn.readResponse() doesn't get stuck
}()

return result, nil
}

Expand Down

0 comments on commit bb49060

Please sign in to comment.