mirror of https://github.com/cbeuw/Cloak
Regression: closing in the middle of a read will block
This commit is contained in:
parent
4351815a4a
commit
334712c413
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -19,7 +20,7 @@ type bufferedPipe struct {
|
||||||
// only alloc when on first Read or Write
|
// only alloc when on first Read or Write
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
|
|
||||||
closed bool
|
closed uint32
|
||||||
rwCond *sync.Cond
|
rwCond *sync.Cond
|
||||||
rDeadline time.Time
|
rDeadline time.Time
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +39,7 @@ func (p *bufferedPipe) Read(target []byte) (int, error) {
|
||||||
p.buf = new(bytes.Buffer)
|
p.buf = new(bytes.Buffer)
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
if p.closed && p.buf.Len() == 0 {
|
if atomic.LoadUint32(&p.closed) == 1 && p.buf.Len() == 0 {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
if !p.rDeadline.IsZero() {
|
if !p.rDeadline.IsZero() {
|
||||||
|
|
@ -66,7 +67,7 @@ func (p *bufferedPipe) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
p.buf = new(bytes.Buffer)
|
p.buf = new(bytes.Buffer)
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
if p.closed && p.buf.Len() == 0 {
|
if atomic.LoadUint32(&p.closed) == 1 && p.buf.Len() == 0 {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
if !p.rDeadline.IsZero() {
|
if !p.rDeadline.IsZero() {
|
||||||
|
|
@ -96,7 +97,7 @@ func (p *bufferedPipe) Write(input []byte) (int, error) {
|
||||||
p.buf = new(bytes.Buffer)
|
p.buf = new(bytes.Buffer)
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
if p.closed {
|
if atomic.LoadUint32(&p.closed) == 1 {
|
||||||
return 0, io.ErrClosedPipe
|
return 0, io.ErrClosedPipe
|
||||||
}
|
}
|
||||||
if p.buf.Len() <= BUF_SIZE_LIMIT {
|
if p.buf.Len() <= BUF_SIZE_LIMIT {
|
||||||
|
|
@ -112,10 +113,7 @@ func (p *bufferedPipe) Write(input []byte) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *bufferedPipe) Close() error {
|
func (p *bufferedPipe) Close() error {
|
||||||
p.rwCond.L.Lock()
|
atomic.StoreUint32(&p.closed, 1)
|
||||||
defer p.rwCond.L.Unlock()
|
|
||||||
|
|
||||||
p.closed = true
|
|
||||||
p.rwCond.Broadcast()
|
p.rwCond.Broadcast()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -15,7 +16,7 @@ import (
|
||||||
type datagramBuffer struct {
|
type datagramBuffer struct {
|
||||||
pLens []int
|
pLens []int
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
closed bool
|
closed uint32
|
||||||
rwCond *sync.Cond
|
rwCond *sync.Cond
|
||||||
rDeadline time.Time
|
rDeadline time.Time
|
||||||
}
|
}
|
||||||
|
|
@ -34,7 +35,7 @@ func (d *datagramBuffer) Read(target []byte) (int, error) {
|
||||||
d.buf = new(bytes.Buffer)
|
d.buf = new(bytes.Buffer)
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
if d.closed && len(d.pLens) == 0 {
|
if atomic.LoadUint32(&d.closed) == 1 && len(d.pLens) == 0 {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,7 +70,7 @@ func (d *datagramBuffer) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
d.buf = new(bytes.Buffer)
|
d.buf = new(bytes.Buffer)
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
if d.closed && len(d.pLens) == 0 {
|
if atomic.LoadUint32(&d.closed) == 1 && len(d.pLens) == 0 {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,7 +104,7 @@ func (d *datagramBuffer) Write(f Frame) (toBeClosed bool, err error) {
|
||||||
d.buf = new(bytes.Buffer)
|
d.buf = new(bytes.Buffer)
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
if d.closed {
|
if atomic.LoadUint32(&d.closed) == 1 {
|
||||||
return true, io.ErrClosedPipe
|
return true, io.ErrClosedPipe
|
||||||
}
|
}
|
||||||
if d.buf.Len() <= BUF_SIZE_LIMIT {
|
if d.buf.Len() <= BUF_SIZE_LIMIT {
|
||||||
|
|
@ -114,7 +115,7 @@ func (d *datagramBuffer) Write(f Frame) (toBeClosed bool, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Closing != C_NOOP {
|
if f.Closing != C_NOOP {
|
||||||
d.closed = true
|
atomic.StoreUint32(&d.closed, 1)
|
||||||
d.rwCond.Broadcast()
|
d.rwCond.Broadcast()
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
@ -128,10 +129,7 @@ func (d *datagramBuffer) Write(f Frame) (toBeClosed bool, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *datagramBuffer) Close() error {
|
func (d *datagramBuffer) Close() error {
|
||||||
d.rwCond.L.Lock()
|
atomic.StoreUint32(&d.closed, 1)
|
||||||
defer d.rwCond.L.Unlock()
|
|
||||||
|
|
||||||
d.closed = true
|
|
||||||
d.rwCond.Broadcast()
|
d.rwCond.Broadcast()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package multiplex
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -66,7 +67,7 @@ func TestDatagramBuffer_RW(t *testing.T) {
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !pipe.closed {
|
if atomic.LoadUint32(&pipe.closed) != 1 {
|
||||||
t.Error("expecting closed pipe, not closed")
|
t.Error("expecting closed pipe, not closed")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue