Merge pull request #504 from filcloud/fix/312-panic-close-miner

fix panic when close miner
This commit is contained in:
Łukasz Magiera 2019-11-02 00:59:12 +01:00 committed by GitHub
commit 09e8cdc109
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -137,6 +137,7 @@ func (c *client) makeOutChan(ctx context.Context, ftyp reflect.Type, valOut int)
// unpack chan type to make sure it's reflect.BothDir // unpack chan type to make sure it's reflect.BothDir
ctyp := reflect.ChanOf(reflect.BothDir, ftyp.Out(valOut).Elem()) ctyp := reflect.ChanOf(reflect.BothDir, ftyp.Out(valOut).Elem())
ch := reflect.MakeChan(ctyp, 0) // todo: buffer? ch := reflect.MakeChan(ctyp, 0) // todo: buffer?
chCtx, chCancel := context.WithCancel(ctx)
retVal = ch.Convert(ftyp.Out(valOut)) retVal = ch.Convert(ftyp.Out(valOut))
buf := (&list.List{}).Init() buf := (&list.List{}).Init()
@ -144,6 +145,7 @@ func (c *client) makeOutChan(ctx context.Context, ftyp reflect.Type, valOut int)
return ctx, func(result []byte, ok bool) { return ctx, func(result []byte, ok bool) {
if !ok { if !ok {
chCancel()
// remote channel closed, close ours too // remote channel closed, close ours too
ch.Close() ch.Close()
return return
@ -173,13 +175,29 @@ func (c *client) makeOutChan(ctx context.Context, ftyp reflect.Type, valOut int)
go func() { go func() {
for buf.Len() > 0 { for buf.Len() > 0 {
front := buf.Front() front := buf.Front()
bufLk.Unlock() bufLk.Unlock()
ch.Send(front.Value.(reflect.Value).Elem()) // todo: select on ctx is probably a good idea cases := []reflect.SelectCase{
{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(chCtx.Done()),
},
{
Dir: reflect.SelectSend,
Chan: ch,
Send: front.Value.(reflect.Value).Elem(),
},
}
chosen, _, _ := reflect.Select(cases)
bufLk.Lock() bufLk.Lock()
buf.Remove(front)
switch chosen {
case 0:
buf.Init()
case 1:
buf.Remove(front)
}
} }
bufLk.Unlock() bufLk.Unlock()