Explorar o código

Fix goroutine leak in deadline

If the deadline timed out then the worker goroutine might have been
leaked trying to write to `result` which had no readers.
Evan Huus %!s(int64=6) %!d(string=hai) anos
pai
achega
eadf0b0259
Modificáronse 1 ficheiros con 5 adicións e 1 borrados
  1. 5 1
      deadline/deadline.go

+ 5 - 1
deadline/deadline.go

@@ -32,7 +32,11 @@ func (d *Deadline) Run(work func(<-chan struct{}) error) error {
 	stopper := make(chan struct{})
 
 	go func() {
-		result <- work(stopper)
+		value := work(stopper)
+		select {
+		case result <- value:
+		case <-stopper:
+		}
 	}()
 
 	select {