From 5aca12cc3e1d0592fdbc42725b8671126594e57d Mon Sep 17 00:00:00 2001 From: xinglong Date: Mon, 1 Apr 2019 10:24:48 +0800 Subject: [PATCH] send messages with select to avoid deadlocks Fixes #287. --- handler.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/handler.go b/handler.go index 250cacf..b3545eb 100644 --- a/handler.go +++ b/handler.go @@ -155,10 +155,18 @@ func (h *TargetHandler) run(ctxt context.Context) { switch { case msg.Method != "": - h.qevents <- msg + select { + case h.qevents <- msg: + case <-ctxt.Done(): + return + } case msg.ID != 0: - h.qres <- msg + select { + case h.qres <- msg: + case <-ctxt.Done(): + return + } default: 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() // queue message - h.qcmd <- &cdproto.Message{ + select { + case h.qcmd <- &cdproto.Message{ ID: id, Method: cdproto.MethodType(methodType), Params: paramsBuf, + }: + case <- ctxt.Done(): + return ctxt.Err() } errch := make(chan error, 1)