Skip to content

Commit

Permalink
fix race condition when we stop and start monitor multiple times, the…
Browse files Browse the repository at this point in the history
… doneCh is closed and never recover.
  • Loading branch information
dhiaayachi committed Jun 9, 2021
1 parent 6c39a98 commit 1eeddf7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions logging/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type monitor struct {
droppedCount int

// doneCh coordinates the shutdown of logCh
doneCh chan struct{}
doneCh chan bool

// Defaults to 512.
bufSize int
Expand All @@ -58,7 +58,7 @@ func New(cfg Config) Monitor {
sw := &monitor{
logger: cfg.Logger,
logCh: make(chan []byte, bufSize),
doneCh: make(chan struct{}, 1),
doneCh: make(chan bool, 1),
bufSize: bufSize,
}

Expand All @@ -72,7 +72,11 @@ func New(cfg Config) Monitor {
// Stop deregisters the sink and stops the monitoring process
func (d *monitor) Stop() int {
d.logger.DeregisterSink(d.sink)
close(d.doneCh)
select {
case d.doneCh <- true:
default:
}

return d.droppedCount
}

Expand Down
2 changes: 1 addition & 1 deletion logging/monitor/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestMonitor_WriteStopped(t *testing.T) {

mwriter := &monitor{
logger: logger,
doneCh: make(chan struct{}, 1),
doneCh: make(chan bool, 1),
}

mwriter.Stop()
Expand Down

0 comments on commit 1eeddf7

Please sign in to comment.