wrap close(wait) with sync.Once to avoid panic

This commit is contained in:
Anton Evangelatov 2021-07-20 11:49:59 +02:00 committed by Steven Allen
parent 8ddd12d144
commit cd4571f0b0

View File

@ -80,19 +80,24 @@ func ReaderParamEncoder(addr string) jsonrpc.Option {
type waitReadCloser struct {
io.ReadCloser
wait chan struct{}
wait chan struct{}
closeOnce sync.Once
}
func (w *waitReadCloser) Read(p []byte) (int, error) {
n, err := w.ReadCloser.Read(p)
if err != nil {
close(w.wait)
w.closeOnce.Do(func() {
close(w.wait)
})
}
return n, err
}
func (w *waitReadCloser) Close() error {
close(w.wait)
w.closeOnce.Do(func() {
close(w.wait)
})
return w.ReadCloser.Close()
}