send messages with select to avoid deadlocks

Fixes #287.
This commit is contained in:
xinglong 2019-04-01 10:24:48 +08:00 committed by Daniel Martí
parent e9aa66f87e
commit 5aca12cc3e

View File

@ -155,10 +155,18 @@ func (h *TargetHandler) run(ctxt context.Context) {
switch { switch {
case msg.Method != "": case msg.Method != "":
h.qevents <- msg select {
case h.qevents <- msg:
case <-ctxt.Done():
return
}
case msg.ID != 0: case msg.ID != 0:
h.qres <- msg select {
case h.qres <- msg:
case <-ctxt.Done():
return
}
default: default:
h.errf("ignoring malformed incoming message (missing id or method): %#v", msg) h.errf("ignoring malformed incoming message (missing id or method): %#v", msg)
@ -355,10 +363,14 @@ func (h *TargetHandler) Execute(ctxt context.Context, methodType string, params
h.resrw.Unlock() h.resrw.Unlock()
// queue message // queue message
h.qcmd <- &cdproto.Message{ select {
case h.qcmd <- &cdproto.Message{
ID: id, ID: id,
Method: cdproto.MethodType(methodType), Method: cdproto.MethodType(methodType),
Params: paramsBuf, Params: paramsBuf,
}:
case <- ctxt.Done():
return ctxt.Err()
} }
errch := make(chan error, 1) errch := make(chan error, 1)