From a88a1209384195bb61bce689299b1dcd4ac72176 Mon Sep 17 00:00:00 2001 From: v2ray Date: Tue, 12 Jul 2016 12:54:09 +0200 Subject: [PATCH] Fix deadlock in kcp.read --- transport/internet/kcp/connection.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/transport/internet/kcp/connection.go b/transport/internet/kcp/connection.go index 8676e8d8..331acfc4 100644 --- a/transport/internet/kcp/connection.go +++ b/transport/internet/kcp/connection.go @@ -179,8 +179,12 @@ func (this *Connection) Read(b []byte) (int, error) { return nBytes, nil } var timer *time.Timer - if !this.rd.IsZero() && this.rd.Before(time.Now()) { - timer = time.AfterFunc(this.rd.Sub(time.Now()), this.dataInputCond.Signal) + if !this.rd.IsZero() { + duration := this.rd.Sub(time.Now()) + if duration <= 0 { + return 0, errTimeout + } + timer = time.AfterFunc(duration, this.dataInputCond.Signal) } this.dataInputCond.L.Lock() this.dataInputCond.Wait()