diff --git a/client.go b/client.go index 19b809ad..817e664e 100644 --- a/client.go +++ b/client.go @@ -799,9 +799,15 @@ func (c *client) Publish(topic string, qos byte, retained bool, payload interfac if publishWaitTimeout == 0 { publishWaitTimeout = time.Second * 30 } + t := time.NewTimer(publishWaitTimeout) + defer func() { + if !t.Stop() { + <-t.C + } + }() select { case c.obound <- &PacketAndToken{p: pub, t: token}: - case <-time.After(publishWaitTimeout): + case <-t.C: token.setError(errors.New("publish was broken by timeout")) } }