From 6fc638673c003166f888d243580085011914443f Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Tue, 25 Aug 2015 08:52:48 -0700 Subject: [PATCH] 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. --- rafthttp/stream.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rafthttp/stream.go b/rafthttp/stream.go index 5e457c468..d2f8e3bca 100644 --- a/rafthttp/stream.go +++ b/rafthttp/stream.go @@ -427,6 +427,12 @@ func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) { } 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.mu.Unlock()