Closed OGKevin closed 5 years ago
Found it, replace close(ch)
with ch <- nil
for the chan
in question and it fixes it.
//defer close(ch)
defer func() {
ch <- nil
}()
closing the channel will make a select statement constantly select the closed channel e.g:
func (p ProcessListChan) MarshalStream(enc *gojay.StreamEncoder) {
select {
case <-enc.Done():
return
case o := <-p:
enc.AddObject(o)
}
}
case <-p
is activated rapidly causing the panic.
I would say that gojay needs to implement better mutex lock to ensure only 1 go routine can close the done chan as users expect contex.Context
to be able to work good under concurrent load.
When i run
in a loop of 1 sec interval, i manage to get
This is caused by https://github.com/francoispqt/gojay/blob/90d953358b68936b4791b8db205b3a5918b1b7a5/encode_stream.go#L124-L136
on line 133, however i've not managed to locate how it got closed right after the
<-done
check and before theclose()
call. However, i can constantly reproduce this 🤔 I have a feeling there should be a mutex with write lock to prevent this from happening.