Browse Source

rafthttp: return err if stopped before setting cancel in dial()

The original workflow may fail to cancel if stop() cancels the finished
request just before dial() assigning a new cancel. This commit checks
streamReader status before setting cancel to avoid this problem.

It is tested at travis for 300 times. go 1.5 always works well, while
go 1.4 fails to stop once.
Yicheng Qin 10 years ago
parent
commit
6fc638673c
1 changed files with 6 additions and 0 deletions
  1. 6 0
      rafthttp/stream.go

+ 6 - 0
rafthttp/stream.go

@@ -427,6 +427,12 @@ func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) {
 	}
 	}
 
 
 	cr.mu.Lock()
 	cr.mu.Lock()
+	select {
+	case <-cr.stopc:
+		cr.mu.Unlock()
+		return nil, fmt.Errorf("stream reader is stopped")
+	default:
+	}
 	cr.cancel = httputil.RequestCanceler(cr.tr, req)
 	cr.cancel = httputil.RequestCanceler(cr.tr, req)
 	cr.mu.Unlock()
 	cr.mu.Unlock()