Skip to content

Commit

Permalink
binarylog: Don't leak the flusher goroutine when closing a Sink (#4583)
Browse files Browse the repository at this point in the history
time.Ticker.Stop() doesn't close the ticker channel, so we need to signal the goroutine to die some other way
  • Loading branch information
Jille committed Jul 7, 2021
1 parent dd58992 commit 91e0aeb
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/binarylog/sink.go
Expand Up @@ -92,6 +92,7 @@ type bufferedSink struct {

writeStartOnce sync.Once
writeTicker *time.Ticker
done chan struct{}
}

func (fs *bufferedSink) Write(e *pb.GrpcLogEntry) error {
Expand All @@ -113,7 +114,12 @@ const (
func (fs *bufferedSink) startFlushGoroutine() {
fs.writeTicker = time.NewTicker(bufFlushDuration)
go func() {
for range fs.writeTicker.C {
for {
select {
case <-fs.done:
return
case <-fs.writeTicker.C:
}
fs.mu.Lock()
if err := fs.buf.Flush(); err != nil {
grpclogLogger.Warningf("failed to flush to Sink: %v", err)
Expand All @@ -127,6 +133,7 @@ func (fs *bufferedSink) Close() error {
if fs.writeTicker != nil {
fs.writeTicker.Stop()
}
close(fs.done)
fs.mu.Lock()
if err := fs.buf.Flush(); err != nil {
grpclogLogger.Warningf("failed to flush to Sink: %v", err)
Expand Down Expand Up @@ -155,5 +162,6 @@ func NewBufferedSink(o io.WriteCloser) Sink {
closer: o,
out: newWriterSink(bufW),
buf: bufW,
done: make(chan struct{}),
}
}

0 comments on commit 91e0aeb

Please sign in to comment.