1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 09:56:18 -05:00

fix a data race in KCP

This commit is contained in:
Darien Raymond 2017-12-17 01:22:39 +01:00
parent 048ffbc7dc
commit 3cc6d8f653
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 13 additions and 21 deletions

View File

@ -8,7 +8,6 @@ import (
"time" "time"
"v2ray.com/core/app/log" "v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/predicate" "v2ray.com/core/common/predicate"
) )
@ -360,16 +359,12 @@ func (v *Connection) Write(b []byte) (int, error) {
return totalWritten, io.ErrClosedPipe return totalWritten, io.ErrClosedPipe
} }
for { for v.sendingWorker.Push(func(bb []byte) (int, error) {
rb := v.sendingWorker.Push() n := copy(bb[:v.mss], b[totalWritten:])
if rb == nil { totalWritten += n
break return n, nil
} }) {
common.Must(rb.Reset(func(bb []byte) (int, error) {
return copy(bb[:v.mss], b[totalWritten:]), nil
}))
v.dataUpdater.WakeUp() v.dataUpdater.WakeUp()
totalWritten += rb.Len()
if totalWritten == len(b) { if totalWritten == len(b) {
return totalWritten, nil return totalWritten, nil
} }
@ -390,14 +385,9 @@ func (v *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
return io.ErrClosedPipe return io.ErrClosedPipe
} }
for { for v.sendingWorker.Push(func(bb []byte) (int, error) {
rb := v.sendingWorker.Push()
if rb == nil {
break
}
common.Must(rb.Reset(func(bb []byte) (int, error) {
return mb.Read(bb[:v.mss]) return mb.Read(bb[:v.mss])
})) }) {
v.dataUpdater.WakeUp() v.dataUpdater.WakeUp()
if mb.IsEmpty() { if mb.IsEmpty() {
return nil return nil

View File

@ -3,6 +3,7 @@ package kcp
import ( import (
"sync" "sync"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
) )
@ -284,17 +285,18 @@ func (v *SendingWorker) ProcessSegment(current uint32, seg *AckSegment, rto uint
} }
} }
func (v *SendingWorker) Push() *buf.Buffer { func (v *SendingWorker) Push(f buf.Supplier) bool {
v.Lock() v.Lock()
defer v.Unlock() defer v.Unlock()
if v.window.IsFull() { if v.window.IsFull() {
return nil return false
} }
b := v.window.Push(v.nextNumber) b := v.window.Push(v.nextNumber)
v.nextNumber++ v.nextNumber++
return b common.Must(b.Reset(f))
return true
} }
func (v *SendingWorker) Write(seg Segment) error { func (v *SendingWorker) Write(seg Segment) error {