forked from mirror/readline
fix deadlock in cancelable stdin
This commit is contained in:
parent
94eaec69a7
commit
fa8d922787
9
std.go
9
std.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -69,6 +70,7 @@ type CancelableStdin struct {
|
||||||
r io.Reader
|
r io.Reader
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
stop chan struct{}
|
stop chan struct{}
|
||||||
|
closed int32
|
||||||
notify chan struct{}
|
notify chan struct{}
|
||||||
data []byte
|
data []byte
|
||||||
read int
|
read int
|
||||||
|
@ -101,6 +103,9 @@ loop:
|
||||||
func (c *CancelableStdin) Read(b []byte) (n int, err error) {
|
func (c *CancelableStdin) Read(b []byte) (n int, err error) {
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
defer c.mutex.Unlock()
|
defer c.mutex.Unlock()
|
||||||
|
if atomic.LoadInt32(&c.closed) == 1 {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
c.data = b
|
c.data = b
|
||||||
c.notify <- struct{}{}
|
c.notify <- struct{}{}
|
||||||
|
@ -113,6 +118,8 @@ func (c *CancelableStdin) Read(b []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CancelableStdin) Close() error {
|
func (c *CancelableStdin) Close() error {
|
||||||
close(c.stop)
|
if atomic.CompareAndSwapInt32(&c.closed, 0, 1) {
|
||||||
|
close(c.stop)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue