From 8c2b18cac42a1b8f300f362e99c6ca8f28156a2d Mon Sep 17 00:00:00 2001 From: v2ray Date: Tue, 19 Apr 2016 14:51:52 +0200 Subject: [PATCH] fix again the race condition issue --- transport/ray/direct.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/transport/ray/direct.go b/transport/ray/direct.go index 18ba161d0..30f5b70c7 100644 --- a/transport/ray/direct.go +++ b/transport/ray/direct.go @@ -57,11 +57,13 @@ func (this *Stream) Read() (*alloc.Buffer, error) { return nil, io.EOF } this.access.RLock() - defer this.access.RUnlock() if this.buffer == nil { + this.access.RUnlock() return nil, io.EOF } - result, open := <-this.buffer + channel := this.buffer + this.access.RUnlock() + result, open := <-channel if !open { return nil, io.EOF } @@ -72,17 +74,11 @@ func (this *Stream) Write(data *alloc.Buffer) error { if this.closed { return io.EOF } - if this.buffer == nil { - return io.EOF - } this.access.RLock() defer this.access.RUnlock() if this.closed { return io.EOF } - if this.buffer == nil { - return io.EOF - } this.buffer <- data return nil } @@ -91,8 +87,8 @@ func (this *Stream) Close() { if this.closed { return } - this.access.RLock() - defer this.access.RUnlock() + this.access.Lock() + defer this.access.Unlock() if this.closed { return }