Browse Source

fix race condition when we stop and start monitor multiple times, the doneCh is closed and never recover.

pull/10368/head
Dhia Ayachi 4 years ago
parent
commit
1eeddf7ad8
  1. 10
      logging/monitor/monitor.go
  2. 2
      logging/monitor/monitor_test.go

10
logging/monitor/monitor.go

@ -35,7 +35,7 @@ type monitor struct {
droppedCount int droppedCount int
// doneCh coordinates the shutdown of logCh // doneCh coordinates the shutdown of logCh
doneCh chan struct{} doneCh chan bool
// Defaults to 512. // Defaults to 512.
bufSize int bufSize int
@ -58,7 +58,7 @@ func New(cfg Config) Monitor {
sw := &monitor{ sw := &monitor{
logger: cfg.Logger, logger: cfg.Logger,
logCh: make(chan []byte, bufSize), logCh: make(chan []byte, bufSize),
doneCh: make(chan struct{}, 1), doneCh: make(chan bool, 1),
bufSize: bufSize, bufSize: bufSize,
} }
@ -72,7 +72,11 @@ func New(cfg Config) Monitor {
// Stop deregisters the sink and stops the monitoring process // Stop deregisters the sink and stops the monitoring process
func (d *monitor) Stop() int { func (d *monitor) Stop() int {
d.logger.DeregisterSink(d.sink) d.logger.DeregisterSink(d.sink)
close(d.doneCh) select {
case d.doneCh <- true:
default:
}
return d.droppedCount return d.droppedCount
} }

2
logging/monitor/monitor_test.go

@ -178,7 +178,7 @@ func TestMonitor_WriteStopped(t *testing.T) {
mwriter := &monitor{ mwriter := &monitor{
logger: logger, logger: logger,
doneCh: make(chan struct{}, 1), doneCh: make(chan bool, 1),
} }
mwriter.Stop() mwriter.Stop()

Loading…
Cancel
Save